Hi,
 
I want enable TAB/SHIFT-TAB in a JTextArea to go to the next JTextArea and not print a tab character in the JTextArea. This can be easily done with setFocusTraversalKeys(). But I found something strange. Below is a testprogram with one JButton and 2 JTextArea's. When the program is started, focus is on the JButton. Press TAB and the focus will go to the first JTextArea and immediately to the second JTextArea. If you place the focus with the mouse on the first JTextArea and press tab, it goes to the second as it should.
 
Can someone explain this behaviour?
 
Below is the code of my testprogram...
 
public class FocusTest extends JFrame
{
 public static void main( String[] args )
 {
  FocusTest test = new FocusTest();
  test.pack();
  test.setVisible(true);
 }
 
 public FocusTest() throws HeadlessException
 {
  super("Focus Test");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
  setContentPane( createContent() );
 
 }
 
 private JPanel createContent()
 {
  JPanel panel = new JPanel();
  panel.setBorder( BorderFactory.createEmptyBorder(12,12,12,12));
  panel.setLayout( new BoxLayout(panel, BoxLayout.Y_AXIS));
  JButton button = new JButton("Test button");
  panel.add(button);
 
  JTextArea textArea1 = new MyTextArea();
  panel.add( textArea1 );
 
  panel.add( Box.createVerticalStrut(7));
 
  JTextArea textArea2 = new MyTextArea();
  panel.add( textArea2 );
 
  return panel;
 }
 
 private class MyTextArea extends JTextArea
 {
  public MyTextArea()
  {
   super(10, 50);
   Set forwardFocusTraversalKeys = new HashSet();
   forwardFocusTraversalKeys.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, 0, true));
   setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forwardFocusTraversalKeys);
 
   Set backwardFocusTraversalKeys = new HashSet();
   backwardFocusTraversalKeys.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK, true));
   setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backwardFocusTraversalKeys);
  }
 }
 
}


 
Ing. Wim Deblauwe
Software Development Engineer
 
BarcoView - Medical Imaging Systems
Theodoor Sevenslaan 106
B-8500 Kortrijk, Belgium
Tel. +32 56 233 985 Fax +32 56 233 457
www.barco.com 
[EMAIL PROTECTED]

 

- - - - - - - - DISCLAIMER - - - - - - - -

Unless indicated otherwise, the information contained in this message is privileged and confidential, and is intended only for the use of the addressee(s) named above and others who have been specifically authorized to receive it. If you are not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this message and/or attachments is strictly prohibited. The company accepts no liability for any damage caused by any virus transmitted by this email. Furthermore, the company does not warrant a proper and complete transmission of this information, nor does it accept liability for any delays. If you have received this message in error, please contact the sender and delete the message. Thank you.

Reply via email to