Java GUI Basics

Swing

Credits

Dialog boxes MaxDialog.java

Java GUI and Event Handling
Using containers:
Components:
General Process

MoodDisplay.java, DisplayStyles.java, StyleGUI.java, LanguageSurvey.javaMusicPreference.java and MusicPreference2.java



Note two different strategies:
  1. Extend JFrame. Used in MusicPreference. The main method is basically a call to the constructor.
  2. Create a JFrame, add components, show. Used in DisplayStyles.


More on Events (from JFC Swing Tutorial)

2D Drawing

Coordinate systems Representing color Graphics class

Commands for drawing graphics. These are methods of the Graphics class.  An object of this type is automatically sent to the paintComponent method.
Forbidden.java and ForbiddenPanel.java


Mouse Events

There are two categories of events that are generated by mouse activities: mouse events and mouse motion events

Mouse Event
Description
mouse pressed
The mouse button is pressed down
mouse released
The mouse button is released
mouse clicked
The mouse button is pressed down and released without moving the mouse in between
mouse entered
The mouse pointer is moved onto (over) a component
mouse exited
The mouse pointer is moved off a component

Mouse Motion Event
Description
mouse moved
The mouse is moved
mouse dragged
The mouse is moved while the mouse button is pressed down

Many events may be generated by simple user actions.  For example, when you click the mouse button over a GUI component, three events are generated: mouse pressed, mouse clicked and mouse released. 

In a specific situation, we may care about only one or two mouse events.  The following program only listens for mouse pressed events.   Notice that because it implements an interface, it must provide empty bodies for all other methods.

Dots.java, DotsPanel.java, Shape.java, Oval.java and Rectangle.java


Layout Managers

A layout manager is an object that governs how components are arranged in a container.  It determines the size and position of every component.  When the size of a container is adjusted, for example, the layout manager is consulted to determine how all components should appear. 

The table below lists the predefined layout managers.  For some layout managers, the order in which you add components affects their position, others provide more specific control.  Some take a component's preferred size into account, others don't. 

Layout Manager
Description
Border Layout
Organizes components into 5 areas North, South, East, West & Center (also called PAGE_START, PAGE_END, LINE_START, LINE_END)
Box Layout
Organizes components into a single row or column
Card Layout
Organizes components into one area such that only one is visible at any time
Flow Layout
Organizes components from left to right, starting new rows as necessary
Grid Layout
Organizes components into a grid of rows and columns
GridBag Layout
Organizes components into a grid of cells, allowing components to span more than one cell


The setLayout method of a container can be used to change the layout manager for a JPanel
JPanel panel = new JPanel();
panel.setLayout (new BorderLayout());

The most popular are: Flow, Border, Box and Grid

The example below also uses a JTabbedPane, which enables the user to display a JPanel with tabs, only one of which can be active at a time. This is similar to the Card Layout, except that the choice of which area is visible is up to the user.

LayoutDemo.java (includes 5 panel classes)

Borders

Java provides the ability to put a border around any Swing component.  A border is not a component itself, but defines how the edge of the component should be drawn.  The BorderFactory class is used to create borders.  The setBorder method to set the border for a specific component.  An empty border can be used to create a buffer of space.  The sizes of borders are specified in pixels.  Types of borders are specified in the table below.

Border
Description
Empty Border
Puts buffering space around the edge of a component, but otherwise has no visual effect
Line Border
A simple line surrounding the component
Etched Border
Creates the effect of an etched groove around a component
Bevel Border
Creates the effect of a component raised above the surface or sunken below it
Titled Border
Includes a text title on or around the border
Matte Border
Allows the size of each edge to be specified.  Uses either a solid color an an image.
Compound Border
A combination of two borders

BorderDemo.java