Graphical User Interfaces
Home - About Us
Introduction to GUI More GUI

More GUI Components

Two more useful GUI components are JCheckBox and JLabel.  Use a JLabel to present static text (images can also be associated with the text).  Often a JLabel is visually placed next to another component, such as a text field, to help the user identify the purpose of the component.  For example, if we wanted the user to enter their favorite color into a text field we might want to present this image:

This above window was created with the code shown below.   Note that this program uses the pack method instead of calling the setSize method.  The setSize method requires that we give (usually by guessing) the size of the window in pixels -- the pack method looks at all of the components that have been added to the window and sets the window to an appropriate width and height.

   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   
   public class DemoComponents
   {
      public static void main(String [] args)
      {
         MyFrame frame = new MyFrame("Demo Label");
         frame.pack();  // NOTE - pack in place of setSize
         frame.setLocation(100, 75);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
      }
   }
   
   class MyFrame extends JFrame
   {
      JLabel label = new JLabel("Enter favorite color:");
      JTextField color = new JTextField(12);
      
      public MyFrame(String s)
      {
         super(s);
         setLayout(new FlowLayout());
         
         add(label);
         add(color);
      }
   }

Another useful component is JCheckBox.  A check box has a built-in label, and is either checked or unchecked.  The state of the check box can be set by either the user, by checking or unchecking the box, or by the program.   The program can set the state via the setSelected method.

   JCheckBox creditCard = new JCheckBox("Credit card", false);  // starts off unchecked
   ...
   creditCard.setSelected(true);   // program makes sure the box is checked
   

When you instantiate a JCheckBox object you can either give just its label

	new JCheckBox("married")

or give its name and the initial state of the check box

	new JCheckBox("married", true)

The current state of a check box can be determined via the getSelected method, which returns either true or false:

   if ( creditCard.isSelected() == true )
      ...

JTextField ActionEvents

JTextField objects generate an action event when the user enters text in the text field and presses the enter key (the event is generated when the enter key is pressed).  You can handle this event just as you would an event generated by a button.  Remember to attach the text field to the event handler via the addActionListener method.

The first exercise, below, asks you write a program that handles an ActionEvent generated by a JTextField object.

Exercises

  1. Write a Java application that presents the following window:

When the user types a color into the text field,  such as the color "red", and presses the enter key, the program will read the color and reset the text field so that its contents become "red is nice" (i.e., your program adds the words "is nice" to whatever color the user enters:

  1. Write a Java application that presents the following window:

Add event handling so that if the user presses the Give Mood button the content of the text field is set to "happy" if the good check box is currently selected.  If the good check box is not selected then the text field should be set to "sad" when the user presses the Give Mood button.

Solutions

  1. Solution to the "Favorite Color" problem.
  2. Solution to the "Mood Reader" problem.

One Handler For Two Different Components

The Intro to GUI page presented an application that had two buttons, labeled Red and Yellow, and a text field.  The background color of the text field was set to the red if the red button was pressed and to yellow is the yellow button was pressed (click here to review that code).  That program used two event handlers -- one handler for each button.  In this revised version of the program both buttons use one event handler -- the event handler determines which button caused the event using the ActionEvent object that is passed to the actionPerformed method.  The ActionEvent object has a method getSource that will yield a reference to the object that caused the event -- the code below compares that to the red and yellow buttons to see which one was the source of the event.

I highlighted the event parameter in red, both where it is passed into the actionPerformed method and where it is used inside the actionPerformed method.

   import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class OneHandler
{
public static void main(String [] args)
{
MyFrame frame = new MyFrame("One Handler");
frame.pack();
frame.setLocation(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class MyFrame extends JFrame
{
JButton red = new JButton("Red");
JButton yellow = new JButton("Yellow");
JTextField txt = new JTextField("some text", 12);

public MyFrame(String s)
{
super(s);
setLayout(new FlowLayout());

add(red);
add(txt);
add(yellow);
red.addActionListener(new ColorHandler());
yellow.addActionListener(new ColorHandler());
}

class ColorHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == red)
txt.setBackground(Color.RED);
else if (event.getSource() == yellow)
txt.setBackground(Color.YELLOW);
else
{
// if we get here there is some kind of error
txt.setBackground(Color.GRAY);
txt.setText("Error!");
}
}
}
}

 

Home - About Us
Copyright © 2006 by Kiowok, Ann Arbor, Michigan, USA