The Abstract Windowing Toolkit Swing: JFC JDK 1.2
The Abstract Windowing Toolkit Swing: JFC JDK 1.2
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
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.
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
Menu bar
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
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.
• 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
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