I have a very simple problem, but I can't seem to find a solution.
I have a textfield. I want the cursor to be in the textfield (so the user doesn't have to click on the textfield to enter text).
This has to be doable.
Printable View
I have a very simple problem, but I can't seem to find a solution.
I have a textfield. I want the cursor to be in the textfield (so the user doesn't have to click on the textfield to enter text).
This has to be doable.
dbCode:requestFocusInWindow()
Thanks for your help, Darryl. I still have not gotten it to work with your suggestion, but I think the problem lies with the fact that the component in question is in a JPanel (that itself might not have focus).
I'll let you knwo when it is working.
O.K. I have code that does what I want it to, but I'd like to make sure that I understand why it works.
<code>
import java.awt.*; import java.awt.event.*;
import javax.swing.*; import javax.swing.event.*;
class Test extends JFrame {
String problemType = "Focus Issue";
Test() {
JFrame frame = new JFrame( problemType );
frame.setSize( 200, 100 );
frame.getContentPane().setLayout( new FlowLayout() );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
JTextField textField = new JTextField( 5 );
textField.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent event ) {
}
});
final JButton button = new JButton( "Button" );
button.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent event ) {
}
});
//frame.getContentPane().add( textField );
//frame.getContentPane().add( button );
JPanel panel = new JPanel();
panel.setLayout( new FlowLayout() );
panel.add( textField );
panel.add( button );
frame.getContentPane().add( panel );
//button.requestFocusInWindow();
frame.addWindowFocusListener(new WindowAdapter() {
public void windowGainedFocus(WindowEvent e) {
button.requestFocusInWindow();
}
});
frame.setVisible(true);
}
public static void main( String[] args ) {
SwingUtilities.invokeLater( new Runnable() {
public void run() { new Test(); }
});
}
}
</code>
1. If I leave out the block that adds the focus listener to the frame, focus is not given to the button. Is this because the frame is not conscious of focus until it is told to look for it?
2. Is the reason I can't have the panel deal with focus is because it doesn't inherit from Window (where addWindowFocusListener() is defined)?
Any clarity would be greatly appreciated.
The key is that you can't set focus on a component until the GUI is visible. The simple solution is:
Also, use the square brackets ([])to wrap the code tags.Code:frame.setVisible(true);
button.requestFocusInWindow();
Thanks for the clarification, Camickr. That makes sense.
I can't get over the fact that my solution seems to be a total hack. I REALLY don't understand how this focus stuff works. I have read and reread the tutorial at the Oracle website, and it is not too helpful. There is also essentially no discussion of focus in the "Core Java" books.
Can someone recommend a resource that will explain this area?