0% found this document useful (0 votes)
68 views

The Abstract Windowing Toolkit Swing: JFC JDK 1.2

The document discusses the Swing toolkit in Java, which was introduced to improve on the original Abstract Window Toolkit (AWT) for building graphical user interfaces (GUIs). Some key points: 1) Swing provides lightweight components that are rendered in Java rather than relying on platform-specific controls. It also supports pluggable look-and-feel and is based on the model-view-controller pattern. 2) The Java Foundation Classes (JFC) include Swing components like JFrame for windows, JButton, JLabel, and various layout managers for positioning components. 3) Layout managers define how components are arranged in containers, and different layouts like BorderLayout, FlowLayout, GridBag

Uploaded by

DrSandeep Mathur
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

The Abstract Windowing Toolkit Swing: JFC JDK 1.2

The document discusses the Swing toolkit in Java, which was introduced to improve on the original Abstract Window Toolkit (AWT) for building graphical user interfaces (GUIs). Some key points: 1) Swing provides lightweight components that are rendered in Java rather than relying on platform-specific controls. It also supports pluggable look-and-feel and is based on the model-view-controller pattern. 2) The Java Foundation Classes (JFC) include Swing components like JFrame for windows, JButton, JLabel, and various layout managers for positioning components. 3) Layout managers define how components are arranged in containers, and different layouts like BorderLayout, FlowLayout, GridBag

Uploaded by

DrSandeep Mathur
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

The Abstract Windowing

Swing
Toolkit
• Since Java was first released, its user • The Swing classes are used to build GUIs
interface facilities have been a significant – Swing does not stand for anything
– Swing is built on top of the 1.1/1.2 AWT libraries
weakness
• Swing makes 3 major improvements on the AWT
– The Abstract Windowing Toolkit (AWT) was part of
– does not rely on the platform’s native components
the JDK form the beginning, but it really was not – it supports “Pluggable Look-and-Feel”
sufficient to support a complex user interface – it is based on the Model-View-Controller (MVC)
• JDK 1.1 fixed a number of problems, and
JFC
most notably, it introduced a new event AWT JDK 1.2

model. It did not make any major additions to Swing

the basic components

Java Foundation Classes Components


• In April 1997, JavaSoft announced the • A GUI consists of different graphic
Java Foundation Classes (JFC). Component objects that are combined into
– a major part of the JFC is a new set of user a hierarchy using Container objects.
interface components called Swing. • Component class
– An abstract class for GUI components such as menus,
buttons, labels, lists, etc.
Drag
Java
AWT Swing Accessibility
2D
And • Container
Drop – An abstract class that extends Component. Containers
can hold multiple components.

1
Weighing Components Additional Swing Features
• Sun makes a distinction between lightweight and • Swing also provides
heavyweight components – A wide variety of components (tables, trees, sliders,
– Lightweight components are not dependent on progress bars, internal frame, …)
native peers to render themselves. They are – Swing components can have tooltips placed over them.
coded in Java. – Arbitrary keyboard events can be bound to components.
– Additional debugging support.
– Heavyweight components are rendered by the
– Support for parsing and displaying HTML based
host operating system. They are resources
information.
managed by the underlying window manager.

Heavyweight Components Applets versus Applications


• Heavyweight components were unwieldy for • Using Swing it is possible to create two different
two reasons: types of GUI programs
– Equivalent components on different platforms do – Standalone applications
• Programs that are started from the command line
not necessarily act alike.
• Code resides on the machine on which they are run
– The look and feel of each component was tied to
– Applets
the host operating system • Programs run inside a web browser
• Almost all Swing components are lightweight • Code is downloaded from a web server
except • JVM is contained inside the web browser
• For security purposes Applets are normally prevented from
– JApplet, JFrame, JDialog, and JWindow doing certain things (for example opening files).
• For now we will write standalone applications

2
JFrames JFrame
• A JFrame is a Window with all of the • Sizing a Frame
adornments added. – You can specify the size.
• Height and width given in pixels.
• A JFrame provides the basic building block • The size of a pixel will vary based on the resolution
for screen-oriented applications. of the device on which the frame is rendered.
JFrame win = new JFrame( “title” ); – The method, pack(), will set the size of the
frame automatically based on the size of the
components contained in the content pane
• Note that pack does not look at the title bar.

– SwingFrame2.java

Creating a JFrame JFrame


Layered pane
• SwingFrame.java • JFrames have several panes:
Glass pane

Menu bar

Content pane

• Components are placed in the content pane

3
Swing Components Hello World Example
• JComponent • SwingFrame3.java – Using a label.
– JComboBox, JLabel, JList, JMenuBar, JPanel,
JPopupMenu, JScrollBar, JScrollPane, JTable,
JTree, JInternalFrame, JOptionPane,
JProgressBar, JRootPane, JSeparator, JSlider,
JSplitPane, JTabbedPane, JToolBar, JToolTip,
Jviewport, JColorChooser, JTextComponent,

JLabels JButtons
• JLabels are components that you can • JButton extends Component , displays a
put text into. string and delivers an ActionEvent for
• When creating a label you can specify each mouse click.
the initial value and the alignment you • Normally buttons are displayed with a
wish to use within the label. border.
• You can use getText() and setText() to • In addition to text, JButtons can also
retrieve and modify the value of the display icons.
label. button = new JButton( ”text“ ) ;
label = new JLabel( ”text", JLabel.RIGHT ) ;
• SwingButton.java

4
Layout Manager Layout Managers
• Layout Manager • Layouts allow you to format components on the
screen in a platform independent manner.
– An interface that defines methods for
• The standard JDK provides five classes for
positioning and sizing objects within a
implementing the LayoutManager interface:
container.
– FlowLayout
– Java defines several default – GridLayout
implementations of LayoutManager. – BorderLayout
• Geometrical placement in a Container is – CardLayout
– GridBagLayout
controlled by a LayoutManager object
• Layout managers are defined in the AWT package

Components, Containers, and


Changing the Layout
Layout Managers
• Containers may contain components • To change the layout used in a container the
– that means containers can contain containers!!. program must first create the layout.
• All containers come equipped with a layout • Then the setLayout() method is invoked on the
manager that positions and shapes (lays container that is to use the new layout.
out) the container's components. JPanel p = new JPanel() ;
• Much of the action in the AWT occurs p.setLayout( new FlowLayout() );
between components, containers, and their
layout managers. • The layout manager should be specified before
any components are added to the container.

5
FlowLayout GridLayout
• The GridLayout manager arranges
• FlowLayout is the default layout for the
components in rows and columns.
JPanel class.
– If the number of rows is specified
• When you add components to the screen, • columns = number of components / rows
they are added from left to right (centered) – If the number of columns is specified
based on the order added and the width of • rows = number of components / columns
the screen. – The number of columns is ignored unless the
– Very similar to word wrap and full justification on number of rows is zero.
a word processor.
– If the screen is resized, the components' layout
will change based on the new width and height.

Flow Layout GridLayout


• SwingFlowLayout.java – Resize • The order in which components are
Window. added to the layout manager matters.
– Component 1 Æ (0,0), Component 2 Æ
(0,1), …...
• Components are resized to fit the row-
column area.

• SwingGridLayout.java – Resize.

6
BorderLayout Containers
• BorderLayout provides 5 areas to hold • A JFrame is not the only container in Swing.
components. • The subclasses of Container are:
– The areas are named after the four different – JPanel
borders of the screen: North, South, East, – JWindow
West, and Center. – JApplet

• When a Component is added to the layout, • Window is subclassed as follows:


– JDialog
the area to place it in must be specified. – JFrame
– The order in which components is not
important.

A Simple 4 Function
BoarderLayout
Calculator
• The center area will always be resized
to be as large as possible.

• SwingBoarder.java – Resize.

7
Swing Components
JFrame
with BorderLayout JLabel

JButton

JPanel
with GridLayout

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy