0

I want to create a dialog from a JPanel, the problem I am encountering is that the Dialog class and the JOptionPane class which are the main dialog classes I read about use templates (an example of this is your "text" and then a cancel and a confirm button). I want to create a custom dialog that contains my JPanel. I Essentially want to display the entire JPanel in screen without making a new JFrame or reusing the existing one.

thanks!

  • If you don't want to use a top level container, you could look at using a CardLayout: https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html where your dialog is one of the cards, and you `show` that card when the dialog should be visible. – tgdavies Dec 22 '21 at 22:57
  • Create a factory class to meet your needs - JOptionPane is surprisingly flexible if you’re willing to dig into it – MadProgrammer Dec 22 '21 at 22:58
  • @MadProgrammer I tried to do this with JOptionPane but my JPanel doesn't fit in the dialog gui. even if I resize it it will be put in the bottom of the screen where you can't even see it. I guess what I want is something like the JFileChooser dialog. –  Dec 22 '21 at 23:11
  • We’d really need a [mcve] – MadProgrammer Dec 22 '21 at 23:18
  • A `JDialog` is a top-level container, just like a `JFrame`. You add one or more `JPanels` to the default `BorderLayout`. When you create your own `JDialog`, you are responsible for creating any `JButtons` that you need, – Gilbert Le Blanc Dec 23 '21 at 09:05

1 Answers1

0

For most part JOptionPane is extremely flexible, for example, starting with something as simple as...

import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class TestPane extends JPanel {

    public TestPane() {
        setLayout(new GridBagLayout());
        setBorder(new EmptyBorder(32, 32, 32, 32));
        JLabel label = new JLabel("Hello world");
        label.setFont(label.getFont().deriveFont(32f));
        add(label);
    }

}

I can do...

enter image description here

JOptionPane.showMessageDialog(null, new TestPane());

Ah, but wait, you probably don't want the icon, so instead you can do something like...

enter image description here

JOptionPane.showMessageDialog(null, new TestPane(), "Hello", JOptionPane.PLAIN_MESSAGE);

... what do you mean, you want custom options?! Well, okay, JOptionPane can handle that as well...

enter image description here

String[] options = new String[]{
    "options",
    "your",
    "are",
    "These"
};

int result = JOptionPane.showOptionDialog(null, new TestPane(), "Hello", JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[3]);
if (result >= 0) {
    System.out.println(options[result]);
}

Now, with a little bit of thought and effort, you could also:

JOptionPane is a good option, but it's not the only option you have. You could roll your own "factory" (like JOptionPane) that did all the heavy lifting of building a JDialog, generating the buttons/options and handling user input (on those actions)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366