Building a Java GUI - Containers
Building a Java GUI - Containers
Written by Ian Elliot   
Wednesday, 07 November 2012
Article Index
Building a Java GUI - Containers
Using Swing GUI Builder
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 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. Getting started with Java

    In chapter 1 we tell you how to get started with modern Java development in the shortest possible time. The approach uses NetBeans and Swing and all of the resources used are free to download and use.

  2. Introducing Java - Swing Objects
    In the second chapter of our beginner's guide to Modern Java we find out more about objects by exploring the Swing framework with a simple hands-on example.

  3. Writing Code

    Using ifs and loops is one of the most difficult parts of learning how to program. Our beginners introduction to Java reaches the part all programmers have know and know well - how to write code.

  4. Command Line Programs
    Command line programming means doing things in the simplest possible way. We take a careful look at how data types and code build a program.

  5. User Interface - More Swing
    Finding out how to create a User Interface (UI) using the Java Swing library is not only a useful skill, it also is an ideal way to learn about objects and to make sure that the ideas really have sunk in.

  6. Working With Class
    The Swing components have provided an easy approach to the idea of objects, but there comes a time when you have to find out how to create your own. In this part of Modern Java, we look at the standard ideas of object-oriented programming.

  7. Java Class Inheritance
    Working with classes and objects is a very sophisticated approach to programming. You can't expect to absorb all of its implications in one go. We have already looked at the basics of class and objects. Now we need to look at encapsulation, constructors, overloading and inheritance.

  8. Java Data Types - Numeric Data 
    After looking at some of the advanced ideas of classes and objects we need to return to some simpler topics to make our understanding complete. We need to look more closely at data and, to get things moving, numeric data. 

  9. Java Data Types - Arrays And Strings
  10. Building a Java GUI - Containers
    In this chapter we get to grips with the idea of a container that is used to host components to build a user interface. We also find out how the Swing GUI Builder generates code to make it all much easier. 
  11. Advanced OOP - Type, Casting, Packages
  12. Value And Reference 
  13. Java Lambdas, SAMs And Events



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






or email your comment to:



Last Updated ( Thursday, 09 June 2016 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.