By Tony Bevis on Friday, 20 October 2023
Category: Java

Java programming course: 14.16 JDialog

JDialog

The JDialog class displays a separate window which is "owned" by an application's frame. You can either add your own components to a dialog or use a simple pre-built one through the JOptionPane class. There follows an ActionListener inner class that builds and displays a JDialog consisting of a label, text field and button.

You will need to import java.awt.event

The JDialog constructor used above has three arguments passed into it:


The dialog is set to use a FlowLayout and then three components are added to it. The size of the dialog is packed, and its display location is set to be relative to the owning frame.

Inside buildUI() create a button that listens for a click by instantiating the above inner class:

If you run the application, you should initially just see the button:

Now click the Show dialog button to show the dialog window:

The dialog does not respond to the OK button in this simple example, but you can close the dialog by clicking the window's close button:

You are now returned to the frame.

Next lesson: 14.17 JOptionPane

Related Posts

Leave Comments