3

I'm using JOptionPane.showOptionDialog to show a JDialog. I would like to know how:

  1. set the dimension of the dialog (for now I'm using setPreferredSize() method on the given panel but I know that such method shouldn't be used).
  2. make the showed dialog resizable.

My code looks like:

JPanel panel; //my JPanel built with dialog contents
int ret = JOptionPane.showOptionDialog(myFrame,
                    panel,
                        "titel",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    options,
                    options[1]);

I know that I could obtain the desired result building a JDialog this way:

JDialog dialog = new JDialog(panel);
dialog.setResizable(true);
dialog.setSize(800,600);
dialog.setVisible(true);

The problem with the last solution is that I can't get the return value.

EDIT: in response to @camickr observations:

Why do you need to set the preferred size? If you build the panel properly is should be displayed at its preferred size.

I'm not sure of having fully understood Swing on this point. The problem is, for example, that I'm displaying through a JDialog a ChartPanel built with JFreeChart. Now, I suppose that panel has it's own preferred size, but I want to see it bigger. How can I do that without explicitly use setPreferredSize()?

Read the JOptionPane API. Search for "Direct Use". It shows you how to directly access the dialog used by the option pane and you can

I read it but I can't find the right method to understand which button (Ok or Cancel) has been pressed on the JDialog.

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
  • `I read it but I can't find the right method to understand which button (Ok or Cancel) has been pressed on the JDialog.` for example http://stackoverflow.com/questions/7613577/java-how-do-i-prevent-windowclosing-from-actually-closing-the-window/7613768#7613768 – mKorbel Oct 01 '11 at 18:54

3 Answers3

6

This hack using a HierarchyListener to get access to the JOptionPane's also works:

http://blogs.oracle.com/scblog/entry/tip_making_joptionpane_dialog_resizable

// TIP: Make the JOptionPane resizable using the HierarchyListener
pane.addHierarchyListener(new HierarchyListener() {
    public void hierarchyChanged(HierarchyEvent e) {
        Window window = SwingUtilities.getWindowAncestor(pane);
        if (window instanceof Dialog) {
            Dialog dialog = (Dialog)window;
            if (!dialog.isResizable()) {
                dialog.setResizable(true);
            }
        }
    }
});
Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
prunge
  • 22,460
  • 3
  • 73
  • 80
2
  1. Why do you need to set the preferred size? If you build the panel properly is should be displayed at its preferred size.

  2. Read the JOptionPane API. Search for "Direct Use". It shows you how to directly access the dialog used by the option pane and you can

With you second approach why are you setting the size? Again just pack() the dialog and it will be displayed at the panels preferred size.

What do you mean you can't get the return value? You have access to any method of your custom panel. So you can just invoke the getXXX() method when you receive control again. Just make sure the dialog is modal and the code after the setVisible(true) will block until the dialog is closed.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I need to set the preferred size. For example I want to display a ChartPanel created with JFreeChart and I want to see it bigger than how displayed normally. how can I do that without explicitly use setPreferredSize? – Heisenbug Oct 01 '11 at 16:25
  • and with return value I mean the int returned and hold by ret variable that tells about which button has been pressed. Of course in the second case there isn't such possibility because there are no button and i have to implement my own dialog. – Heisenbug Oct 01 '11 at 16:31
  • I find it hard to believe that `JFreeChart` does not offer methodology to suggest a size for the graph, but that is a separate question. Also I don't understand *"Of course in the second case there isn't such possibility because there are no button and i have to implement my own dialog."* Which part of that is not possible? – Andrew Thompson Oct 01 '11 at 16:55
  • @AndrewThompson: I prefer use a standard dialog instead of implementing my own, for an estetic reason. The default available dialogs put the ok,cancel buttons in the right place, implementing a custom dialog force me to do layout operations. – Heisenbug Oct 01 '11 at 16:59
  • @AndrewThompson: probably JFreeChart offers the possibility of chosing the chart dimension, but if I want to make the dialog resizable I still have to play around with setPreferredSize, right? – Heisenbug Oct 01 '11 at 17:00
  • @Heisenbug Please note that my name has a space. It is best to copy/paste these things. Note also that you can add 'at'Andrew(1) & I will be notified. Abbreviate my name if you like, but please don't mangle it! 1) If I put '@' the site software would complain. – Andrew Thompson Oct 01 '11 at 17:04
  • @Andrew Thompson: oh sorry. I used autocompletion and the space has been removed. – Heisenbug Oct 01 '11 at 17:06
  • *"if I want to make the dialog resizable I still have to play around with setPreferredSize, right?"* Umm.. no. Whether a TLC is resizable has piss-all to do with (preferred/max/min) size and more to do with `setResizable(boolean)`. The resizable nature of components inside TLCs has a lot to do with the layouts used. I think we are getting to the stage of 'post an SSCCE of your best effort, and I'll see what I can do'. I'd recommend pursuing the `JOptionPane` as per point 2 above. – Andrew Thompson Oct 01 '11 at 17:21
  • @AndrewThompson: yep auto completion will concatenate your name and mine by removing the the spaces. It's something we will have to endure. – Hovercraft Full Of Eels Oct 01 '11 at 18:35
  • @Hov This version of auto-completion bites, IMO. – Andrew Thompson Oct 01 '11 at 18:38
  • @And: but 1) it saves valuable keystrokes, and I'm always for that, and 2) I now have a better chance of getting replies sent to me that are flagged to me. If they send it to `@eels` or misspell it some other way, I'm hosed. – Hovercraft Full Of Eels Oct 01 '11 at 18:41
  • @Hov I'll stop commenting on it, but I won't start liking it. :( – Andrew Thompson Oct 01 '11 at 19:03
0

If you want to go the second way completely, you have to create and position your own "YES" and "NO" buttons somewhere (since a raw JDialog is just an empty modable frame). Therefore, you need to attach a MouseListener to both buttons and handle click events. On a click, you will know what button was pressed, and you'll just have to call dispose() on the dialog to close it.

Aurelien Ribon
  • 7,548
  • 3
  • 43
  • 54