Swing
Handling Events
In the previous chapter, we learn
how to create frames with labels, buttons and panels. However, when the button
is clicked it does nothing. In this chapter we will learn how to handle events
for buttons when it is clicked. The technique we use is called
event listening,
and it's one of the most important aspect of writing swing programs.
An event
is an object that's generated when the user does something with one of the user
interface components. For examples, a click of a button, selection of an item, a
drag of the mouse over a label and so on, all those are events. Then those event
objects are passed to a special method we create called
event listener.
Hence, the event listener determine exactly what type of event occurred, and
respond accordingly.
Look at the link (" Swing Method & constructor Table " ) below for a list of event and listener class with description.
When working with the swing class, we need to import javax.swing.*; for the swing components, import java.awt.*; for the color class, and import java.awt.event.*; for the event class.
Lets see a program that will
demonstrate a click of a button changes the background and text colors. The
method for changing the background color is
setBackground(name of color) and the method for changing the text color is
setForeground(name of color).
Here is a list of the color:
Color.BLACK
Color.RED
Color.PINK
Color.YELLOW
Color.GREEN
Color.ORNAGE
Color.WHITE
Color.CYAN
.
.
.
.
Example)
ClickHere to download ChangeBackground.java
The above example created 3 buttons, each button has its own event. A click of a button calls the event listener, then inside the event listener it changes the text and background color. Although, sometimes it would be a lot of work to write each event listener out hence we can use a else-if statement to determine which button is clicked with the help of two unique methods:
e.getSource(); --- Returns a reference to the object that generated this event
e.getActionCommand(); --- Returns the action command for this event as a String
Example)
ClickHere to download NameFrame.java
The above program used the requestFocus() method, what it does is when the user enters a name, clicks the ok button, then the JOptionPane pops out and prints out a message. When the use clicks the ok button on the JOptionPane again, with the requestFocus() method, the focus will automatically go back to the text field. However, if we didn't put the requestFocus() method, the focus will go back to the ok button. It might be a little confusing just reading it, thus download/copy the program run it and test it out for yourself with the focus method then run once again without the focus method.
Creating JTextArea
A text area simply is a text field that allows the user to enter more than one line of text. The text area can use the scroll bar to help the user to see the entire text. To create a text area we must use two classes. First, we use the JTextArea class to create the text area, then we would use the JScrollPane class to create scroll bars for the text area. Lets see a basic example on how to create a JTextArea.
Note: For more GUI examples please refer to the Java Example, GUI on the home page or simply click here. As for the JTextArea & JScrollPane methods and constructors click on the link below " Swing Method & Constructor Table. "
ClickHere to download TextArea.java