Home arrow Java arrow Page 2 - Graphical User Interface

Graphical User Interface

Graphical user interfaces allow end users to interact with applications in a more or less intuitive manner. This article explains how to use these components in your own Java programs. It is taken from chapter 12 of the book Java DeMYSTiFieD, written by Jim Keogh (McGraw-Hill/Osborne, 2004; ISBN: 0072254548).

Author Info:
By: McGraw-Hill/Osborne
Rating: 5 stars5 stars5 stars5 stars5 stars / 45
September 01, 2005
  1. · Graphical User Interface
  2. · A Simple GUI
  3. · Content Container
  4. · Grid Layout Manager and the Gridbag Layout Manager
  5. · Labels and Text Fields
  6. · Combo Boxes
  7. · Getting Data from GUI Components
  8. · Reading Radio Buttons and Check Boxes

print this article

Graphical User Interface - A Simple GUI
(Page 2 of 8 )

The easiest way to create a GUI for your program is to use a message dialog box and an input dialog box. A message dialog box is used to display a message on the screen, and an input dialog box prompts the user to enter information that is returned to your program for processing.

Let’s take a look at how to create a message dialog box. You create a message dialog box by calling the showMessageDialog() method, which is defined in the JOptionPane class contained in the javax.swing package. This means you’ll need to import the javax.swing package at the top of your program in order to call the showMessageDialog() method.

The showMessageDialog() method requires four arguments. The first argument is a reference to the parent that calls the showMessageDialog() method. Many programmers simply use null as the value for this argument because there isn’t a parent for this dialog box.

The second argument is the message you want displayed on the screen. The third argument is the caption that appears in the title bar of the dialog box, and the last argument is a constant that states the kind of message dialog box you want displayed. Table 12-1 contains a list of the most commonly used message constants. Each one displays an icon within the message dialog box that corresponds to the kind of message being displayed.





Used to display a general-purpose message


Used to display an error message


Used to display a general-purpose message


Used to display a warning message


Used to question the user regarding a user


response to your program

Table 12-1.  Constants Used to Determine the Kind of Message Dialog
                  Box to Display

The showMessageDialog() method also displays an OK push button that the user selects to acknowledge the message. The push button closes the dialog box when selected.

Here is how you call the showMessageDialog() method:

JOptionPane.showMessageDialog(null, "Message", "Window Caption",

Figure 12-1 shows the dialog box created by this method call.

Figure 12-1.   Here is a “plain” message dialog box. It is useful for 
                    displaying any kind of message on the screen.



An input dialog box is created by calling the showInputDialog() method, which is also defined in the JOptionPane class. The showInputDialog() method requires one argument, which is a message prompting the user to enter information into the dialog box.

The showInputDialog() method displays the message and a text box, which is where the user enters information. In addition, the OK and Cancel push button are displayed. Selecting OK causes the showInputDialog() method to return the information that the user entered into the text box. Selecting either push button closes the dialog box.

Information that the user enters into the text box is returned by the showInputDialog() method as a string, which is usually assigned to a String variable and then processed by the program. Here is how to call the showInputDialog():

String str = JOptionPane.showInputDialog("Enter Student ID: ");

Figure 12-2 shows the input dialog that is displayed when this method is called.

Figure 12-2.  The showInputDialog() method displays an input dialog 
                    box that is used to gather information from the user of your program.


The following example illustrates how to call both the showMessageDialog() method and the showInputDialog() method from within a program. This example begins by importing all the swing classes, which is necessary to call either dialog box method. Next, a string is declared and is later used to store information returned by the showInputDialog() method. The showMessageDialog() method is called first to display the message dialog box. Once the user acknowledges the message by selecting the OK push button, the message dialog box closes and the showInputDialog() method is called to display the input dialog box. The information returned by the showInputDialog() method is then assigned to the string.

import javax.swing.*;
public class Demo {
public static void main(String[] args) {
String str;
"Message", "Window Caption", JOptionPane. PLAIN_MESSAGE);
      str = JOptionPane.showInputDialog (
            "Enter Student ID ");


In the previous section, you learned how to use the JOptionPane class that’s contained in the javax.swing package. The javax.swing package contains a large collection of GUI classes that have become known as the Java Foundation Classes (JFC).The javax.swing package contains a richer collection of graphic classes than the Abstract Windows Toolkit (AWT) packages of classes. AWT and JFC are complementary. Programmers use both libraries to create a graphical interface to their programs.

The JFC contains a wealth of interesting classes you can incorporate into your program. In fact, the JFC contains so many classes, there aren’t enough pages in this book to explain each of them. Therefore, we’ll focus on the common classes used to create a GUI for a typical program. You can learn about the entire JFC at java.sun.com.

Let’s begin by covering the basics of a GUI. All GUIs require a window that can be minimized, maximized, and resized by the person using the computer. A window needs a content container that is used to contain push buttons, text boxes, and other GUI elements that comprise the GUI for your program. The content container appears inside the window.

Typically, programmers define their own window class that contains the definition for all the elements of their window, including the content container and GUI elements. An instance of the window class is then declared any time the programmer needs a window. Any changes to the window occur in one place—the window class.

The window class extends the JFrame class. The JFrame class is a JFC that defines all the methods necessary to manage the window, such as the methods for resizing it. This means that your window class has all the features found in the JFrame class.

Three commonly used methods are defined in the JFrame class. These are the setSize() method, the setDefaultCloseOperation() method, and the setVisible() method. The setSize() method requires two arguments: the integers that define the width and height of the window, measured in pixels. For example, the following method defines the size of the window to be 400 pixels wide and 100 pixels high:


The setDefaultCloseOperation() method defines how the window behaves when the user closes the window (for example, by clicking the “X” in the window’s upper-right corner). The setDefaultCloseOperation() method requires one argument: a constant that defines the behavior. Many programmers use the EXIT_ON_CLOSE constant (which causes the window to exit when the user selects Close), as shown here:


The window you create is not shown until you call the setVisible()method. The setVisible() method requires one argument: a Boolean true or false. A Boolean true causes the window to be displayed. A Boolean false causes the window to be hidden from view. Here’s the setVisible() method.


Typically, each window has a caption on the title bar that describes the purpose of the window. You create the window caption by calling the constructor of the JFrame class and passing it the caption. Because your window class extends the JFrame class, you can call the constructor by using super, as shown here:

super ("Window Title");

The follow example illustrates how to define your own window class and display the window on the screen. Figure 12-3 shows the window it displays.

Figure 12-3.   You create a window by defining your own window
                     class that extends the
JFrame class.

import javax.swing.*;
public class Demo {
public static void main(String[] args) {
Window win = new Window();
class Window extends JFrame {
public Window () {
super ("Window Title");

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials