Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Department of Industrial and

Information Technology

DCIT 50: Object Oriented Programming

LESSON
Java Swing
6

Prepared:

NATHANAEL F. BUENO
Department of Industrial and
Information Technology

Objectives
• Discuss the concept of GUI design in Java programming using
Swing
Department of Industrial and
Information Technology

What is Java Swing?


Java Swing is a part of Java Foundation Classes (JFC) that is used to
create window-based applications.

The Swing library is built on top of the Java Abstract Widget Toolkit
(AWT)

The javax.swing package provides classes for java swing


API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox,
JMenu, JColorChooser etc.
Department of Industrial and
Information Technology

What is Java Swing?


JAVA AWT JAVA SWING
AWT components are platform-dependent. Java swing components are platform- independent
AWT components are heavyweight. Swing components are lightweight.
AWT doesn't support pluggable look and feel. Swing supports pluggable look and feel.
AWT provides less components than Swing. Swing provides more powerful components such as
tables, lists, scrollpanes, colorchooser, tabbedpane
etc.
AWT doesn't follows MVC(Model View Controller) Swing follows MVC.
where model represents data, view represents
presentation and controller acts as an interface
between model and view.
Department of Industrial and
Information Technology

Hierarchy of Java Swing classes


Department of Industrial and
Information Technology

Container Class
What is a container class?
Container classes are classes that can have other components on it. So for creating a GUI, we
need at least one container object. There are 3 types of containers.
1. Panel: It is a pure container and is not a window in itself. The sole purpose of a Panel is
to organize the components on to a window.
2. Frame: It is a fully functioning window with its title and icons.
3. Dialog: It can be thought of like a pop-up window that pops out when a message has to
be displayed. It is not a fully functioning window like the Frame.
Department of Industrial and
Information Technology

Java Swing IDE 1

5 6
Department of Industrial and
Information Technology

Java Swing IDE


1. Menu Bar – use to show tools not available in your work area.
2. Task Bar – use as shortcut on some of the events of the menu bar.
3. Project Explorer window – where you can see your project lists. If you cannot see this window, then you can go to
Window Menu and choose Projects as shown below. Or press Ctrl then 1 for the shortcut
4. Toolbox – sometimes called Palette in java. The Toolbox window contains all the controls you can use to build your
application’s interface. If you cannot see this window, then you can navigate to the menu bar as shown below.
5. Work Area – this is where you do your application development (design and coding). This is your form design.
6. Properties - This window (also known as the Properties Browser) displays all the properties of the selected component
or control and its settings. Every time you place a control on a form, you switch to this window to adjust the appearance
of the control. If you cannot see this window then you can navigate to the menu bar as shown below.
Department of Industrial and
Information Technology

Java Layout Managers


Layout Managers
• Absolute layout • Flow layout
• Free Design • Grid Bag Layout
• Border Layout • Grid Layout
• Box Layout • Null Layout
• Card Layout • OverLay Layout
Department of Industrial and
Information Technology

Java Layout Managers


When you create a JFrame, you can set a layout you want in the form. This can be done by
right clicking the JFrame form, select Set Layout, then select the layout you want int the
form as shown in the screenshot below.
Department of Industrial and
Information Technology

Java Layout Managers


Java BorderLayout
The BorderLayout is used to arrange the components in five regions: north, south, east,
west and center. Each region (area) may contain one component only. It is the default layout
of frame or window. The BorderLayout provides five constants for each region:

1. public static final int NORTH


2. public static final int SOUTH
3. public static final int EAST
4. public static final int WEST
5. public static final int CENTER
Department of Industrial and
Information Technology

Java Layout Managers


Java GridLayout
The GridLayout is used to arrange the components in rectangular grid. One component
is displayed in each rectangle.
Department of Industrial and
Information Technology

Java Layout Managers


Java FlowLayout
The FlowLayout is used to arrange the components in a line, one after another (in a
flow). It is the default layout of applet or panel.
Department of Industrial and
Information Technology

Java Layout Managers


Java BoxLayout
The BoxLayout is used to arrange the components either vertically or horizontally. For
this purpose, BoxLayout provides four constants. They are as follows:

Fields of BoxLayout class


1. public static final int X_AXIS
2. public static final int Y_AXIS
3. public static final int LINE_AXIS
4. public static final int PAGE_AXIS
Department of Industrial and
Information Technology

Java Layout Managers


Java CardLayout
The CardLayout class manages the components in such a manner that only one
component is visible at a time. It treats each component as a card that is why it is known as
CardLayout.
Department of Industrial and
Information Technology

Java Layout Managers


Java GridBagLayout
The Java GridBagLayout class is used to align components vertically, horizontally or
along their baseline.
Department of Industrial and
Information Technology

Java Layout Managers


Java GridBagLayout
The Java GridBagLayout class is used to align components vertically, horizontally or
along their baseline.
Department of Industrial and
Information Technology

GUI Building
Introduction to GUI Building

We will create a simple graphical user interface and add simple back-end
functionality. In particular we will go steps by steps on how to code the behavior of buttons and
fields in a Swing form, JFrame in particular. We will work through the layout and design of a
GUI and add a few buttons and text fields. The text fields will be used for receiving user input
and also for displaying the program output. The button will initiate the functionality built into
the front end. The application we create will be a simple display of information entered by the
user.

You might also like