Home arrow Java arrow Page 7 - 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 - Getting Data from GUI Components
(Page 7 of 8 )

The purpose of using GUI elements is to make it easy for the user to enter information into your program. So far in this chapter, you learned how to create a window and display GUI elements in the window. Now we値l turn our attention to gathering information from those GUI elements.

Each time a person interacts with a GUI element, they cause an event to occur. Your program must have two features in order to react to an event: an event listener and a handler method, also called an event-handling method.

Think of an event listener as a part of your program that monitors the GUI looking for a particular event to occur, such as when the user clicks a push button. When the event happens, the listener detects the event and then calls an event handler. An event handler is a method that reacts to a specific event, such as the selection of a particular push button.

Suppose that you have two text areas and a push button on the window. A person can select all or a portion of the text appearing in the first text area and then click the push button to copy the text to the second text area. You値l see how this works a little later in this chapter. For now, we値l simply explain how this is done.

The program uses a listener to monitor events that happen with the push button. When the user clicks the push button, the listener recognizes the event and then calls the event handler that is associated with the push button. The event handler is a method that contains statements that read the selected text in the first text area and then places a copy of the text into the second text area.

An event listener is an interface that your program must implement. Actually, there are many event listeners, each designed to listen for particular GUI events. Table 12-4 contains commonly used event listeners.

 

Event Listener

GUI Element

ActionListener

Buttons

ItemListener

Check boxes, radio buttons, and combo boxes

KeyListener

Keyboard input

MouseListener

Mouse actions

MouseMotionListener

Mouse movements

Table 12-4  Commonly Used Event Listeners

You need to perform two steps in order to use an event listener. First, you値l need to implement the appropriate event listener interface. You値l see how this is done in examples throughout the rest of this chapter.

Second, you need to associate the listener with a GUI element. Let痴 say you created a push button using the following statement:

JButton bt = new JButton("Copy");

A push button GUI element requires an ActionListener interface, so you値l need to implement that ActionListener interface in the class that creates the push button. You associate the push button to the ActionListener interface by calling the addActionListener() method that is defined by the JButton class. The addActionListener() method requires that you pass it the this keyword to reference the action event. This is illustrated in the following statement:

bt.addActionListener(this);

The ActionListener detects when the push button is selected by the user and then calls the actionPerformed() method. The actionPerformed() method is a method you define in your program that reacts to the event. The actionPerformed() method requires one argument預 reference to the event, as shown here:

actionPerformed(actionEvent event)

Statements within the method can use the event to determine the proper course of action to take.

Reading a Push Button

As mentioned in the previous section, you値l need to implement the ActionListener in order for your program to respond to the push button GUI element. The following example shows how to react to the selection of a push button. This example displays two text areas and one push button called Copy.

As mentioned in the previous section, you値l need to implement the in order for your program to respond to the push button GUI element. The following example shows how to react to the selection of a push button. This example displays two text areas and one push button called Copy.

The addActionListener() method is called for the push button to associate the push button with the action listener. If you fail to call the addActionListener() method, your program won稚 be able to react to the selection of a push button.

The push button and the text areas are then added to the content pane through a call to the add() method; then the content pane is placed in the container through a call to the setContentPane() method.

The actionPerformed() method is defined next. Statements within this method are executed when the user clicks the push button. This example causes the highlighted text in the first text area to be copied to the second text area when the user clicks the push button. Figure 12-12 shows the window when this example is run.

 
Figure 12-12.  The second text area contains selected text copied
                      from the first text area when the Copy push button is
                      clicked.

The actionPerformed() method is called whenever any push button is clicked. Although this example shows one push button, typically you値l have multiple push buttons in the window of your application. You determine which push button was clicked by calling the event.getSource() method. The event.getSource() method returns the reference of the push button that was selected by the user. You can compare this reference using an if statement to reference each push button on the screen to determine which push button was clicked.

You can also determine the kind of action that was performed by calling the event.getActionCommand() method. Try displaying the action that is returned by the event.getActionCommand() method and see the kinds of actions that occur for the push button.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Demo {
    
public static void main(String[] args)
      {
          Window win = new Window();
     
}
}
class Window extends JFrame implements ActionListener {
  
JTextArea ta1 = new JTextArea("default text", 2, 25);
   JTextArea ta2 = new JTextArea(2, 25);
   JButton bt1 = new JButton("Copy");
   public Window ()
   {
     
super ("Hello world!");
      setSize(400,200);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      Container ca = getContentPane();
      ca.setBackground(Color.lightGray);
      FlowLayout flm = new FlowLayout();
      ca.setLayout(flm);
      bt1.addActionListener(this);
      ca.add(bt1);
      ca.add(ta1);
      ca.add(ta2);
      setContentPane(ca);
  
}
   public void actionPerformed(ActionEvent event) {
      ta2.setText(ta1.getSelectedText());
   }
}


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