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

  dialog.setVisible(true); 

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);
dialog.setVisible(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.

Summary

  • 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 Contents

  1. Getting started with Java
  2. Introducing Java - Swing Objects
  3. Writing Java Code - Methods
  4. Command Line Programs
  5. The Java User Interface - More Swing
  6. Working With Class
  7. Class Inheritance
  8. Building a Java GUI - Containers

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.

 

blog comments powered by Disqus

 

 



Last Updated ( Wednesday, 07 November 2012 )
 
 

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