1

I have written a Swing application which has a main window.

Now I would like to display a configuration dialog before the main window is shown because depending on the output of the configuration dialog, the main window might have to be created differently.

I have read somewhere that a dialog should always have a parent window, however.

What is the proper way to create and display the configuration dialog under these circumstances? Should I make it a JDialog or a regular JFrame?

tajmahal
  • 1,665
  • 3
  • 16
  • 29
  • `Somewhere - over the rainbow ...`. Somewhere-questions should always be asked somewhere. How shall we know, what somebody had in mind? Whether somewhere is some context, in which that claim makes sense. A citation, where we can get our own impression, would be fine. Maybe you don't remember right, and somewhere was in a VB-tutorial? – user unknown May 30 '11 at 16:18

2 Answers2

4

I have read somewhere that a dialog should always have a parent window, however.

I'm not aware of any such limitation. Here's a simple counter-example.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
4

Use null to reference the parent window. This is an OK practice when there is no parent.

Then you are free to do something like this

String body = "Which will you choose?";
String title = "The Title";

int choice = JOptionPane.showConfirmDialog(null, body, title, JOptionPane.YES_NO_OPTION);
switch(choice) {
case JOptionPane.YES_OPTION:
    createAndDoWindowForYesOption(); break;
case JOptionPane.NO_OPTION:
    createAndDoWindowForNoOption(); break;
default:
    doNothing(); break;
}

Edit: I have used JOptionPane as an example only, as pointed out by @Mre this could be extended to JDialog if more functionality is required.

Sean
  • 7,597
  • 1
  • 24
  • 26
  • +1 This is simpler that `JDialog` and just as flexible, as discussed under [Direct Use](http://download.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html). – trashgod May 30 '11 at 16:07
  • 1
    +1, although I would use `JDialog` if the main window requires more robust functionality. – mre May 30 '11 at 16:11
  • If I needed a dialog with a custom layout, then I would extend `JDialog` and use `customDialog.setVisible(true)`, right? How would I get a return value back then? – tajmahal May 31 '11 at 06:46