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];}

}

}

[12371 byte] By [fiziwiga] at [2007-11-26 12:58:01]
# 1

change the JTextAreas to JTextFields and it seems to do what you want

(listeners etc stripped for brevity)

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

import javax.swing.border.*;

class LoginDialog extends JDialog {

private JButton cancelButton;

private JButton loginButton;

private JPanel myPanel;

private JTextField taLogin;

private JPasswordField taPassword;

private JTextField 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);

taAccount = new JTextField(16);

myPanel.add(taAccount);

taAccount.setBorder(outline);

myPanel.add(new JLabel("Admin Acct Name:"));

//taLogin = new JTextArea("",1,10);

taLogin = new JTextField(16);

myPanel.add(taLogin);

taLogin.setBorder(outline);

myPanel.add(new JLabel("Admin Acct Password:"));

taPassword = new JPasswordField("");

taPassword.setEchoChar('*');

myPanel.add(taPassword);

cancelButton = new JButton("Cancel");

cancelButton.setBounds(80, 40, 100, 100);

myPanel.add(cancelButton);

loginButton = new JButton("Login");

myPanel.add(loginButton);

focusList[0] = taAccount;

focusList[1] = taLogin;

focusList[2] = taPassword;

pack();

setFocusTraversalPolicy(new MyFocusTraversalPolicy());

setLocationRelativeTo(frame);

setVisible(true);

}

class MyFocusTraversalPolicy extends 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];}

}

public static void main(String[] args){new LoginDialog(new JFrame());}

}

Michael_Dunna at 2007-7-7 16:54:58 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thank you very much!I won't even pretend to know why that makes a difference. Is it possible to ever really learn Java?--gary
fiziwiga at 2007-7-7 16:54:58 > top of Java-index,Desktop,Core GUI APIs...
# 3

textAreas process the tab key, so it is consumed before it gets to the traversalPolicy

if your login dialog is not going to change that much, and all you want is for the

traversal to loop through the textfields/passwordfield (and not the buttons),

it might be a lot easier for you if you just added these 2 lines

cancelButton.setFocusable(false);

loginButton.setFocusable(false);

and skip the focusTraversalPolicy

Michael_Dunna at 2007-7-7 16:54:58 > top of Java-index,Desktop,Core GUI APIs...