Guide to Object-oriented Programming With Java (Buffalo State College Version)


Chapter 16: Using java.awt and javax.swing to Create GUI Applications

Up to this point, the Java programs have accepted input from and written output to the Eclipse Console window. Java provides a number or robust libraries for creating Graphical User Interfaces (GUIs). These include the java.awt.*, javax.swing.*, javafx.*, org.eclipse.swt.* libraries.

Java GUI Basic Concepts

Before you can begin creating a Java GUI application you need to understand a few basic GUI concepts, such as frames, components (fields, labels, etc.), panels, layout managers, and coordinates:

Frames
The window (GUI) where your program’s components are held is called the Frame.
Components
Components consist of: text fields, radio buttons, labels, and menus. Components are all contained within the frame. Every Java application runs within a window and every window needs a frame where various components can "live".
Panels
The panel is the content pane is where all of your programs components are placed. You can think of the content pane as a background and the various components of your program are placed onto this background.
Layout Managers
The layout manager aligns components within content pane into a specific style depending on what layout you choose (BorderLayout, BoxLayout, CardLayout, FlowLayout, GridBagLayout, GridLayout, GroupLayout, SpringLayout, etc.). Certain applications are well suited for using a layout manager since they can make component placement much easier, but they can also create additional complications not typically experienced when you set your layout manager to null and assign exact coordinates for every component.
Coordinates
Using coordinates is important when you are not using a layout manager. Coordinates are used to specify where components are placed on the content pane. Coordinates are described just like x-y graphs from high school math class (X, Y). The (0, 0) coordinate is the top left corner of the pane. You can specify the exact position of components in your program by setting these coordinates.

Figure 16-1: Java GUI Terminology

Adding WindowBuilder and Swing Your Eclipse Project

Before you can run a GUI Java program you need to follow these steps to add Swing and WindowBuilder to Eclipse.

Steps to add Swing to Eclipse

  1. Click on: Help
  2. Click on: Install New Software
  3. In the "Work with:" box type: neon or oxygen
  4. From the dropdown, choose: Neon - http://download.eclipse.org/releases/neon or /oxygen
  5. And in the second box type: Swing
  6. Check all the boxes
  7. Click: Next
  8. Click: Next
  9. Accept the terms
  10. Click: Finish
  11. Restart Eclipse, click: Yes
Figure 16-2: Steps to add Swing to Eclipse

Steps to add WindowBuilder to Eclipse

  1. Click on: Help
  2. Click on: Install New Software
  3. In the "Work with:" box type: neon (or oxygen)
  4. From the dropdown, choose: Neon - http://download.eclipse.org/releases/neon or /oxygen)
  5. In the second box type: window
  6. Check all the boxes
  7. Click: Next
  8. Click: Next
  9. Accept the terms
  10. Click: Finish
  11. Restart Eclipse, click: Yes
Figure 16-3: Steps to add WindowBuilder to Eclipse

The java.awt and java.swing Libraries

To create a Java GUI application using the java.awt and java.swing libraries you first need to import the packages you need from each of them. Then you can add a layout (in this example BorderLayout) and components. Finally, you need to add the panes to the frame and make the frame using frame.setVisable(true). This creates the pop-up GUI application.

package buffalostate.edu.sp18.assignments.your-last-name;
  
  import java.awt.BorderLayout;
  import java.awt.Color;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  
  public class MyFirstFrame {

  public static void main(String[] args) {    
    JFrame frame = new JFrame(); // Create the window frame
    frame.getContentPane().setBackground(Color.white);
    frame.getContentPane().setForeground(Color.white);
    frame.setForeground(Color.white);
    frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
    frame.setTitle("My First Frame");
    JPanel bluePanel = new JPanel();
    JPanel redPanel = new JPanel();
    JLabel label = new JLabel(" <-- pick your side --> ");
    frame.getContentPane().add(label, BorderLayout.CENTER);
    bluePanel.setBackground(Color.blue);
    redPanel.setBackground(Color.red);
    frame.getContentPane().add(bluePanel, BorderLayout.LINE_START);
    frame.getContentPane().add(redPanel, BorderLayout.LINE_END);
    JButton blueButton = new JButton("PICK BLUE TEAM");
    JButton redButton = new JButton("PICK RED TEAM");    
    bluePanel.add(blueButton);
    redPanel.add(redButton);    
    frame.pack();
    frame.setVisible(true);
  }
}
Figure 16-4: MyFirstFrame.java Using BorderLayout Code

Here is a list of Java background and foreground colors.

MyFirstFrame

Figure 16-5: MyFirstFrame.class Output

Let's get started with a GUI program!

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2018 Jim Gerland