temerariomalaga
temerariomalaga

Reputation: 87

How to do a custom JoptionPane

I need to do a JOptionPane (or something similar) with a JTextField and two RadioButtons but I don't know if that's possible. I have a main frame with differents options and when I click on "Operacion" I should call the dialog. How can I make that dialog?

Upvotes: 1

Views: 1873

Answers (2)

D-Klotz
D-Klotz

Reputation: 2073

Personally, I'm not a fan of using the JOptionPane for this. Go down the path of using JDialog.

It can be as simple as:

        JPanel innerPanel = new JPanel(new FlowLayout());
        // Add components and listeners here

        JDialog dialog = new JDialog();
        dialog.add(innerPanel);
        dialog.setModal(true);
        dialog.pack();
        dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        dialog.setVisible(true);
        dialog.addWindowListener(new WindowAdapter()
        {
            @Override
            public void windowClosing (WindowEvent e)
            {
                super.windowClosing(e);
            }
        });

Upvotes: 1

NESPowerGlove
NESPowerGlove

Reputation: 5496

A textfield and two radio buttons should be small enough to fit into a JOptionPane, so perhaps it's best to keep using that.

Add the JTextField with the two radio buttons to a JPanel, and add that JPanel as the component that's displayed in a JOptionPane. Probably want to use the option pane that displays just an "Ok", with one of the radio buttons already selected.

To be safe, you may want to wrap that JPanel in a JScrollPane because I don't think JOptionPanes are re-sizable and depending on if a user changes your look and feel through command line options or perhaps accessibility settings then you might cut off some GUI components from them.

Upvotes: 4

Related Questions