0

I'm trying to create a custom dialog with input from the user, after each step(of the input) the information is verified, and if it's correct a new panel appears, and the user can continue adding info. To do this i'm using JOptionPane. I'll have three buttons "Ok", enabled only if the user went trough the whole input proces, "Next", to check if the inputted info is correct and show a new pane, and lastly "Cancel", to cacel. Here how it looks:

enter image description here

Right now if i click on any of the buttons, the dialog get closed, how should i change that?

My Code so far(most of it):

public MakeBookingForm()
{
    components = new ArrayList<>();

    this.title = "Make Booking";
    setMessageType(JOptionPane.PLAIN_MESSAGE);
    setRootPane(null);
    setOptions(new String[] { "OK", "Next", "Cancel" });
    setOptionSelection(0);

    JPanel roomNumPanel = new JPanel();
    lblRoomNum = new JLabel( "Enter the booking number" );
    roomNumPanel.add(lblRoomNum);
    txtFldRoomNum = new JTextField(20);
    roomNumPanel.add(txtFldRoomNum);
    this.addComponent(roomNumPanel);

}

public void show() //the function i use to show the form
{
    int optionType = JOptionPane.OK_CANCEL_OPTION;
    //JOptionPane.OK_OPTION
    Object optionSelection = null;

    if(options.length != 0)
    {
        optionSelection = options[optionIndex];
    }

    int selection = JOptionPane.showOptionDialog(rootPane,
            components.toArray(), title, optionType, messageType, null,
            options, optionSelection);

    System.out.println(selection);

    //return selection;
}
  • and your code is? – Youcef LAIDANI May 26 '17 at 20:03
  • *"My Code so far(most of it): "* For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 26 '17 at 20:28
  • Possible duplicate of [JOptionPane - check user input and prevent from closing until conditions are met](https://stackoverflow.com/questions/13055107/joptionpane-check-user-input-and-prevent-from-closing-until-conditions-are-met) – Catalina Island May 27 '17 at 17:41

2 Answers2

1

JOptionPane has its own concept of what the buttons should do, which you are setting via the OK_CANCEL_OPTION. All of the options result in the popup window closing. Technically, you could get the list of components dynamically, find the buttons that JOptionPane creates, remove the default handler, and then add your own... but that seems like a convoluted way of doing it (compared to not extending JOptionPane).

If you're set on using JOptionPane, I would recommend creating your own JButtons and adding them to the JPanel that you create. That will allow you to set callback behavior as you wish. Then tell JOptionPane not show any buttons. This can be done by setting the constructor's option argument to: new Object[]{} or calling: setOptions(new Object[]{});

Because there will be no default buttons showing, JOptionPane will automatically pack() itself, leaving only your pane visible, without the default buttons that have undesirable behaviour.

msanford
  • 11,803
  • 11
  • 66
  • 93
Tmar
  • 11
  • 1
  • 1
0

Because after showing your dialog you need to check int selection to know which one did user choose?

I'm trying to give some simple example to you :

private int selection;
private final String password = "12345";

public TestClass() throws MalformedURLException {

    Object[] options = {"Enter", "Quit", "Cancel"};
    URL url = new URL("http://education.oracle.com/education/images/wdpsub/java.png");
    ImageIcon icon = new ImageIcon(url);

    JPanel panel = new JPanel();
    panel.add(new JLabel("Welcome to my\nJOptionPane example"));

    selection = JOptionPane.showOptionDialog(null, panel, "Show me yout skills", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, icon, options, null);
    System.out.println(selection);//print to test which button returns wich value

    switch (selection) {
    case 2:
        JOptionPane.showMessageDialog(null, "Your job is cancelled");
        break;
    case 1:
        String answer = JOptionPane.showInputDialog("Are you sure?");
        if(answer.equalsIgnoreCase("YES")) {
            System.exit(0);
        }else {
            JOptionPane.showOptionDialog(null, panel, "Show me yout skills", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, icon, options, null);
        }

        break;  
    case 0:
        String keylog = JOptionPane.showInputDialog("Enter your password : ");
        if(keylog.equals(password)) {
            JOptionPane.showMessageDialog(null, "Hello user.");
        }
        break;  
    }
}

public static void main(String[] args) throws MalformedURLException {
    new TestClass();
}

Note : This code written just to explain the logic of this work.

I hope to this help you.

Coder ACJHP
  • 1,940
  • 1
  • 20
  • 34