Home arrow Java arrow Graphical User Interface
JAVA

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

Graphical User Interface
(Page 1 of 8 )

If someone mentioned to you the word Excel, you probably conjure the image of an Excel spreadsheet that consists of columns, rows, menus, buttons, and assorted components. You might say that this image is the face of the Excel program because it is the portion of the program that you see. Programmers call that image a graphical user interface (GUI) because, collectively, those components are used to interact with the program. In this chapter, you’ll learn how to use in your own program the push buttons, radio buttons, text boxes, and other components found in commercial programs.

What Is a User Interface?

An interface is a way to interact with something. For example, your television remote control is an interface to your television. Throughout this book, you learned how to use the Application Program(ming) Interface (API) to write code that interacts with a computer. A user interface is the way someone interacts with a program.

The simplest user interface consists of two components: a prompt displayed on the screen and the keyboard used to enter information into the program. The prompt might be some text that tells the user of the program to enter a student ID. The user then enters the student ID into the program using the keyboard. The program then processes the student ID when the ENTER key is pressed.

The following example illustrates how to create this simple user interface. System.in is used to reference the standard input to the computer, which by default is the keyboard. The person running your program could redirect the standard input to another input device, such as a file.

This example uses an InputStreamReader object and a BufferedReader object to improve reading information from the keyboard. Information entered by the user into the keyboard is stored as a series of bytes. However, our example needs a sequence of characters. The InputStreamReader converts the bytes into characters. We want to read information from the keyboard quickly. To do so, we want to read characters from memory (called a buffer) rather than directly from the keyboard. However, the InputStreamReader doesn’t use a buffer. Therefore, we need to use the BufferedReader, which reads characters from the InputStreamReader and saves them to a buffer. We then called the readLine() method defined in the BufferedReader class to read one full line of text from the buffer.

This example illustrates a very simple user interface. It displays a prompt on the screen and then calls the readLine() method. The readLine() method returns all the characters that were entered into the keyboard, which are then collectively assigned to the String studentID and displayed on the screen.

import java.io.*;
public class Demo {
  public static void main(String args[]) {
     BufferedReader stdin = new BufferedReader(
           new InputStreamReader(System.in));
    
try {
       
System.out.print("Enter your student ID: ");
       
String studentID = stdin.readLine();
       
System.out.println( "Student ID: " + studentID);
    
} catch (IOException e) {
        System.out.println( "Exception:" + e );
   }
  }
}

What Is a GUI?

No doubt you’ve heard the term GUI used whenever anyone talks about a user interface for a program. GUI is an acronym for graphical user interface. Practically every program used today uses graphics as a way for a user to interact with the program because a GUI is an intuitive and efficient way to collect information from a user and to display information for a user to read.

Researchers at Xerox’s Palo Alto Research Center (PARC) are credited with developing the GUI, which was later enhanced by Apple Computer and then by Microsoft. At the heart of a GUI are the standard graphical elements that collectively form the user interface. These elements are commonly recognized as windows, menus, push buttons, labels, text boxes, radio buttons, and other similar GUI objects that you see used in nearly all commercial programs today.

Besides making a program look pretty, the standard GUI dramatically reduces the time necessary for someone to learn how to use a new program. Arguably, 75 percent of every program is the same. That is, it uses the same two-dozen or so graphical elements. Each of those graphical elements operates identically across programs. For example, nearly everyone who has used a computer program knows to select the down arrow to the right of a text box in order to see a list of values that can be entered into the text box. The values are likely to change from program to program, but the process used to select those values is the same in all programs.

You have two ways in which you can create a GUI for your program. First is the simple approach of using a message dialog box and an input dialog box. A dialog box is a small window. You’ve seen a dialog box when you select File | Open from the menu bar—the Open dialog box is displayed.

A message dialog box is a dialog box that displays a message and an OK push button, which is what you see whenever a warning message is displayed on the screen. An input dialog box is similar to a message dialog box, except you can enter information into an input dialog box.

Both the message dialog box and the input dialog box are displayed by calling one method, which you’ll learn how to do in the next section. This is a quick-and-dirty way to create a GUI for your program. However, these are very limiting because you won’t be able to use all the GUI elements in these dialog boxes.

The second way to create a GUI for your program is to use radio buttons, push buttons, and other GUI objects that you see in most programs. Most of this chapter shows you how to create a GUI object for your program.


blog comments powered by Disqus
JAVA ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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