0

I am somewhat new to UI Development in Java and Swing. I would like to open a dialog when I click an item in my JMenuBar and then validate that dialog before closing it (so pressing the X or the ok button will not work unless the users input meets certain conditions).

At the moment, my project is structured like this:

Window.java

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Window {
    private static JFrame f;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(Window::createAndShowGUI);
    }

    private static void createAndShowGUI() {
        f = new JFrame("Stackoverflow - Testproject");
        JMenuBar menubar = new JMenuBar();
        JMenu j_menu_test = new JMenu("Test");
        JMenuItem j_menuitem_clickme = new JMenuItem("Click me");
        j_menu_test.add(j_menuitem_clickme);
        j_menuitem_clickme.addActionListener(new Open_Dialog());
        menubar.add(j_menu_test);

        f.setJMenuBar(menubar);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);

    }

    static class Open_Dialog implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            DialogPanel dialog = new DialogPanel();
            int result = JOptionPane.showConfirmDialog(null, dialog, "Test", JOptionPane.OK_CANCEL_OPTION);
            if (result == JOptionPane.OK_OPTION) {
                //do something
            }
        }
    }
}

And the content of my dialog in DialogPanel.java

import javax.swing.*;

public class DialogPanel extends JPanel {
    private JTextField id_field = new JTextField(20);

    public DialogPanel() {
        add(new JLabel("Insert something to validate here:"));
        add(id_field);
    }
}

Now obviously this does not do any dialog validation yet. I have been trying to get that to work, and I have found a working example of dialog validation but I can't seem to integrate it/get the whole thing to run. If I take the example from the link, how do I call dialog? I think I am not supposed to use the JOptionPane but everytime I try to display a JDialog any other way I get an "Boxlayout cannot be shared" error. Please help me integrate this into my code.

krise
  • 485
  • 1
  • 10
  • 22

1 Answers1

2

First, you need to add getters to your DialogPanel class. If you wish to verify the information typed in the idField, you'll have to add code to this class by putting an ActionListener on the JtextField.

public class DialogPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    
    private JTextField idField = new JTextField(20);

    public DialogPanel() {
        add(new JLabel("Insert something to validate here:"));
        add(idField);
    }

    public JTextField getIdField() {
        return idField;
    }
    
    public String getIdFieldString() {
        return idField.getText();
    }
}

Then, your result would look like this.

static class Open_Dialog implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        DialogPanel dialog = new DialogPanel();
        int result = JOptionPane.showConfirmDialog(null, 
                dialog, "Test", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String string = dialog.getIdFieldString();
            // Do your processing
        }
    }
}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Thanks for the response, but if I understand correctly clicking the OK or Cancel Button when using the JOptionPane class will always result in the dialog closing. With your implementation, I get the string afterwards and I can do some checks on that, but the dialog will still be closed, won't it? – krise Nov 10 '20 at 09:53
  • @krise: You have to add an ActionListener and additional code to the DialogPanel class to verify the contents of the JTextField. You would have more control over the button actions if you create your own JDialog. – Gilbert Le Blanc Nov 10 '20 at 12:57