WHOOHOO! I think i figured it out.
Like i said in my original post, i was missing something.
Let me explain...(to solidify the topics in my head and also to help someone else who reads this thead later...) Here's what i think is/was going on. Someone please correct me if i'm completely off my rocker...
- First off, i didn't understand the difference between the window having focus and the component having focus
- Secondly, I was trying to get the focus before the component was realized (before the event dispatch thread was started.
I first did some more debugging and found that when the appliacation is initially started up, the parent
ApplicationFrame (a
Frame subclass) had the focus. I wondered why that didn't automatically translate into my component having focus since there was only one component in the frame.
I then went poking around the javadoc some more and found a link to the
How to use the Focus Subsystem page in the Swing tutorial (
http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html). On this page, it says:
A component generally gains the focus by the user clicking it, tabbing between components, or otherwise interacting with a component. A component can also be given the focus programmatically, such as when its containing frame or dialog is made visible. This code snippet shows how to give a particular component the focus every time the window is activated:
1 2 3 4 5 6
| frame.addWindowListener(new WindowAdapter() { public void windowActivated(WindowEvent e) { textField.requestFocusInWindow(); } }); |
If you want to ensure that a particular component gains the focus the first time window is activated, you can call requestFocusInWindow on the component after the component has been realized, but before the frame is displayed. Here is some sample code showing how this can be done:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| JFrame frame = new JFrame("Test"); JPanel = new JPanel(new BorderLayout());
JButton button = new JButton("I'm first"); panel.add(button); frame.getContentPane().add(panel); frame.pack(); button.requestFocusInWindow(); frame.setVisible(true); |
From this, i was able to see the err in my ways.
the answer to the initial question i had (how to get the component to have focus when the app starts up) can be accomplished in either of these two ways. Although since i didn't specify that i wanted the component to always have the focus when the window gets the focus, the latter is more applicable.
So, in the last version of the KeyListenerTest code the following changes were made:
- removed the getFocus() method altogether and all calls to getFocus() as well.
- In the main() method, I added a call to pack(), as well as requestFocusInWindow() before i set the frame to visible:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public static void main ( String[] argv ) { final Frame f = new Frame ( "KeyListenerTest3 1" ); final KeyListenerTest3 klt = new KeyListenerTest3();
f.addWindowListener ( new WindowAdapter () { public void windowClosing ( WindowEvent e ) { f.dispose (); System.exit ( 0 ); } } ); f.add ( klt ); f.pack();
f.setSize ( 220, 220 );
klt.requestFocusInWindow();
f.setVisible ( true ); } |
The full listing follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| import java.awt.*; import java.awt.event.*; import javax.swing.*; public class KeyListenerTest3 extends JPanel implements Runnable { private Thread m_subThread = null; public static void main ( String[] argv ) { final Frame f = new Frame ( "KeyListenerTest3 1" ); final KeyListenerTest3 klt = new KeyListenerTest3();
f.addWindowListener ( new WindowAdapter () { public void windowClosing ( WindowEvent e ) { f.dispose (); System.exit ( 0 ); } } ); f.add ( klt ); f.pack();
f.setSize ( 220, 220 );
klt.requestFocusInWindow();
f.setVisible ( true ); }
public void run () { Thread.currentThread ().setPriority ( Thread.MIN_PRIORITY ); while ( true ) { try { Thread.sleep ( 500 ); } catch ( Exception e ) { } repaint (); } } public KeyListenerTest3 () { setBackground ( Color.white ); setLayout ( new BorderLayout () ); addKeyListener ( new KeyListener () { public void keyPressed ( KeyEvent ke ) { System.out.println ( "Key Pressed: " + ke.getKeyChar () ); } public void keyReleased ( KeyEvent ke ) { System.out.println ( "Key Released: " + ke.getKeyChar () ); } public void keyTyped ( KeyEvent ke ) { System.out.println ( "Key Typed: " + ke.getKeyChar () ); } } ); m_subThread = new Thread ( this ); m_subThread.start (); }
public Dimension getMinimumSize() { return new Dimension ( 220, 200 ); } public void paintComponent ( Graphics g ) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint ( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); g2.setPaint ( Color.orange ); g2.fillOval ( 100, 100, 100, 100 ); } } |
This solution seems more right to me. I hope i'm correct.

Thanks.
...alex...