Home arrow Java arrow Page 4 - 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 - Grid Layout Manager and the Gridbag Layout Manager
(Page 4 of 8 )

The Grid Layout Manager divides the container into rows and columns. You create the Grid Layout Manager by declaring an instance of the GridLayout class, which is shown here:

The Grid Layout Manager divides the container into rows and columns. You create the Grid Layout Manager by declaring an instance of the class, which is shown here:

GridLayout glm = new GridLayout();

You specify the number of rows and columns that form the grid when you declare the instance of the GridLayout class. You do so by passing the constructor two arguments. Both arguments are integers. The first argument specifies the number of rows, and the second argument specifies the number of columns. Each cell is of equal size regardless of the size of the GUI element assigned to that cell.

A drawback of using the Grid Layout Manager is that you cannot specify the cell to place a GUI element. Elements are placed in cells from left to right in the order in which they are added to the container.

It is for this reason that some programmers prefer to use the Gridbag Layout Manager. The Gridbag Layout Manager enables you to place a GUI element into a specific cell. You create a Gridbag Layout Manager by declaring an instance of the GridbagLayout class, which is shown in the next example.

You’ll also need to declare an instance of the GridBagConstraints class. The GridBagConstraints class is used to specify where to position the GUI element in the grid. You assign a row and column number to the gridx and gridy attributes, respectively, and then pass a reference to the instance of the GridBagConstraints class to the container’s add() method when adding a new GUI element to the container.

 
Figure 12-5.   You can create a grid and specify the location of GUI
                     elements on the grid by using the Gridbag Layout
                     Manager.

The following example shows how to create a grid and then position two buttons within the grid. Figure 12-5 displays the grid created by this example.

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);
      GridBagLayout gblm = new GridBagLayout();
      GridBagConstraints gbc = new GridBagConstraints();
      ca.setLayout(gblm);
      JButton bt1 = new JButton("Start Test 1");
      gbc.gridx = 1;
      gbc.gridy = 0;
      ca.add(bt1,gbc);
      JButton bt2 = new JButton("Start Test 2");
     
gbc.gridx = 1;
     
gbc.gridy = 1;
     
ca.add(bt2,gbc);
     
setContentPane(ca);
  
}
}

Push Buttons

Now that you’ve learned how to create a window and a container and then specify a layout manager, we can turn our attention to adding GUI elements to the container. Let’s begin by creating a push button.

You must follow three steps to add a push button to the container. First, you’ll need to create the push button. This is done by declaring an instance of the JButton class. With few exceptions, most buttons you’ll create will have a label on them. Passing text to the constructor creates the label. Here’s how you’d create a “Start” button:

JButton start = new JButton("Start");

The second step is to pass a reference to the instance of JButton to the add() method of the container. Depending on the layout manager you choose, you may also want to specify the location of the button as the second argument to the add() method (see “Java Layout Managers”).

The third step is to pass a reference to the content pane of the container to the setContentPane() method. Think of the content pane as a pane of glass that fits into the container. GUI elements are on this pane of glass. The setContentPane() method places the pane of glass into the window (container). You create a content pane by calling the getContentPane() method, as shown in the following example.

 
Figure 12-6.  Adding two push buttons to the window

Figure 12-6.   shows the window displayed when you run the example.

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);
      FlowLayout flm = new FlowLayout();
      ca.setLayout(flm);
      JButton start = new JButton("Start");
      ca.add(start);
      JButton stop = new JButton("Stop");
      ca.add(stop);
      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