Search code examples
javaswingjoptionpane

Can't Automatically Select JOption Pane showInputDialog


So I have a JOptionPane like this in the very beginning of my main method:

JFrame frame = new JFrame("Input");
String progName = JOptionPane.showInputDialog(frame, "Name?");

However, before I can start typing, I need to manually go click on the pop up. Is there any way to make it so that as soon as I run the program, it will automatically "select" the pop up so that when I start typing it will just be in the text box. If this can't be done with a JOptionPane, I am OK with other alternatives, I just need to get a user inputted string with the above constraint in mind.


Solution

  • I have created a simple example in which the logic for asking the user's name is concentrated in a single method. This method is called at the very start of the application and every time you click on a button.

    This way, the user is forced to enter the data when the application starts and every time he/she wishes to change the entered value.

    public class Jf53136132 extends JFrame {
    
        private static final long serialVersionUID = -3336501835025139522L;
    
        private JPanel contentPane;
    
    
    
        public Jf53136132() {
            setTitle("Jf53136132");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(new BorderLayout(0, 0));
            setContentPane(contentPane);
    
            JPanel panel = new JPanel();
            contentPane.add(panel, BorderLayout.CENTER);
    
            JButton btnInvokeJoptionpane = new JButton("set some text on label");
            panel.add(btnInvokeJoptionpane);
    
            JLabel lblx = new JLabel("-x-");
            panel.add(lblx);
    
            getNewTextForLabel(lblx);
    
            btnInvokeJoptionpane.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    getNewTextForLabel(lblx);
                }
            });
        }
    
    
    
        private void getNewTextForLabel(JLabel label) {
            String inputText = JOptionPane.showInputDialog("Enter the text for the label");
            System.out.println("you entered <" + inputText + ">");
            if (inputText != null && !inputText.trim().isEmpty()) {
                label.setText(inputText);
            }
        }
    
    }
    

    Notice how the method getNewTextForLabel(...) is called as soon as the label is added to the content pane and at every click of the button.

    Also, as VGR correctly pointed out, it is a good practice to not run any Swing code inside the main application thread. You can have a look at the java tutorials for swing (here's a classic example).

    The following is some example code that runs the frame on a separate thread.

    public class Main {
    
        private static void createAndShowGUI() {
            Jf53136132 f = new Jf53136132();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setPreferredSize(new Dimension(640, 480));
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
    
    
        void execute() {
            SwingUtilities.invokeLater(new Runnable() {
    
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
    
    
        public static void main(String[] args) {
            new Main().execute();
        }
    
    }