Home arrow Java arrow Page 3 - 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 - Content Container
(Page 3 of 8 )

Before you can insert GUI elements into the window, you need to create a container for them. Think of a container as a box into which you place the GUI elements. The container is then placed inside the window.

A container is defined in the Container class found in the Abstract Windowing Toolkit. You can access the Container class by importing the java.awt package. You declare an instance of the Container class by calling the getContentPane() method of the JFrame class within the window class you defined for your program. The getContentPane() method returns a reference to the content container, which is assigned to a Container object reference. This is illustrated in the following statement:

Container ca = getContentPane();

The Container class defines many methods, but the method that is most commonly used is the setBackground() method. The setBackground() method colors the content container, giving your window a background color. The setBackground() method requires one argument: the constant defined in the Color class that refers to the color you want for the background. Table 12-2 lists the most commonly used color constants.

 

Constant

Color

Color.white

White

Color.lightGray

Light Gray

Color.gray

Gray

Color.black

Black

Color.blue

Blue

Color.green

Green

Color.cyan

Cyan

Color.magenta

Magenta

Color.orange

Orange

Color.pink

Pink

Color.yellow

Yellow

Table 12-2.  Color Constants Used to Paint the Content Container of 
                  a Window

The follow example shows how to declare an instance of a container and display the container within the window. You’ll notice that this example builds upon the previous example. This example colors the background of the container light gray:

Import java.awt.*;
import javax.swing.*;
   public class Demo {
      public static void main(String[] rgs) {
         Window win = new Window();
     
}
}
class Window extends JFrame {
  
public Window () {
      super ("Window Title");
      setSize(400,100);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      Container ca = getContentPane();
      ca.setBackground(Color.lightgray);
 
}
}

Java Layout Managers

In some programming languages you specify the pixel coordinates where the GUI element should appear within the window. Programmers call this specifying the exact position of the GUI element. Positing GUI elements within the container is a little tricky because you specify relative positions rather than exact positions. That is, you specify generally where you want the GUI element to be positioned on the screen, and the Java virtual engine determines the exact location.

At first, this technique of locating GUI elements within the container appears archaic. However, there is a very logical reason Java uses this technique. Remember that a Java program is machine independent. This means that the program that runs on your computer must also be able to run on any kind of computer that has a Java Virtual Machine running. And those other computers are likely to have a different screen than the screen you used to design your program.

To compensate for these differences, Java requires the Java Virtual Machine to determine the exact screen location for GUI elements. You simply provide a general location on the screen for where you want the GUI element.

Your first step is to decide on a layout manager for the container. Think of a layout manager as a way that Java organizes the container. There are four commonly used layout managers: the Flow Layout Manager, the Border Layout Manager, the Grid Layout Manager, and the Gridbag Layout Manager.

Flow Layout Manager

The Flow Layout Manager is used to place GUI elements in the container the way words are placed on a page—that is, from left to right then at the beginning of the next line. GUI elements are automatically repositioned in the center of the container whenever the window is resized.

You use the Flow Layout Manager by declaring an instance of the FlowLayout class, as shown here:

FlowLayout flm = new FlowLayout();

As you’ll soon discover when you begin placing GUI elements in a container, you have little control over the placement of the elements. For example, the FlowLayout class enables you to specify if you want GUI elements to be centered, flush left, or flush right by passing the constructor an appropriate constant. The default behavior is to center elements, so you don’t need to pass the constructor a constant to center the elements. You can specify left or right by passing the constructor the FlowLayout.LEFT or FlowLayout.RIGHT constant. You can also specify the horizontal and vertical gap between GUI elements by passing the constructor integers for both these values. The integer represents the number of pixels that will separate elements.

Many of the examples in this chapter use the Flow Layout Manager.

Border Layout Manager

The Border Layout Manager divides the container into five regions. These are north, south, east, west, and center. You use the Border Layout Manager by declaring an instance of the BorderLayout class, which is shown in the next example.

The Border Layout Manager divides the container into five regions. These are north, south, east, west, and center. You use the Border Layout Manager by declaring an instance of the class, which is shown in the next example.

The add() method of the container class is used to place a GUI element in the container. A version of the add() method requires two arguments. The first argument is a reference to the GUI element. The second argument is the layout manager reference used to position the element.

You use one of the BorderLayout class’s constants to specify the position. The BorderLayout class’s constants are listed here:

BorderLayout.NORTH
BorderLayout.SOUTH
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.CENTER

The following example illustrates how to use the Border Layout Manager. This example displays a window that contains two push buttons. You’ll learn more about push buttons in the “Push Buttons” section. One button is placed in the west area of the container, and the other button is displayed in the east area of the container (Figure 12-4).

 
Figure 12-4.  The Border Layout Manager divides the container into
                    five areas, known as north, south, east, west, and 
                    center.

import java.awt.*;
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");
      setSize(400,100);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      Container ca = getContentPane();
      ca.setBackground(Color.lightGray);
      BorderLayout blm = new BorderLayout();
      ca.setLayout(blm);
      JButton bt1 = new JButton("Start Test 1");
      ca.add(bt1,blm.WEST);
      JButton bt2 = new JButton("Start Test 2");
      ca.add(bt2,blm.EAST);
      setContentPane(ca);
  
}
}


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