30

What is the difference between a JFrame and a JDialog?

Why can't we use setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE); for a JDialog?

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Mahdi_Nine
  • 14,205
  • 26
  • 82
  • 117

4 Answers4

30

JFrame is a normal window with its normal buttons (optionally) and decorations. JDialog on the other side does not have a maximize and minimize buttons and usually are created with JOptionPane static methods, and are better fit to make them modal (they block other components until they are closed).

But both inherit from Window, so they share much functionality.

fortran
  • 74,053
  • 25
  • 135
  • 175
5

Why we can't use setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); for JDialog?

Sure you can.

Post your SSCCE that demonstrates the problem you are having when using this value.

However you can't use EXIT_ON_CLOSE for a JDialog because that value is not supported which makes sense since a JDialog is a "child" or "helper" window for your application which is represented by a JFrame. Closing a dialog should not close the application.

Nathan
  • 8,093
  • 8
  • 50
  • 76
camickr
  • 321,443
  • 19
  • 166
  • 288
1

There are some JDialog constructors with a owner parameter which can be a Frame, a Dialog or a Window. A non-null value also makes the JDialog stay above his owner. This is complementary of the modal behavior described by Fortran.

Nathan
  • 8,093
  • 8
  • 50
  • 76
Laurent Caillette
  • 1,281
  • 2
  • 14
  • 20
  • 1
    not an answer to the question, is it? Once you'll have enough rep, you'll be able to comment :-) – kleopatra Aug 12 '13 at 08:21
  • The question is about the differences, so I find very interesting that a JDialog has the capabilities to stay on top of its parent without having to be modal. – lqbweb Oct 24 '14 at 11:02
0

You can also use setModal(boolean t); This only works on JDialog. User must operate on JDialog not other window. If they wanna operate owner windows, they must shut down this JDialog.

Nathan
  • 8,093
  • 8
  • 50
  • 76
Ryan
  • 53
  • 6
  • I'd use the newer [ModalityType](http://docs.oracle.com/javase/8/docs/api/java/awt/Dialog.ModalityType.html) constructs in lieu of the boolean modal APIs -- finer grain control and clearer intent in your code. – gerardw Jul 21 '15 at 16:38