Search code examples
javajoptionpane

Creating Conditional States with JOptionPane Buttons


Is there a way that I could create separate conditional statements for the ok and cancel button in JOptionPane.showInputDialog()?
The reason I want to do this is because I don't want an empty string or a string just full of spaces to be set to s.
When I click ok with an empty text field or just click cancel, s would be set to null.
Is there a way that I can choose different options for the ok and cancel button to where if I were to click cancel, the window would close and if I were to click ok, the code would proceed with the conditional statements?

public boolean stringIsNullOrOnlyWhiteSpaces(String s)  {
   //checks if string is null or if string only contains white spaces
}


private void nameOfString() {
String s = JOptionPane.showInputDialog(this, "Enter String Name");

if (!stringIsNullOrOnlyWhiteSpaces(s)) {
   // do action    
  }

else { // if "ok" were to be clicked and not "cancel"
JOptionPane.showMessageDialog(this, "String cannot be empty");
nameOfString();
}
}

Solution

  • I doubt that there's a way where you can get just using JOptionPane, they're supposed just to catch a value, in the case of .showInputMessage() it's a string.

    The right way would be using JDialog and adding components with its logic inside it.

    I'm really bored at job so I did something that I think would work for you:

    public class CustomInput {
    
    private JButton btAccept;
    private JButton btCancel;
    private JLabel lbInput;
    private JTextField tfInput;
    private JPanel pnDialog;
    
    private JPanel createPanel() {
        java.awt.GridBagConstraints gridBagConstraints;
        pnDialog = new JPanel();
        lbInput = new JLabel();
        tfInput = new JTextField();
        btAccept = new JButton();
        btCancel = new JButton();
    
        pnDialog.setLayout(new java.awt.GridBagLayout());
    
        btAccept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAcceptAction(evt);
            }
        });
    
        btCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelAction(evt);
            }
        });
    
        lbInput.setText("Enter your input.");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        pnDialog.add(lbInput, gridBagConstraints);
    
        tfInput.setText("");
        tfInput.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        pnDialog.add(tfInput, gridBagConstraints);
    
        btAccept.setText("Accept");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        pnDialog.add(btAccept, gridBagConstraints);
    
        btCancel.setText("Cancel");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        pnDialog.add(btCancel, gridBagConstraints);
    
        return pnDialog;
        //Mommy someone is reading my code
    }
    
    private void btAcceptAction(java.awt.event.ActionEvent evt) {
        if (tfInput.getText().isEmpty()) {
            JOptionPane.showMessageDialog(pnDialog, "Input cannot be null", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void btCancelAction(java.awt.event.ActionEvent evt) {
        JDialog topFrame = (JDialog) SwingUtilities.getWindowAncestor(pnDialog);
        topFrame.dispose();
    }
    
    public static void main(String[] args) {
        JDialog dialog = new JDialog();
        CustomInput tp = new CustomInput();
        dialog.add(tp.createPanel());
        dialog.setSize(new Dimension(200,200));
        dialog.setVisible(true);
    }
    
    }