Search code examples
javamathinputjoptionpaneoperations

Java JOptionPane Multiple Inputs and Arithmetic Operations In a Single Output


I've started taking a class about Java and just have learned about JOptionPane and we did some arithmetic operations with the inputs given by the user in an input dialog window but different dialog boxes for each input and then do the operations and show to the user the output in a message box.

What I'd like to know is, how to get all the inputs from a user in just one box and show the output to the user in a message box?

Here is the code of the example we've worked on.

    double ap = 0.3;
    double op = 0.2;
    double fp = 0.5;


    String a = JOptionPane.showInputDialog(null,"Input A:");
    String b = JOptionPane.showInputDialog(null,"Input B:");
    String c = JOptionPane.showInputDialog(null,"Input C:");
    String d = JOptionPane.showInputDialog(null,"Input D:");

    double aValue =  (Double.parseDouble(a)*ap);
    double bcValue = (((Double.parseDouble(b)/2(Double.parseDouble(c)/2))*op);
    double fValue = (Double.parseDouble(d)*fp);

    double res = aValue+bcValue+fValue;


    JOptionPane.showMessageDialog(null,"Result : " +res);

Solution

  • Well...if you want to have several inputs in one dialog then you can use the JOptionPane.showConfirmDialog() method and supply it with a custom JPanel (some examples here). In other words, you create a custom dialog.

    In your case here is one way you can do it:

    String dialogMessage = "Please suppliy digits to the following<br>input boxes:";
    int btns = JOptionPane.OK_CANCEL_OPTION;
    BorderLayout layout = new BorderLayout();
    JPanel panel = new JPanel(layout);
    JLabel label = new JLabel("<html>" + dialogMessage + "<br><br><br></html>");
    panel.add(label, BorderLayout.NORTH);
    
    JPanel p = new JPanel(new BorderLayout(5, 5));
    JPanel labels = new JPanel(new GridLayout(0, 1, 2, 2));
    labels.add(new JLabel("Input A", SwingConstants.RIGHT));
    labels.add(new JLabel("Input B", SwingConstants.RIGHT));
    labels.add(new JLabel("Input C", SwingConstants.RIGHT));
    labels.add(new JLabel("Input D", SwingConstants.RIGHT));
    p.add(labels, BorderLayout.WEST);
    
    JPanel controls = new JPanel(new GridLayout(0, 1, 2, 2));
    JTextField inputA = new JTextField();
    controls.add(inputA);
    JTextField inputB = new JTextField();
    controls.add(inputB);
    JTextField inputC = new JTextField();
    controls.add(inputC);
    JTextField inputD = new JTextField();
    controls.add(inputD);
    p.add(controls, BorderLayout.CENTER);
    panel.add(p);
    
    // Get Input from User...
    int res = JOptionPane.showConfirmDialog(null, panel, "My Dialog Title", btns);
    if (res == JOptionPane.OK_OPTION) {
        System.out.println("Input A is: " + inputA.getText());
        System.out.println("Input B is: " + inputB.getText());
        System.out.println("Input C is: " + inputC.getText());
        System.out.println("Input D is: " + inputD.getText());
    }
    

    You can modify this concept to suit your needs.