We would like to know how to focus on JTextField in JDialog.
import java.awt.Dimension; import java.awt.event.ActionEvent; /*w ww .ja v a 2s . co m*/ import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class Main extends JFrame { static JFrame myFrame; public Main() { setDefaultCloseOperation(EXIT_ON_CLOSE); JButton openDialog = new JButton("Click here"); JPanel myPanel = new JPanel(); myPanel.add(new JButton(new AbstractAction("Click here") { @Override public void actionPerformed(ActionEvent e) { JDialog dialog = new JDialog(myFrame, true); JTextField myField = new JTextField(10); JPanel innerPanel = new JPanel(); innerPanel.add(myField); dialog.add(innerPanel); dialog.pack(); dialog.setSize(new Dimension(160, 120)); dialog.setLocationRelativeTo(myFrame); dialog.setVisible(true); } })); add(myPanel); pack(); setSize(new Dimension(320, 240)); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { myFrame = new Main(); } }