Week+2 4+Event+Driven+Programs
Week+2 4+Event+Driven+Programs
Event
Changing the state of an object is known as an event. For example, click on button,
dragging mouse etc. The java.awt.event package provides many event
classes and Listener interfaces for event handling.
The java.awt package provides classes for AWT API such as TextField, Label,
TextArea, RadioButton, CheckBox, Choice, List etc.
The AWT tutorial will help the user to understand Java GUI programming in
simple and easy steps.
GUIs are built from GUI components. These are sometimes called controls or
widgets short for window gadgets in other languages. A GUI component is an
object with which the user interacts via the mouse, the keyboard or another form of
input, such as voice recognition.
A Frame is a top-level window with a title and a border. The size of the frame
includes any area designated for the border. The dimensions of the border area may
be obtained using the getInsets method. Since the border area is included in the
overall size of the frame, the border effectively obscures a portion of the frame,
constraining the area available for rendering and/or displaying subcomponents to
the rectangle which has an upper-left corner location of (insets.left, insets.top), and
has a size of width - (insets.left + insets.right) by height - (insets.top +
insets.bottom).
JFrame
A frame, implemented as an instance of the JFrame class, is a window that has
decorations such as a border, a title, and supports button components that close or
iconify the window. Applications with a GUI usually include at least one frame.
Applets sometimes use frames, as well.
JFrame provides the basic attributes and behaviors of a window a title bar at the
top of the window, and buttons to minimize, maximize and close the window.
Since an application's GUI is typically specific to the application, most of our
examples will consist of two classes a subclass of JFrame that helps us
demonstrate new GUI concepts and an application class in which main creates and
displays the application's primary window.
actually a base window, on which other components rely, namely the menu bar,
panels, labels, text fields, buttons, etc. Almost every other Swing application starts
with the JFrame window. Unlike a frame, JFrame has the option to hide or close
the window with the help of the method setDefaultCloseOperation(int).
The following FrameDemo code shows how to create and set up a frame.
5 menuBar.add(menuFile);
image. Applications rarely change a label's contents after creating it. The
application of Example 1 and Example 2 demonstrates several JLabel features and
presents the framework we use in most of our GUI examples. We did not highlight
the code in this example since most of it is new.
[Note: There are many more features for each GUI component than we can cover
in our examples. To learn the complete details of each GUI component, visit its
page in the online documentation.
Example 1: LabelFrame.java
1 // Example 1: LabelFrame.java
2 // Demonstrating the JLabel class.
3 import java.awt.FlowLayout; // specifies how components are arranged
4 import javax.swing.JFrame; // provides basic window features
5 import javax.swing.JLabel; // displays text and images
6 import javax.swing.SwingConstants; // common constants used with Swing
7 import javax.swing.Icon; // interface used to manipulate images
8 import javax.swing.ImageIcon; // loads images
9
10 public class LabelFrame extends JFrame
11 {
12 private JLabel label1; // JLabel with just text
13 private JLabel label2; // JLabel constructed with text and icon
14 private JLabel label3; // JLabel with added text and icon
15
16 // LabelFrame constructor adds JLabels to JFrame
17 public LabelFrame()
18 {
19 super( "Testing JLabel" );
20 setLayout( new FlowLayout() ); // set frame layout
21
22 // JLabel constructor with a string argument
23 label1 = new JLabel( "Label with text" );
24 label1.setToolTipText( "This is label1" );
25 add( label1 ); // add label1 to JFrame
26
Example 2: LabelTest.java
1 // Example 2: LabelTest.java
2 // Testing LabelFrame.
3 import javax.swing.JFrame;
4
5 public class LabelTest
6 {
7 public static void main( String args[] )
8 {
9 LabelFrame labelFrame = new LabelFrame(); // create LabelFrame
10 labelFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
11 labelFrame.setSize( 275, 180 ); // set frame size
In this section, we introduce two new GUI components that can generate events
JTextFields and JPasswordFields (package javax.swing). Class
JTextField extends class JTextComponent (package javax.swing.text),
as the user enters them, but hides the actual characters with an echo character,
assuming that they represent a password that should remain known only to the
user.
When the user types data into a JTextField or a JPasswordField, then presses Enter,
an event occurs. Our next example demonstrates how a program can perform a task
when that event occurs. The techniques shown here are applicable to all GUI
components that generate events. The application of example 3 and example 4 uses
classes JTextField and JPasswordField to create and manipulate four text
fields. When the user types in one of the text fields, then presses Enter, the
application displays a message dialog box containing the text the user typed. You
can only type in the text field that is "in focus." A component receives the focus
when the user clicks the component. This is important because the text field with
the focus is the one that generates an event when the user presses Enter. In this
example, when the user presses Enter in the JPasswordField, the password is
revealed. We begin by discussing the setup of the GUI, then discuss the event-
handling code.
Example 3
1 // Example 3: TextFieldFrame.java
2 // Demonstrating the JTextField class.
3 import java.awt.FlowLayout;
4 import java.awt.event.ActionListener;
Example 4.
1 // Fig. 13.3: TextFieldTest.java
2 // Testing TextFieldFrame.
3 import javax.swing.JFrame;
4
5 public class TextFieldTest
6 {
7 public static void main( String args[] )
8 {
Lines 39 import the classes and interfaces we use in this example. Class
TextFieldFrame extends JFrame and declares three JTextField variables
and a JPasswordField variable (lines 13-16). Each of the corresponding text
fields is instantiated and attached to the TextFieldFrame in the constructor (lines
19 to 47)
Line 38 creates passwordField with the text "Hidden text" to display in the text
field. The width of the text field is determined by the width of the default text.
When you execute the application, notice that the text is displayed as a string of
asterisks.
Line 39 adds passwordField to the JFrame.
and are frequently used for event handling. Before an object of an inner class can
be created, there must first be an object of the top-level class that contains the inner
There is also a special relationship between these objects the inner-class object is
allowed to directly access all the instance variables and methods of the outer class.
A nested class that is static does not require an object of its top-level class and
does not implicitly have a reference to an object of the top-level class.
The event handling in this example is performed by an object of the private inner
class TextFieldHandler (lines 50-80). This class is private because it will be
used only to create event handlers for the text fields in top-level class
TextFieldFrame. As with other members of a class, inner classes can be declared
public, protected or private.
JPasswordField supports the same events. To prepare to handle the events in this
Now, when the user presses Enter in any of these four text fields, method
actionPerformed (line 53-79) in class TextFieldHandler is called to handle
the event. If an event handler is not registered for a particular text field, the event
that occurs when the user presses Enter in that text field is consumed that it, it is
simply ignored by the application.
ActionEvent method getSource (called in lines 58, 63, 68 and 73) returns a
reference to the event source. The condition in line 58 asks, "Is the event source
textField1?" This condition compares the references on either side of the ==
operator to determine whether they refer to the same object. If they both refer to
textField1, then the program knows that the user pressed Enter in textField1.
In this case, lines 59-60 create a String containing the message that line 78 will
display in a message dialog. Line 60 uses ActionEvent method
getActionCommand to obtain the text the user typed in the text field that generated
the event. If the user interacted with the JPasswordField, lines 74-75 use
JPasswordField method getPassword to obtain the password and create the
String to display. This method returns the password as an array of type char that
https://www.edureka.co/blog/java-jframe/
https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html