|
Make sure that my JTextField has the focus when a JFrame is created |
|
|
In a JFrame, use a small WindowAdapter to listen to the WindowOpened event. From there, simply request the focus for
the JTextField.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFrame extends JFrame {
JTextField field1;
JTextField field2;
JPanel panel;
public MyFrame() {
super( "This is my Frame" );
panel = new JPanel();
field1 = new JTextField( 10 );
field2 = new JTextField( 10 );
panel.add( new JLabel("Field 1:"));
panel.add( field1 );
panel.add( new JLabel("Field 2:"));
panel.add( field2 );
getContentPane().add( "Center", panel );
addWindowListener(
new WindowAdapter() {
public void windowOpened( WindowEvent e ){
field1.requestFocus();
}
}
);
pack();
setVisible( true );
}
}
|
To try it:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFrameApplet extends JApplet {
public void init() {
MyFrame myFrame = new MyFrame();
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.