Results 1 to 1 of 1
Thread: Focus - Traversal Issue
- 05-30-2012, 11:14 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
Focus - Traversal Issue
I understand that the default forward and backward traversal keys are TAB and SHIFT+TAB respectively.
In some contexts, these defaults cause confusion for users; for example, when users are entering data, the return key is a more appropriate choice for the forward traversal key.
In the following code, a dialog is popped up when a user presses a button. He is then asked to enter a username and password in the dialog.
I have succeeded in adding the ENTER key to the TAB key for forward traversal.
The problem is that the ActionListener for a button in the dialog no longer responds to the ENTER button. Is there a way to make the ENTER key take care of both types of action?
In the following code (ripped from a larger program), I set the new forward traversal behavior in the main method at the bottom; the rest of the code sets up the dialog, shows the GUI, etc..
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.AWTKeyStroke; import java.awt.KeyboardFocusManager; import java.util.HashSet; import java.util.Set; import javax.swing.*; import javax.swing.event.*; public class Test extends JFrame { // variables // boolean change = false; char[] password = null; String username = null; // swing components // static JButton firstBtn; static JDialog loginDlg; static JFrame frame; static JPasswordField loginPF; JRadioButton loginRBtn; static JTextField loginTF; // colors // final static Color backgroundColor = new Color( 255, 255, 255 ); final static Color buttonPanelColor = new Color( 188, 210, 238 ); public void addComponentToPane( Container pane ) { firstBtn = new JButton( "Dialog" ); firstBtn.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { loginDlg.setLocationRelativeTo( frame ); loginDlg.setVisible( true ); } } ); JPanel mainBtnPnl = new JPanel(); mainBtnPnl.setLayout( new FlowLayout( FlowLayout.CENTER, 0, 20 ) ); mainBtnPnl.setMinimumSize( new Dimension( 400, 90 ) ); mainBtnPnl.setPreferredSize( new Dimension( 400, 90 ) ); mainBtnPnl.setMaximumSize( new Dimension( 400, 90 ) ); mainBtnPnl.setBackground( buttonPanelColor ); mainBtnPnl.setBorder( BorderFactory.createLineBorder( backgroundColor, 10 ) ); Box mainBtnBox = Box.createHorizontalBox(); mainBtnBox.add( firstBtn ); mainBtnPnl.add( mainBtnBox ); pane.add( mainBtnPnl, BorderLayout.PAGE_END ); // dialog // loginDlg = new JDialog( frame, "Log In", true ); loginDlg.setSize( 300, 225 ); loginDlg.setResizable( false ); loginDlg.getContentPane().setLayout( new BorderLayout() ); loginDlg.getContentPane().setBackground( backgroundColor ); JPanel loginCenterPnl = new JPanel(); loginCenterPnl.setMinimumSize( new Dimension( 300, 150 ) ); loginCenterPnl.setPreferredSize( new Dimension( 300, 150 ) ); loginCenterPnl.setMaximumSize( new Dimension( 300, 150 ) ); loginCenterPnl.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) ); loginCenterPnl.setBackground( backgroundColor ); loginCenterPnl.setLayout( new GridBagLayout() ); JLabel loginLbl1 = new JLabel( "Username:" ); loginTF = new JTextField( 10 ); JLabel loginLbl2 = new JLabel( "Password:" ); loginPF = new JPasswordField( 10 ); loginRBtn = new JRadioButton( "Change Password" ); GridBagConstraints loginGBC = new GridBagConstraints(); loginGBC.weightx = 1.0; loginGBC.weighty = 1.0; loginGBC.anchor = GridBagConstraints.LINE_START; loginGBC.gridx = 0; loginGBC.gridy = 0; loginCenterPnl.add( loginLbl1, loginGBC ); loginGBC.gridx = 1; loginGBC.gridy = 0; loginCenterPnl.add( loginTF, loginGBC ); loginGBC.gridx = 0; loginGBC.gridy = 1; loginCenterPnl.add( loginLbl2, loginGBC ); loginGBC.gridx = 1; loginGBC.gridy = 1; loginCenterPnl.add( loginPF, loginGBC ); loginGBC.anchor = GridBagConstraints.CENTER; loginGBC.insets = new Insets( 20, 0, 0, 0 ); loginGBC.gridwidth = 2; loginGBC.gridx = 0; loginGBC.gridy = 2; loginCenterPnl.add( loginRBtn, loginGBC ); JPanel loginSouthPnl = new JPanel(); loginSouthPnl.setMinimumSize( new Dimension( 300, 65 ) ); loginSouthPnl.setPreferredSize( new Dimension( 300, 65 ) ); loginSouthPnl.setMaximumSize( new Dimension( 300, 65 ) ); loginSouthPnl.setLayout( new FlowLayout( FlowLayout.CENTER, 10, 10 ) ); loginSouthPnl.setBorder( BorderFactory.createLineBorder( backgroundColor, 10 ) ); loginSouthPnl.setBackground( buttonPanelColor ); JButton loginBtn1 = new JButton( "Log In" ); loginBtn1.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { username = loginTF.getText(); password = loginPF.getPassword(); String passStr = new String ( password ); if ( loginRBtn.isSelected() ) change = true; System.out.println( "username = " + username + " passStr = " + passStr + " change = " + change ); } } ); //loginDlg.getRootPane().setDefaultButton( loginBtn1 ); JButton loginBtn2 = new JButton( "Close" ); loginBtn2.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { loginDlg.setVisible( false ); } } ); Box loginBox = Box.createHorizontalBox(); loginBox.add( loginBtn1 ); loginBox.add( Box.createRigidArea( new Dimension( 10, 0 ) ) ); loginBox.add( loginBtn2 ); loginSouthPnl.add( loginBox ); loginDlg.getContentPane().add( loginCenterPnl, BorderLayout.CENTER ); loginDlg.getContentPane().add( loginSouthPnl, BorderLayout.SOUTH ); } private static void createAndShowGUI() { frame = new JFrame( "Test" ); frame.setSize( 400, 400 ); frame.setResizable( false ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.getContentPane().setLayout( new BorderLayout() ); Test test = new Test(); test.addComponentToPane( frame.getContentPane() ); frame.setLocationByPlatform( true ); frame.setVisible( true ); } public static void main( String[] args ) { // this block doesn't work because of non-static methods // /*Set forwardKeys = getFocusTraversalKeys( KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS ); Set newForwardKeys = new HashSet( forwardKeys ); newForwardKeys.add( KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0 ) ); setFocusTraversalKeys( KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newForwardKeys );*/ // this block kind of works // Set<AWTKeyStroke> set = new HashSet<AWTKeyStroke>(KeyboardFocusManager .getCurrentKeyboardFocusManager().getDefaultFocusTraversalKeys( KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS)); set.add( KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0 ) ); KeyboardFocusManager.getCurrentKeyboardFocusManager().setDefaultFocusTraversalKeys( KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set); try { UIManager.setLookAndFeel( "javax.swing.plaf.metal.MetalLookAndFeel" ); } catch ( UnsupportedLookAndFeelException ex ) { ex.printStackTrace(); } catch ( IllegalAccessException ex ) { ex.printStackTrace(); } catch ( InstantiationException ex ) { ex.printStackTrace(); } catch ( ClassNotFoundException ex ) { ex.printStackTrace(); } UIManager.put( "swing.boldMetal", Boolean.FALSE ); javax.swing.SwingUtilities.invokeLater( new Runnable() { public void run() { createAndShowGUI(); } } ); } }
Similar Threads
-
custom jxtable header focus traversal
By AmitYatagiri in forum AWT / SwingReplies: 3Last Post: 08-10-2010, 08:37 PM -
Strange tab key focus traversal behavior in JTable
By javaexplorer in forum AWT / SwingReplies: 0Last Post: 05-19-2010, 12:59 PM -
Focus traversal keys not responding
By javaexplorer in forum AWT / SwingReplies: 1Last Post: 02-16-2010, 08:52 AM -
Focus Traversal Demo in SWT
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:37 PM -
JTable Focus Traversal
By helios_lie in forum AWT / SwingReplies: 1Last Post: 12-20-2007, 10:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks