ch3 Java
ch3 Java
AWT and Swing are used to develop window-based applications in Java. Awt is an abstract
window toolkit that provides various component classes like Label, Button, TextField, etc., to
show window components on the screen. All these classes are part of the Java.awt package.
On the other hand, Swing is the part of JFC (Java Foundation Classes) built on the top of AWT
and written entirely in Java. The javax.swing API provides all the component classes like
JButton, JTextField, JCheckbox, JMenu, etc.
The components of Swing are platform-independent, i.e., swing doesn't depend on the operating
system to show the components. Also, the Swing's components are lightweight.
import javax.swing.*;
public class SwingApp {
SwingApp(){
JFrame f = new JFrame();
JLabel firstName = new JLabel("First Name");
firstName.setBounds(20, 50, 80, 20);
JLabel lastName = new JLabel("Last Name");
lastName.setBounds(20, 80, 80, 20);
JLabel dob = new JLabel("Date of Birth");
dob.setBounds(20, 110, 80, 20);
JTextField firstNameTF = new JTextField();
firstNameTF.setBounds(120, 50, 100, 20);
JTextField lastNameTF = new JTextField();
lastNameTF.setBounds(120, 80, 100, 20);
JTextField dobTF = new JTextField();
dobTF.setBounds(120, 110, 100, 20);
JButton sbmt = new JButton("Submit");
sbmt.setBounds(20, 160, 100, 30);
JButton reset = new JButton("Reset");
reset.setBounds(120,160,100,30);
f.add(firstName);
f.add(lastName);
f.add(dob);
f.add(firstNameTF);
f.add(lastNameTF);
f.add(dobTF);
f.add(sbmt);
f.add(reset);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
Output
Java AWT controls are the controls that are used to design graphical user interfaces or web
applications. To make an effective GUI, Java provides java.awt package that supports various
AWT controls like Label, Button, CheckBox, CheckBox Group, List, Text Field, Text Area,
Choice, Canvas, Image, Scrollbar, Dialog, File Dialog, etc that creates or draw various
components on web and manage the GUI based application.
Structure of the Java AWT Controls
The structure of the AWT is quite simple and it is used extensively in programs. Every AWT
inherits controls from the Container Class.
Container Class
It represents objects in graphical representation and it is an abstract class in the GUI interface.
The following are the list of commonly used UI elements in the GUI or commonly known as
Graphical User Interface.
GUI is a Graphical Interface that visually represents communication presented to the user for
easy interaction with the machine. GUI means Graphical User Interface. The common user
Interface includes Graphical representations like buttons and icons, and communication can be
performed by interacting with these icons rather than the usual text-based or command-based
communication.
Frame
The Frame is the container that contain title bar and border and can have menu bars. It can have
other components like button, text field, scrollbar etc. Frame is most widely used container while
developing an AWT application.
1. Label
A label is a user for placing text inside the container. A label is used only for inputting text. The
label does not imply that the text can be altered or it can be used as a button which can be further
worked upon.
Syntax:
2. Button
This command generates a button in the User Interface. Clicking on the button would move the
command to another page or another web server which is used to show several other outputs in
the user interface page.
Syntax:
3. Checkbox
There can be a certain question and the checkbox is used to determine the true or false nature of
the question being asked. If the checkbox is ticked then it means that the said question is true
which if it is unchecked it means that the said question is false. It is basically a true or false state
in Java programming language.
Syntax:
4. Checkbox Group
As the name implies the checkbox group is a set of checkboxes that are being used in the
programming language. There are many checkboxes that are being used and hence the group of
checkboxes is known as the checkbox group.
Syntax:
5. List
The list gives a scrolling list of items for the user. The scrolling list of items is also being set by
the user. The user sets the scrolling list of items such as Fruits, Vegetables, some questionnaire
or other facts.
Syntax:
6. Text Field
A text field is used for the editing of a particular line of text which can be used within the
programming concept.
Syntax:
7. Text Area
A text area is used for the editing of multiple lines of text. The only difference between the Text
field and Text area is that Text Field is used for editing a single line of text within the user
interface while a Text Area is used for editing multiple lines of text.
Syntax:
8. Choice
A choice, as the name implies, shows the various options and the choice that is selected is shown
in the top menu bar of the screen.
Syntax:
9. Canvas
In the canvas space, there can be an input being given by the user or the user can draw something
on the Canvas space being given.
Syntax:
10. Image
There can be a single image or multiple images within a UI. There can be a button being
associated with an image and when it is clicked it can produce some functionality.
Syntax:
12. Dialog
The dialog is used to take some form of input from the user and produce it in a sequential
manner.
Syntax:
Some of the important and common components of the Java Swing class are:
JFrame: JFrame is a top-level container that represents the main window of a GUI
application. It provides a title bar, and minimizes, maximizes, and closes buttons.
JPanel: JPanel is a container that can hold other components. It is commonly used to group
related components together.
JButton: JButton is a component that represents a clickable button. It is commonly used to
trigger actions in a GUI application.
JLabel: JLabel is a component that displays text or an image. It is commonly used to provide
information or to label other components.
JTextField: JTextField is a component that allows the user to input text. It is commonly used
to get input from the user, such as a name or an address.
JCheckBox: JCheckBox is a component that represents a checkbox. It is commonly used to
get a binary input from the user, such as whether or not to enable a feature.
JList: JList is a component that represents a list of elements. It is typically used to display a
list of options from which the user can select one or more items.
JTable: JTable is a component that represents a data table. It is typically used to present data
in a tabular fashion, such as a list of products or a list of orders.
JScrollPane: JScrollPane is a component that provides scrolling functionality to other
components. It is commonly used to add scrolling to a panel or a table.
Java JLabel
The object of JLabel class is a component for placing text in a container. It is used to display a
single line of read only text. The text can be changed by an application but a user cannot edit it
directly. It inherits JComponent class.
Java JTextField
The object of a JTextField class is a text component that allows the editing of a single line text. It
inherits JTextComponent class.
Java JTextArea
The object of a JTextArea class is a multi line region that displays text. It allows the editing of
multiple line text. It inherits JTextComponent class
JTextArea class declaration
Let's see the declaration for javax.swing.JTextArea class.
public class JTextArea extends JTextComponent
Commonly used Constructors:
Constructor Description
JTextArea() Creates a text area that displays no text initially.
JTextArea(String s) Creates a text area that displays specified text initially.
JTextArea(int row, int column) Creates a text area with the specified number of rows and
columns that displays no text initially.
JTextArea(String s, int row, int Creates a text area with the specified number of rows and
column) columns that displays specified text.
Java JCheckBox
The JCheckBox class is used to create a checkbox. It is used to turn an option on (true) or off
(false). Clicking on a CheckBox changes its state from "on" to "off" or from "off" to "on ".It
inherits JToggleButton class.
Java JRadioButton
The JRadioButton class is used to create a radio button. It is used to choose one option from
multiple options. It is widely used in exam systems or quiz.
Java JComboBox
The object of Choice class is used to show popup menu of choices. Choice selected by user is
shown on the top of a menu. It inherits JComponent class.
Java JTable
The JTable class is used to display data in tabular form. It is composed of rows and columns.
Java JList
The object of JList class represents a list of text items. The list of text items can be set up so that
the user can choose either one item or multiple items. It inherits JComponent class.
Java JFrame
The javax.swing.JFrame class is a type of container which inherits the java.awt.Frame class.
JFrame works like the main window where components like labels, buttons, textfields are added
to create a GUI.
Unlike Frame, JFrame has the option to hide or close the window with the help of
setDefaultCloseOperation(int) method.
Nested Class
Modifier and Class Description
Type
protected class JFrame.AccessibleJFrame This class implements accessibility support for
the JFrame class.
Fields
Modifier and Type Field Description
protected accessibleContext The accessible context property.
AccessibleContext
static int EXIT_ON_CLOSE The exit application default window close
operation.
protected JRootPane rootPane The JRootPane instance that manages the
contentPane and optional menuBar for this
frame, as well as the glassPane.
protected Boolean rootPaneCheckingEnabled If true then calls to add and setLayout will
be forwarded to the contentPane.
Constructors
Constructor Description
JFrame() It constructs a new frame that is initially invisible.
JFrame(GraphicsConfiguration gc) It creates a Frame in the specified
GraphicsConfiguration of a screen device and a blank
title.
JFrame(String title) It creates a new, initially invisible Frame with the
specified title.
JFrame(String title, It creates a JFrame with the specified title and the
GraphicsConfiguration gc) specified GraphicsConfiguration of a screen device.
Useful Methods
Modifier and Method Description
Type
protected void addImpl(Component comp, Object Adds the specified child
constraints, int index) Component.
protected createRootPane() Called by the constructor
JRootPane methods to create the default
rootPane.
protected void frameInit() Called by the constructors to
init the JFrame properly.
Void setContentPane(Containe contentPane) It sets the contentPane
property
static void setDefaultLookAndFeelDecorated(boolean Provides a hint as to whether
defaultLookAndFeelDecorated) or not newly created JFrames
should have their Window
decorations (such as borders,
widgets to close the window,
title...) provided by the
current look and feel.
void setIconImage(Image image) It sets the image to be
displayed as the icon for this
window.
void setJMenuBar(JMenuBar menubar) It sets the menubar for this
frame.
void setLayeredPane(JLayeredPane layeredPane) It sets the layeredPane
property.
JRootPane getRootPane() It returns the rootPane object
for this frame.
TransferHandler getTransferHandler() It gets the transferHandler
property.
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.
For registering the component with the Listener, many classes provide the registration methods. For
example:
Button
public void addActionListener(ActionListener a){}
MenuItem
public void addActionListener(ActionListener a){}
TextField
public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
TextArea
public void addTextListener(TextListener a){}
Checkbox
public void addItemListener(ItemListener a){}
Choice
public void addItemListener(ItemListener a){}
List
public void addActionListener(ActionListener a){}
public void addItemListener(ItemListener a){}