Graphical User Interfaces with Swing

"It don't mean a thing if it ain't got that Swing"
                             -- Duke Ellington

Reading Assignment: Section 9.3 in Morelli
Reading Assignment for Nov 14: Section 9.5 in Morelli


We will only use Swing for GUI components - we will not use the Abstract Window Toolkit (AWT).
We will use AWT event handling, but no AWT components.


First some definitions and terminology-



So to create our first GUI we will:
Import the necessary libraries
Use a JFrame for our top-level container.
Add all buttons and other low-level components to a JPanel object, and then add the JPanel to the content pane.



Ex: Adding a panel (instance of JPanel) to the content pane of our frame (instance of JFrame).
Container contentPane = frame.getContentPane(); // returns content pane object for this JFrame
JPanel p = new JPanel();
contentPane.add(p);



Two additional comments:

Event Handling - The Basics


Different event sources produce different event objects. A button sends ActionEvent objects, a window sends WindowEvent objects.


We will first handle making our frame closeable - we need some way of being notified that the user is closing the frame:
Event = window being closed.
We will attach a listener to the frame that will close the frame in response to this event.

The listener we will use: an instance of a class that implements the WindowListener interface. The WindowListener interface contains 7 methods, and we are interested in this one.
public void windowClosing(WindowEvent e)
We will implement this method so that the program exits when it is executed.

To avoid implementing all 7 methods in the WindowListener interface, we will use a class WindowAdapter that implements this interface.. The WindowAdapter class implements all 7 methods of WindowListener interface to do nothing - we will write a new class that extends the WindowAdapter class and overrides the windowClosing() method.


Example
addWindowListener(new WindowAdapter()
{
   public void windowClosing(WindowEvent e)
   {
      System.exit(0);
   }
});  // Our anonymous inner class extends the WindowAdapter class. Now the program terminates when the frame is closed.


Example: FirstGUI class


The Inheritance Hierarchy for GUI Components (in class)
 -- Methods that are used to resize and reshape frames are in the Component class and the Window class.


Example: Another GUI. This time we will:


Layout Management

Flow Layout
Example:
Component pane = JPanel();
...
pane.setLayout(new FlowLayout(FlowLayout.LEFT));  // pass in alignment to FlowLayout constructor




Example: GUIEx.java. Multiple Buttons in a panel.


Example: GUIEx - edit it to use a different alignment

Another FlowLayout constructor:
FlowLayout(int alignment, int horizontalGap, int verticalGap)



Border Layout

Example: pane.setLayout(new BorderLayout() );
pane.add(button, "South");

Exercise: Modify GUIEx so that button, button2, button3 appear in the South region of the frame.




Grid Layout

Example: JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 2));


Exercise: Write your own program that uses a grid layout to place 2 rows of buttons in a frame, with 2 buttons per row. Handle button clicks for each button.