Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and...

61
Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components JLabel Component JTextField Component Component Listeners Interfaces Inner Classes Anonymous Inner Classes JButton Component JOptionPane Dialog Box Distinguishing Between Multiple Events Using getActionCommand to Distinguish Between Multiple Events (optional) Color 1

Transcript of Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and...

Page 1: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Chapter 17GUI Programming Basics

GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components JLabel Component JTextField Component Component Listeners Interfaces Inner Classes Anonymous Inner Classes JButton Component JOptionPane Dialog Box Distinguishing Between Multiple Events Using getActionCommand to Distinguish Between Multiple

Events (optional) Color

1

Page 2: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Mouse Listeners MouseListener Interface MouseMotionListener Interface Mouse Adapter Classes DragSmiley Program Displaying an Image

Chapter 17GUI Programming Basics

2

Page 3: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

GUI Overview

GUI stands for Graphical User Interface. Graphical = picture objects (windows, buttons,

menus, etc.) User = the person who uses the program. Interface = the manner in which the user interacts

with the program. Although companies still write text-based

programs for internal use, most companies write GUI-based programs for programs that are to be used externally.

3

Page 4: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Event-Driven Programming Basics

GUI programs usually use event-driven programming techniques.

Basic idea behind event-driven programming: The program waits for events to occur and then it

responds. An event is a message that tells the program

that something has happened. For example, if the user clicks a button, then an event is generated, and it tells the program that a particular button was clicked.

More formally, when the user clicks a button, we say that the button object fires an event.

4

Page 5: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Event-Driven Programming Basics

Note these additional event examples:

User Action What Happens

Pressing the enter key while the cursor is inside a text box.

The text box object fires an event, and it tells the program that enter was pressed within the text box.

Clicking a menu item. The menu item object fires an event, and it tells the program that the menu item was selected.

Closing a window (clicking on the window's top-right corner "X" button).

The window object fires an event, and it tells the program that the window's close button was clicked.

5

Page 6: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Event-Driven Programming Basics

If an event is fired, and you want your program to handle the fired event, then you need to create a listener for the event.

For example, if you want your program to do something when the user clicks a particular button, you need to create a listener for the button.

If an event is fired and there's no listener listening to it, then the fired event is never "heard" and there's no response to it.

On the other hand, if there is a listener listening to a fired event, then the listener "hears" the event and the program then responds to the fired event.

The way the program responds is by executing a chunk of code known as an event handler.

6

Page 7: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Event-Driven Programming Basics

What happens when a button is pressed:

OK

useraction

firedevent

listener

--------------------------------------------------------------------------------------------------------------------------

eventhandler

7

Page 8: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

GUI Classes and Packages

In writing a GUI program, use Java's pre-built GUI classes. For example:

Use the pre-built JButton class when you need a button. Use the pre-built Color class when you need to specify a color.

In the first Java compiler, JDK 1.0, all GUI classes were bundled into one library known as the Abstract Windowing Toolkit (AWT).

The AWT's component classes were less than ideal in terms of portability, so Sun developed a set of more-portable GUI components and put them in a new GUI library named Swing.

The Swing library adds lots of functionality to the AWT, but it does not replace the AWT entirely.

Today, Java GUI application programmers use both libraries - Swing and the AWT.

The primary Swing package is javax.swing. The primary AWT packages are java.awt and java.awt.event. Get used to importing those three packages in all of your GUI programs.

8

Page 9: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

A Simple Window Program

import javax.swing.*; // for JFrame & JLabelimport java.awt.*; // for FlowLayout

public class SimpleWindow extends JFrame{ private static final int WIDTH = 300; private static final int HEIGHT = 200;

public SimpleWindow() { setTitle("Simple Window"); setSize(WIDTH, HEIGHT); setLayout(new FlowLayout()); setDefaultCloseOperation(EXIT_ON_CLOSE); createContents(); setVisible(true); } // end SimpleWindow constructor

private void createContents() { JLabel label = new JLabel("Hi! I'm Larry the label!"); add(label); } // end createContents

public static void main(String[] args) { new SimpleWindow(); } // end main} // end class SimpleWindow

9

Page 10: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

JFrame Class

The JFrame class: Should be used as the superclass for most of your

GUI application windows, so programmer-defined windows should extend the JFrame class.

Is in the javax.swing package, so import that package. (As explained in Chapter 5, you can use an * as a wildcard to import all the classes within a particular package.)

Is called a container class because: It contains things (like labels, buttons, menus, etc). It's derived from the Container class.

Implements all the standard window features such as: a border, a title bar, a minimize button, a close-window

button (the "X"), the ability to resize the window, etc.

12

Page 11: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

JFrame Class

JFrame methods: setTitle - Displays a title in the current window. setSize - Sets the width and height dimensions in

pixels of the current window. Your monitor will be set to a certain number of pixels

(e.g., 800x600, 1024x768, etc.). The pixel setting is called the resolution. A pixel is a monitor's smallest displayable unit.

setLayout(new FlowLayout()) - Assigns a specified layout manager to the current window.

The layout manager determines the positioning of components.

setDefaultCloseOperation(EXIT_ON_CLOSE) - Enables the close-window button (the "X" in the top-right corner) to work properly.

13

Page 12: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

JFrame Class

JFrame methods (continued): add - Adds a specified component to the current

window. Once the component is added to the window, it stays

with the window for the life of the program. Thus, in the SimpleWindow program, even though label

is defined locally within createContents, label stays with the window after createContents finishes.

setVisible(true) - Displays the window. Make sure you call setVisible at the end (after you've added all of the window's components).

setVisible(false) - Hides the window.

14

Page 13: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Java Components

Example Java components: JLabel, JTextField, JButton, JCheckBox, JRadioButton, JComboBox, JList, JTextArea JMenuBar, JMenu, JMenuItem

All of these component classes are in the javax.swing package so import that package.

All of these component classes are descendants of the JComponent class. The JComponent class supports many useful inheritable features. Along with many other methods, it contains methods that handle a component's: foreground and background colors text font border appearance tool tips focus

15

Page 14: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

JLabel Component

JLabel component interface: JLabel is a read-only component; the user can read

the label's message, but cannot update it. JLabel is a single-line component; \n won't work.

How to implement a label:1. Instantiate a JLabel object:

<reference-variable> = new JLabel(<label-text>);

2. Add the JLabel object to the window.

add(<reference-variable>);

3. The JLabel class is in the javax.swing package so import that package.

optional

16

Page 15: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

JLabel Component

Here are API headings and descriptions for two of the more popular JLabel methods:

public String getText() Returns the label's text.

public void setText(String text) Assigns the label's text.

17

Page 16: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

JTextField Component

JTextField component interface: The user can enter text into a text box.

How to implement a text box:1. Create a JTextField object with the JTextField

constructor: <reference-variable> = new JTextField(<default-text>, <width>);

2. Add the JTextField object to the window. add(<reference-variable>);

3. The JTextField class is in the javax.swing package so import that package.

optional

18

Page 17: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

This program demonstrates text boxes and labels. When the user presses enter in the text box, the text box's value displays in the bottom label.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Greeting extends JFrame

{

private static final int WIDTH = 325;

private static final int HEIGHT = 100;

private JTextField nameBox; // holds user's name

private JLabel greeting; // personalized greeting

//*******************************************

public Greeting()

{

setTitle("Greeting");

setSize(WIDTH, HEIGHT);

setLayout(new FlowLayout());

setDefaultCloseOperation(EXIT_ON_CLOSE);

createContents();

setVisible(true);

} // end constructor

//*******************************************

// Create components and add them to window.

private void createContents()

{

JLabel namePrompt =

new JLabel("What's your name?");

nameBox = new JTextField(15);

greeting = new JLabel();

add(namePrompt);

add(nameBox);

add(greeting);

nameBox.addActionListener(new Listener());

} // end createContents

//****************************************************

// Inner class for event handling.

private class Listener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

String message; // the personalized greeting

message = "Glad to meet you, " +

nameBox.getText() + "!";

nameBox.setText("");

greeting.setText(message);

} // end actionPerformed

} // end class Listener

//****************************************************

public static void main(String[] args)

{

new Greeting();

} // end main

} // end class Greeting

19

Page 18: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

JTextField Component

Here are API headings and descriptions for some of the more popular JTextField methods:

public String getText() Returns the text box's contents.

public void setText(String text) Assigns the text box's contents.

public void setEditable(boolean flag) Makes the text box editable or non-editable.

public void setVisible(boolean flag) Makes the text box visible or invisible.

public void addActionListener(ActionListener listener) Adds a listener to the text box.

20

Page 19: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Component Listeners

When the user interacts with a component (e.g., when the user clicks a button or presses enter while in a text box), the component fires an event.

If the component has a listener attached to it, the fired event is "heard" by the listener and consequently handled by the listener.

The listener handles the event by executing the code in its actionPerformed method.

21

Page 20: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Component Listeners

How to implement a listener for a text box:1. Define a class with an implements ActionListener clause (that

means that the class is an implementation of the ActionListener interface).

2. Include an actionPerformed event handler method in your listener's class:private class Listener implements ActionListener{ public void actionPerformed(ActionEvent e) { <do something> }

}

Note that if the ActionEvent event-handler parameter (e, above) isn't used within the actionPerformed method body, you are still required to define it. Why?

Because when a text-box-enter event occurs, the JVM looks for an actionPerformed method with an ActionEvent parameter. If the JVM can't find that exact method signature, then no event handling takes place.

22

Page 21: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Component Listeners

How to implement a listener for a text box (continued):3. Register your text box listener object. For example,

in the Greeting program, the createContents method registers a listener for the nameBox text box like this:

nameBox.addActionListener(new Listener());

4. Import the java.awt.event package. Event handling requires the use of the ActionListener interface and the ActionEvent class. Those entities are in the java.awt.event package, so that package must be imported for event handling to work.

23

Page 22: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Interfaces

An interface is a class-like thing whose methods are all empty.

If a programmer uses an interface to implement a new class, the compiler requires the new class to implement methods for all of the interface's methods.

So what's the point of having a class-like thing with all empty methods? It can be used as a template/pattern when creating a

class that falls into a certain category. More specifically, what's the point of the

ActionListener interface? Since all component listeners must implement it,

It means that all component listeners will be similar and therefore easier to understand.

It means that all component listeners will implement the actionPerformed method with the proper heading (and that ensures that component events will be received properly).

24

Page 23: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Inner Classes

If a class is limited in its scope such that it is only needed by one other class, then define the class as an inner class (a class inside of another class).

Since a listener is usually limited to listening to just one class, listeners are often implemented as inner classes.

To further the goal of encapsulation, "hide" the inner class by declaring it with the private access modifier.

25

Page 24: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Inner Classes

Besides furthering the goal of encapsulation, what's another reason to use an inner class as opposed to a top-level class? (Top-level class is the formal term for a regular class - a class not defined inside of another class.) An inner class can directly access its enclosing

class's instance variables. And listeners normally do need to access their enclosing class's instance variables, so this is an important benefit.

26

Page 25: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Greeting Program with an Anonymous Inner Class

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class GreetingAnonymous extends JFrame

{

private static final int WIDTH = 300;

private static final int HEIGHT = 200;

private JTextField nameBox; // holds user's name

private JLabel greeting; // personalized greeting

//**********************************************

public GreetingAnonymous()

{

setTitle("Greeting Anonymous");

setSize(WIDTH, HEIGHT);

setLayout(new FlowLayout());

setDefaultCloseOperation(EXIT_ON_CLOSE);

createContents();

setVisible(true);

} // end constructor

//***********************************************

// Create components and add them to window.

private void createContents()

{

JLabel namePrompt = new JLabel("What's your name?");

nameBox = new JTextField(15);

greeting = new JLabel();

add(namePrompt);

add(nameBox);

add(greeting);

nameBox.addActionListener(

// anonymous inner class for event handling

new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String message; // the personalized greeting

message =

"Glad to meet you, " + nameBox.getText();

nameBox.setText("");

greeting.setText(message);

} // end actionPerformed

} // end anonymous inner class

); // end addActionListener call

} // end createContents

//****************************************************

public static void main(String[] args)

{

new GreetingAnonymous();

} // end main

} // end class GreetingAnonymous

27

Page 26: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Anonymous Inner Classes

The point of using an anonymous object is to avoid cluttering up the code with a variable name when an object only needs to be used one time.

The point of using an anonymous inner class is to avoid cluttering up the code with a class name when a class only needs to be used one time.

For example, if a particular listener class listens to just one object, then the listener class needs to be used only one time as part of an addActionListener() method call. Therefore, to unclutter your code, you may want to use an anonymous inner class for the listener.

28

Page 27: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Anonymous Inner Classes

Syntax:new <interface-name>()

{

<class-body>

}

Example:nameBox.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

...

ActionListener is an interface

The anonymous inner class implements the specified interface.

29

Page 28: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

JButton Component

Button component interface: A button component acts like a real-world button -

when you press/click it, something happens.

How to implement a button:1. Create a button component with the JButton

constructor:

JButton helloButton = new JButton("Press me");

2. Add the button component to thewindow:

add(helloButton);

3. The JButton class and the add method are both in the javax.swing package so import that package.

button label's text, OK to omit argument

30

Page 29: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

JButton Component

How to implement a button (continued):4. Implement a listener class that includes an

actionPerformed event handler method:

private class Listener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

<do something>

}

}

5. Register your button-listener object:helloButton.addActionListener(new Listener());

31

Page 30: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

JButton Component

Here are API headings and descriptions for some of the more popular JButton methods :

public String getText() Returns the button's label.

public void setText(String text) Assigns the button's label.

public void setVisible(boolean flag) Makes the button visible or invisible.

public void addActionListener(ActionListener listener) Adds a listener to the button. The listener "listens" for

the button being clicked.

32

Page 31: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

When the user 1) clicks the button or 2) presses enter in the input text box, the entered number's factorial displays in the output text box.

import javax.swing.*;import java.awt.*;import java.awt.event.*;

public class FactorialButton extends JFrame{ private static final int WIDTH = 300; private static final int HEIGHT = 75;

private JTextField xBox; // user entry private JTextField xfBox; // factorial

//************************************

public FactorialButton() { setTitle("Factorial Calculator"); setSize(WIDTH, HEIGHT); setLayout(new FlowLayout()); setDefaultCloseOperation( EXIT_ON_CLOSE); createContents(); setVisible(true); } // end FactorialButton constructor

//*******************************

private void createContents() { JLabel xLabel = new JLabel("x:"); JLabel xfLabel = new JLabel("x!:"); JButton btn = new JButton("Factorial"); Listener listener = new Listener();

xBox = new JTextField(2); xfBox = new JTextField(10); xfBox.setEditable(false);

add(xLabel); add(xBox); add(xfLabel); add(xfBox); add(btn);

xBox.addActionListener(listener); btn.addActionListener(listener); } // end createContents

33

Page 32: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Factorial Button Program

//***********************************

// Inner class for event handling.

private class Listener implements ActionListener { public void actionPerformed(ActionEvent e) { int x; // user entered number int xf; // x factorial

try { x = Integer.parseInt( xBox.getText()); } catch (NumberFormatException nfe) { x = -1; // indicates invalid x }

if (x < 0) { xfBox.setText("undefined"); } else { if (x == 0 || x == 1) { xf = 1; } else { xf = 1; for (int i=2; i<=x; i++) { xf *= i; } } // end else

xfBox.setText( Integer.toString(xf)); } // end else } // end actionPerformed } // end class ListenerAll GUI I/O is string-based. Use

parseInt to convert string input to an integer.

34

Page 33: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Factorial Button Program

//**************************************

public static void main(String[] args) { new FactorialButton(); } // end main} // end class FactorialButton

35

Page 34: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

JOptionPane Dialog Box

A dialog box is a simple window that contains a message. For example:

To generate a simple output dialog box, call JOptionPane's showMessageDialog method:JOptionPane.showMessageDialog(null, <message>);

Example:JOptionPane.showMessageDialog(null, "Click factorial button to perform calculation.");

36

Page 35: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

JOptionPane Dialog Box

JOptionPane's showMessageDialog method is a class method so call it using <class-name> dot syntax.

showMessageDialog's first argument specifies the position of the dialog box. If the argument is null, the dialog box appears in the center of the screen.

The JOptionPane class is in the javax.swing package so import that package.

37

Page 36: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Distinguishing Between Multiple Events

For listeners that are registered with multiple components, when the listener "hears" an event firing, the listener usually needs to identify which component was responsible for the fired event.

To identify the component whose event was fired:

Within the actionPerformed method, use the actionPerformed method's ActionEvent parameter to call getSource.

The getSource method returns the address of the component whose event was fired, so to check which component is returned, use == with the original component reference variable.

38

Page 37: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Updated listener for the factorial-button program:When the user presses enter in the input text box, a warning dialog box is displayed.

private class Listener implements ActionListener { public void actionPerformed(ActionEvent e) { ... if (e.getSource() == xBox) { JOptionPane.showMessageDialog(null, "Click factorial button to perform calculation."); } else { try { x = Integer.parseInt(xBox.getText()); } ... }} // end actionPerformed } // end class Listener

39

Page 38: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Using getActionCommand to Distinguish Between Multiple Events (optional)

We call getSource to identify the component whose event was fired. That works fine most of the time, but not all of the time. Note the following cases where calling getSource is inadequate:1. If the event-firing component(s) is in a different class

from the listener class. The listener class's getSource method can successfully

retrieve the component responsible for the fired event, but there is no way to identify the type of the returned component because that requires comparing the returned component with the original components (using ==).

If the original component is in a different class and private, using it in the listener class generates a compilation error.

40

Page 39: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Using getActionCommand to Distinguish Between Multiple Events (optional)

Note the following cases where calling getSource is inadequate (continued)2. If there's a need to have a modal component.

A modal component is a component with more than one mode.

For example, suppose there's a button whose label toggles between "show details" and "hide details." The two labels correspond to two different modes of operation – in one mode details are shown, and in another mode details are hidden.

If a modal button is clicked, getSource can retrieve the button, but it cannot retrieve the button's mode. So the getSource method cannot reveal anything about the intended action (e.g., show details vs. hide details).

41

Page 40: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Using getActionCommand to Distinguish Between Multiple Events (optional)

What's the solution? To identify the event that was fired, use the actionPerformed method's ActionEvent parameter to call getActionCommand. The getActionCommand method returns the "action command" associated with the component whose event was fired. Typically, the action command is the component's label.

For example, the default action command for a button is the button's label and the default action command for a menu item is the menu item's label.

Example code:if (e.getActionCommand().equals("Factorial"))

...

42

Page 41: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Color

Most GUI components are comprised of two colors - foreground color is the color of the text, background color is the color of the area behind the text.

Here's how to create a red button with white text:JButton btn = new JButton("Click Me");btn.setBackground(Color.RED);btn.setForeground(Color.WHITE);

And here's what the red-white button looks like:

43

Page 42: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Color Methods

The setBackground and setForeground methods are standard mutator methods available to most GUI components.

Likewise, getBackground and getForeground methods are standard accessor methods available to most GUI components. Here are their API headings and descriptions:

public Color getBackground() Returns the component's background color.

public Color getForeground() Returns the component's foreground color.

Example for a text box:JTextField nameBox = new JTextField();

Color originalBackground = nameBox.getBackground();

Color originalForeground = nameBox.getForeground();

44

Page 43: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Color Named Constants

Color's variables (all of which are named constants):Color.BLACK Color.GREEN Color.REDColor.BLUE Color.LIGHT_GRAY Color.WHITEColor.CYAN Color.MAGENTA Color.YELLOWColor.DARK_GRAY Color.ORANGEColor.GRAY Color.PINK

The Color class is in the java.awt package, so import that package.

45

Page 44: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Color Objects

To obtain a color that is not in the Color class's list of named constant colors, instantiate a Color object with a specified mixture of red, green, and blue.

Here's the Color constructor call syntax:new Color(<red 0-255>, <green 0-255>, <blue 0-255>)

Each of the three Color constructor arguments is an int value between 0 and 255. The int value represents the amount of color, with 0 indicating no color and 255 indicating the maximum amount of color.

For example, this statement sets a button's background color to purple:button.setBackground(new Color(128, 0, 128));

46

Page 45: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

JFrame Background Color

To set the background color for a JFrame window, you first have to get the JFrame's content pane and then you apply the background color to the content pane.

To get the content pane, call the JFrame class's getContentPane method.

This code sets the background color to yellow:getContentPane().setBackground(Color.YELLOW);

content pane

47

Page 46: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

This program's buttons allow the user to set the window's background color to light red or light green.

import javax.swing.*;import java.awt.*;import java.awt.event.*;

public class ColorChooser extends JFrame{ private static final int WIDTH = 300; private static final int HEIGHT = 100;

// These buttons change the window's // background color to light red and // light green, respectively. private JButton stopButton; private JButton goButton;

//************************************

public ColorChooser() { setTitle("Background Color Chooser"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation( EXIT_ON_CLOSE); createContents(); setVisible(true); } // end ColorChooser constructor

//****************************

private void createContents() { setLayout(new FlowLayout()); stopButton = new JButton("Stop"); stopButton.setBackground(Color.RED); stopButton.addActionListener( new ButtonListener()); add(stopButton);

goButton = new JButton("Go"); goButton.setBackground(Color.GREEN); goButton.addActionListener( new ButtonListener()); add(goButton); } // end createContent

48

Page 47: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Background Color Chooser Program

//***************************************

// Inner class for event handling.

private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { Container contentPane = getContentPane(); if (e.getSource() == stopButton) { // Change the window background color to pink. contentPane.setBackground(Color.PINK); } else { // Change the window background color to light green. contentPane.setBackground(new Color(220, 255, 220)); } } // end actionPerformed } // end class ButtonListener

//**************************************

public static void main(String[] args) { new ColorChooser(); }} // end class ColorChooser

49

Page 48: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Mouse Listeners

Previously, you learned about the most common listener – the ActionListener.

You should use the ActionListener for events where the user does something to a component, such as clicking a button or pressing Enter within a text box. For mouse dragging events, you’ll need a mouse listener.

In creating a mouse listener, you use the same basic steps that you use for the ActionListener:

Define a listener class. Define an event handler method(s) within the listener

class. Register your listener class with a component.

50

Page 49: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

MouseListener Interface

Here are the API headings and descriptions for some of the more popular MouseListener interface event handlers:public void mouseClicked(MouseEvent event)

Called when the user presses and releases the mouse button while the mouse cursor is stationary on a MouseListener-registered component.

public void mousePressed(MouseEvent event)Called when the user presses the mouse button while the mouse cursor is on a MouseListener-registered component.

public void mouseReleased(MouseEvent event)Called when the user releases the mouse button, but only if the prior mouse press was on a MouseListener-registered component.

51

Page 50: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

MouseMotionListener Interface

Here are the API headings and descriptions for the MouseMotionListener interface event handlers:public void mouseDragged(MouseEvent event)

Called when the user holds the mouse button down while moving the mouse cursor, but only if the initial mouse press was on a MouseMotionListener-registered component.

public void mouseMoved(MouseEvent event)Called when the user moves the mouse while the mouse cursor is on a MouseMotionListener-registered component.

52

Page 51: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Mouse Listeners

For the mouse event handlers to be called, you need to register your mouse listeners with a Component object.

An image is not a component, so for the DragSmiley program, you can’t register your listeners with the smiley icon image. Instead, register your listeners with a JPanel object (a descendent of the Component class), and add the JPanel object to your JFrame.

In the DragSmiley program, note the listener class headings:private class ClickListener extends MouseAdapter

private class DragListener extends MouseMotionAdapter

The extends clauses indicate inheritance from the MouseAdapter and MouseMotionAdapter classes.

53

Page 52: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Mouse Adapter Classes

For each event handling interface with more than one method, Sun provides an associated class that already implements the interface's methods for you. Those classes are called adapter classes.

The MouseAdapter class implements the MouseListener interface's methods, and the MouseMotionAdapter class implements the MouseMotionListener interface's methods. Adapter classes don't do much. They simply implement their associated interface's methods as dummy methods, like this:public void mousePressed(MouseEvent event){ }

To implement a listener that detects the mouse being pressed, you extend the MouseAdapter class and provide an overriding mousePressed method.

54

Page 53: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

DragSmiley Program

/************************************************************** DragSmiley.java* Dean & Dean** This program displays a smiley face image.* When the user presses the mouse, the image changes to a* scared image. The user can drag the image.*************************************************************/

import javax.swing.*;

public class DragSmiley extends JFrame{ private static final int WIDTH = 250; private static final int HEIGHT = 250; private SmileyPanel smileyPanel; // drawing panel //**************************************

55

Page 54: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

DragSmiley Program

public DragSmiley() { setTitle("Drag Smiley"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(EXIT_ON_CLOSE); smileyPanel = new SmileyPanel(); add(smileyPanel); setVisible(true); } // end DragSmiley constructor //**************************************

public static void main(String[] args) { new DragSmiley(); }} // end class DragSmiley

56

Page 55: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

DragSmiley Program

/************************************************************** SmileyPanel.java* Dean & Dean** This class contains a smiley image and listeners* that enable image dragging and image swapping.*************************************************************/

import javax.swing.*;import java.awt.*;import java.awt.event.*;

public class SmileyPanel extends JPanel{ private final ImageIcon SMILEY = new ImageIcon("smiley.gif"); private final ImageIcon SCARED = new ImageIcon("scared.gif"); private final int WIDTH = SMILEY.getIconWidth(); private final int HEIGHT = SMILEY.getIconHeight();

private Point imageCorner; // image's top-left corner location private Point prevPt; // mouse location for previous event private ImageIcon image; // toggles between smiley and scared private boolean grabbed; // does mouse have a hold on the icon?

57

Page 56: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

DragSmiley Program

//**************************************

public SmileyPanel() { image = SMILEY; imageCorner = new Point(0, 0); // image starts at top left ClickListener clickListener = new ClickListener(); DragListener dragListener = new DragListener(); this.addMouseListener(clickListener); this.addMouseMotionListener(dragListener); } // end SmileyPanel constructor

//**************************************

private class ClickListener extends MouseAdapter { // When mouse pressed, change to scared image.

public void mousePressed(MouseEvent e) { image = SCARED; repaint();

58

Page 57: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

DragSmiley Program

prevPt = e.getPoint(); // save current position

// Make sure mouse was pressed within the image. if (prevPt.getX() >= imageCorner.getX() && prevPt.getX() <= imageCorner.getX() + WIDTH && prevPt.getY() >= imageCorner.getY() && prevPt.getY() <= imageCorner.getY() + HEIGHT) { grabbed = true; } } // end mousePressed

// When mouse released, return to smiley image.

public void mouseReleased(MouseEvent e) { image = SMILEY; repaint(); grabbed = false; } // end mouseReleased } // end class ClickListener

59

Page 58: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

DragSmiley Program

//**************************************

private class DragListener extends MouseMotionAdapter { // Enable image to be dragged by mouse.

public void mouseDragged(MouseEvent e) { Point currentPt = e.getPoint(); // current position

// Make sure mouse was pressed within the image. if (grabbed) { imageCorner.translate( (int) (currentPt.getX() - prevPt.getX()), (int) (currentPt.getY() - prevPt.getY())); prevPt = currentPt; // save current position repaint(); } } // end mouseDragged } // end class DragListener

60

Page 59: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

DragSmiley Program

//****************************************

// Draw the window, including the updated image.

public void paintComponent(Graphics g) { super.paintComponent(g); image.paintIcon(this, g, (int) imageCorner.getX(), (int) imageCorner.getY()); } // end paintComponent} // end class SmileyPanel

61

Page 60: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Displaying an Image

To display an image:1. Instantiate an ImageIcon object that stores an image file.

Specifically, call the ImageIcon constructor like this:private final ImageIcon SMILEY = new ImageIcon("smiley.gif");

2. Call the ImageIcon’s paintIcon method from within a paintComponent method.

The paintComponent method is called automatically by the JVM when the program starts up and whenever a user does something to alter the program's window (e.g., when the user resizes the window, or moves another window off of the window).

The JPanel class has a paintComponent method that's in charge of drawing Swing components (e.g., text boxes and buttons) within the JPanel container. But it doesn't handle drawing lines, shapes, or images. To draw those things, you need to provide an overriding paintComponent method with calls to graphics methods.

62

Page 61: Chapter 17 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.

Displaying an Image

To display an ImageIcon object, call the paintIcon method like this:<ImageIcon-reference-variable>.paintIcon(<image-observer>, <Graphics-reference-variable>, <top-left-x>, <top-left-y>);

Where: The <image-observer> argument refers to an object that

listens for the completion of the image being loaded. <top-left-x> refers to the x coordinate position of the

image's top-left corner. <top-left-y> refers to the y coordinate position of the

image's top-left corner. For example:

image.paintIcon(this, g, (int) imageCorner.getX(),

(int) imageCorner.getY());

63