Newbie Focus Traversal Problem
I got chewed out for posting this in the newbie forum, so I'll try here.
I've cut and pasted focus traversal code from two different online examples, and one example from this forum. They all work as stand alone examples, but none of them works when I paste them into my own code.
One example is at http://forum.java.sun.com/thread.jspa?threadID=5117033&tstart=0
and the other example (the traversal code from this post http://forum.java.sun.com/thread.jspa?threadID=738872 looks like this.
None of them does anything when I hit the tab key. What obvious thing am I, as a newbie, overlooking? (Whew! this is sooooo much harder than C++)
import javax.swing.JDialog;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import javax.swing.border.*;
publicclass LoginDialogextends JDialogimplements ActionListener{
privateboolean cancel =false;
privateboolean loggedIn =false;
private String postUrl;
private JButton cancelButton;
private JButton loginButton;
private JPanel myPanel;
private JTextArea taLogin;
private JPasswordField taPassword;
private JTextArea taAccount;
private Border outline;
private Border margins;
Component[] focusList =new Component[3];
int focusNumber = 0;
LoginDialog(JFrame frame){
super(frame,"Login to Your Merchant Account",true);
myPanel =new JPanel();
margins = BorderFactory.createEmptyBorder(15, 15, 15, 15);
myPanel.setBorder(margins);
getContentPane().add(myPanel);
myPanel.setMaximumSize(new Dimension(100,100));
myPanel.setLayout(new GridLayout(4,2));
outline = BorderFactory.createLineBorder( Color.black );
myPanel.add(new JLabel("Merchant Account Name: "));
taAccount =new JTextArea("",1,16);
myPanel.add(taAccount);
taAccount.setBorder(outline);
myPanel.add(new JLabel("Admin Acct Name:"));
taLogin =new JTextArea("",1,10);
myPanel.add(taLogin);
taLogin.setBorder(outline);
myPanel.add(new JLabel("Admin Acct Password:"));
taPassword =new JPasswordField("");
taPassword.setEchoChar('*');
taPassword.addActionListener(this);
myPanel.add(taPassword);
cancelButton =new JButton("Cancel");
cancelButton.addActionListener(this);
cancelButton.setBounds(80, 40, 100, 100);
myPanel.add(cancelButton);
loginButton =new JButton("Login");
loginButton.addActionListener(this);
myPanel.add(loginButton);
focusList[0] = taAccount;
focusList[1] = taLogin;
focusList[2] = taPassword;
pack();
//setFocusTraversalPolicy(new MyFocusTraversalPolicy()); // commenting or uncommenting makes no difference
setLocationRelativeTo(frame);
setVisible(true);
}
publicvoid actionPerformed(ActionEvent e){
Object source = e.getSource();
boolean good =false;
loggedIn =false;
if(source==cancelButton){
cancel =true;
setVisible(false);
}elseif (source==loginButton){
String account = taAccount.getText();
String login = taLogin.getText();
String password =new String(taPassword.getPassword());
String postData = account+")+("+login+")+("+password;
try{
URL my_url =new URL("http://www.--myURL--.com/logger.php");
HttpURLConnection connection = (HttpURLConnection)my_url.openConnection();
connection.setDoOutput(true);
connection.setUseCaches (false);
connection.setRequestMethod("POST");
connection.setFollowRedirects(true);
connection.setRequestProperty("Content-Length",""+postData.length());
connection.setRequestProperty("Content-Language","en-US");
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
DataOutputStream posted =new DataOutputStream (connection.getOutputStream ());
posted.writeBytes(postData);
posted.flush ();
posted.close ();
BufferedReader inStream =new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
good =false;
while ((inputLine = inStream.readLine()) !=null){
System.out.println("Server response: "+inputLine);
if (inputLine.substring(0,4).equals("url=")){
postUrl = inputLine.substring(4);
good =true;
cancel =false;
loggedIn =true;
}elseif (inputLine.equals("invalid login")){
good =true;// server did respond, but response was "invalid login"
loggedIn =false;
}
}
inStream.close();
if (!good){
JOptionPane.showMessageDialog(null,"Unable to contact server.\nTry again later.","alert", JOptionPane.ERROR_MESSAGE);
}
}catch (MalformedURLException me){
System.out.println("MalformedURLException: " + me);
JOptionPane.showMessageDialog(null,"Unable to contact server.\nTry again later.","alert", JOptionPane.ERROR_MESSAGE);
}catch (IOException ioe){
System.out.println("IOException: " + ioe);
JOptionPane.showMessageDialog(null,"Unable to contact server.\nTry again later.","alert", JOptionPane.ERROR_MESSAGE);
}
if (loggedIn){
setVisible(false);
}else{
JOptionPane.showMessageDialog(null,"Login or password not correct.","alert", JOptionPane.ERROR_MESSAGE);
}
}
}
publicboolean ifCancel(){return cancel;}
publicboolean ifLoggedIn(){return loggedIn;}
public String getPostUrl(){return postUrl;}
class MyFocusTraversalPolicyextends FocusTraversalPolicy
{
public Component getComponentAfter(Container focusCycleRoot,Component aComponent)
{
focusNumber = (focusNumber+1) % focusList.length;
return focusList[focusNumber];
}
public Component getComponentBefore(Container focusCycleRoot,Component aComponent)
{
focusNumber = (focusList.length+focusNumber-1) % focusList.length;
return focusList[focusNumber];
}
public Component getDefaultComponent(Container focusCycleRoot){return focusList[0];}
public Component getLastComponent(Container focusCycleRoot){return focusList[focusList.length-1];}
public Component getFirstComponent(Container focusCycleRoot){return focusList[0];}
}
}

