sassandsparkles
sassandsparkles

Reputation: 51

How to stop Java Swing JOptionPane OK_CANCEL_OPTION from closing on OK option?

I am using the JOptionPane OK_CANCEL_OPTION to submit data and (for now ) print it out. However, I would like the GUI to stay open and allow for multiple submissions. But, right now the GUI shuts down after the "OK" option is clicked and I have to rerun the program to submit different data. Is there a way to keep the GUI open for multiple submissions?

import java.awt.GridLayout; 
import javax.swing.*;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

class Display {

    public static void main(String[] args) {
    JTextField symbol = new JTextField();
    JTextField contractEx = new JTextField();
    JTextField lots = new JTextField();
    JTextField price = new JTextField();
    String[] items = {"Buy", "Sell"};
    JComboBox<String> combo = new JComboBox<String>(items);
    JTextField trader = new JTextField();
    JPanel panel = new JPanel(new GridLayout(0, 1));
    panel.add(new JLabel("Symbol (e.g. HH):"));
    panel.add(symbol);
    panel.add(new JLabel("Contract Expiry (e.g. JUL 16):"));
    panel.add(contractEx);
    panel.add(new JLabel("Lots:"));
    panel.add(lots);
    panel.add(new JLabel("Price:"));
    panel.add(price);
    panel.add(combo);
    panel.add(new JLabel("Trader"));
    panel.add(trader);

    int result = JOptionPane.showConfirmDialog(null, panel, "Trade Capture   System",
    JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    if (result == JOptionPane.OK_OPTION) {
        if(symbol.getText().equals("") || contractEx.getText().equals("") || lots.getText().equals("") || price.getText().equals("") || trader.getText().equals("")){
            JOptionPane.showMessageDialog(panel, "Please enter data into all fields.");
    }
    else{  
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            System.out.println(dateFormat.format(cal.getTime())); //2014/08/06 16:00:22
            System.out.println(symbol.getText() + " "
                            + contractEx.getText() + " "
                            + lots.getText() + " "
                            + combo.getSelectedItem() + " "
                            + price.getText() + " "
                            + trader.getText());
    }
    } else {
         System.out.println("Cancelled");
   }
}
}

Thank you!!!

Upvotes: 1

Views: 816

Answers (2)

camickr
camickr

Reputation: 324088

Is there a way to keep the GUI open for multiple submissions?

Check out the section from the Swing tutorial on Stopping Automatic Dialog Closing for the solution.

You can decide whether it is easier to use this approach or create your own custom dialog.

If you use this approach then you might also want to look at the section on Customizing Button Text so you button says something other than "OK". So maybe you have buttons like "Submit", "Submit and Close", "Cancel".

Upvotes: 2

ktfjulien
ktfjulien

Reputation: 41

If the first input doesn't alter anything in the JOptionPane itself, you can just create the window again as soon as it closes.

if (result == JOptionPane.OK_OPTION) {
    int result = JOptionPane.showConfirmDialog(null, panel, "Trade Capture   System",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    if(symbol.getText().equals("") || contractEx.getText().equals("") || lots.getText().equals("") || price.getText().equals("") || trader.getText().equals("")){
        JOptionPane.showMessageDialog(panel, "Please enter data into all fields.");
}

Upvotes: 1

Related Questions