Search code examples
javaswingjoptionpane

How to use JOptionPane to input 2 numbers for a formula?


I need to use JOptionPane to read in 2 user entered numbers then display those two numbers sums and products, I'm having trouble understanding how to make the boxes in swing for this program.


Solution

  • There are two ways to do this:

    1. Create a custom panel and add the panel to the JOptionPane
    2. Let the JOptionPane build the panel for you

    Here is an example of both:

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class OptionPanePanel
    {
        private static void createAndShowUI()
        {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo( null );
            frame.setVisible( true );
    
            //  Build a custom panel
    
            JPanel panel = new JPanel( new GridLayout(2, 2) );
            panel.setBackground(Color.RED);
            panel.add( new JLabel("First Name") );
            JTextField firstName = new JTextField(10);
    //      firstName.addAncestorListener( new RequestFocusListener(false) );
            panel.add( firstName );
            panel.add( new JLabel("Last Name") );
            JTextField lastName = new JTextField(10);
            panel.add( lastName );
    
            int result = JOptionPane.showConfirmDialog(
                frame, // use your JFrame here
                panel,
                "Use a Panel",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.PLAIN_MESSAGE);
    
            if(result == JOptionPane.YES_OPTION)
            {
                System.out.println(firstName.getText() + " : " + lastName.getText());
            }
            else
            {
                System.out.println("Canceled");
            }
    
            //  Let Option Pane build the panel for you
    
            JTextField firstName2 = new JTextField(10);
    //      firstName2.addAncestorListener( new RequestFocusListener() );
            Object[] msg = {"First Name:", firstName2, "Last Name:", lastName};
    
            result = JOptionPane.showConfirmDialog(
                frame,
                msg,
                "Use default layout",
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.PLAIN_MESSAGE);
    
            if (result == JOptionPane.YES_OPTION)
            {
                System.out.println(firstName.getText() + " : " + lastName.getText());
            }
            else
            {
                System.out.println("Canceled");
            }
        }
    
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowUI();
                }
            });
        }
    }
    

    Note:

    When you run the above code focus will be place on the button on the JOptionPane.

    If you want the focus to be on the text field then you need to use the Request Focus Listener (after you download the class) as demonstrated in the example.