Building a Java GUI - Containers
Written by Mike James   
Article Index
Building a Java GUI - Containers
Adding A Button
Obtaining User Input

Communicating With A Dialog

In many cases a dialog box is used to get some data from the user. This raises the question of how to get the data back from a dialog box to the program that created it.

This is a surprisingly difficult question because it raised lots of advanced design ideas and programmers can argue about elegant ways of doing the same job. For simplicity I'm going to concentrate on getting data back from a modal dialog box in the simplest possible way. This at least gives you a chance to see if you understand some very basic object oriented ideas.

When you show a modal dialog box the program that used the


instruction is paused and it waits for the user to dismiss the dialog box. The key to getting data from the dialog box is to realize that the dialog box object doesn't vanish when the users closed the visible dialog box on the screen. What this means is that if you add a public property or better a "getter" method then the parent program can access the values stored in the dialog box.

For example, if you add the following public method to MyDialog

public String getResult(){    
   return jTextField3.getText();

Then in the program that calls it the value of the third text field can be retrieved using:

MyDialog dialog=new MyDialog(this,true);
String s=dialog.getResult();


This is one of the reasons why modal dialog boxes are so easy to use to gather data - the parent program pauses while the user fills in the data and the dialog box object isn't destroyed when the user finished with the on-screen dialog. This allows the parent program to gather the data via the dialog box's properties.

Notice that as the dialog object exists before it is made visible to the user the parent program can use setter methods to set values in the dialog in the same way before calling setVisible(true).

There are other more elegant ways of doing both jobs.

The Standard Dialogs

Before leaving this topic it is worth mentioning that fact that Java has lots of standard dialog boxes ready for you to use. It would be a waste of a lot of time to use the jDialog class to recreate any of these. For example there is a standard file open, color picker and a range of simple alert type dialogs. Look up jOptionPane., jFileChooser and jColorChooser.


  • Swing provides a number of container objects that you can use to host components such as buttons and text fields.
  • The two most used containers are the frame and the dialog although there are other simple components that act like containers.
  • The applet container is no longer used to host Java programs within a web page.
  • Containers and components are represented in Swing by classes and you create instances of these classed to create a user interface.
  • The drag-and-drop designer is an easier way to create the Java code needed to create a user interface.
  • The designer creates a custom jFrame or jDialog class complete with all the code needed to create and add the components that you have placed on it.
  • To use the code that the designer creates all you need to do is to create an instance of the class in the usual way.
  • You can use the designer to create event handlers and code these within the class definitions it autogenerates.
  • A dialog is a container that has to have a parent frame.
  • A modal dialog causes the parent frame's code to pause while the user interacts with it. As the dialog object still exists after the user has closed the on-screen dialog the code in the frame can access any public properties that the user sets.


Modern Java
With NetBeans And Swing



  1. Why Java?
  2. Getting started with Java
  3. Introducing Java - Swing Objects
  4. Writing Code
  5. Command Line Programs
  6. User Interface - More Swing
  7. Working With Class
  8. Java Class Inheritance
  9. Java Data Types - Numeric Data
  10. Java Data Types - Arrays And Strings
  11. Building a Java GUI - Containers
  12. Advanced OOP - Type, Casting, Packages
  13. Value And Reference 
  14. Java Lambdas, SAMs And Events


To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.





or email your comment to: