Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
-
date post
20-Dec-2015 -
Category
Documents
-
view
218 -
download
0
Transcript of Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
© B
en
ko 2
00
1
Swing Top-Level Containers
heavyweight: JFrame, JDialog, JWindow, JApplet– extend AWT containers– platform-depended code – every component takes on the look-and-feel of the
platform on which the program currently executes lightweight: JInternalFrame
– completely written in Java – provides uniform functionality across platforms as
well as the uniform cross-platform look-and-feel
© B
en
ko 2
00
1
Understanding Swing Top-Level Containers
We NEVER add() any components directly to the top-level container
Instead we add() to the container’s content pane:– content pane is a repository for the components
added to the container– obtained by getContentPane() method– content pane is an object of the JRootPane
© B
en
ko 2
00
1
What is JRootPane?
It provides the architectural structure of the Swing container hierarchy.
JRootPane object is not usually created by user, but instead it is given automatically whenever one creates a container
Adding children, setting layout managers, adding/removing components to the JRootPane is NOT ALLOWED
All those actions have to be done to the content pane!!!
© B
en
ko 2
00
1
JRootPane Information
Root pane is made up of:– contentPane– glassPane (used for intercepting mouse events and
drawing)– JMenuBar (optional)
The content pane and the menu bar are managed by JLayeredPane
© B
en
ko 2
00
1
Why is JLayeredPane needed?
It provides depth to the JFC/Swing container
Allows components to overlap each other when needed
© B
en
ko 2
00
1
Explanation of Layers
Default - standard layer where most components go
Palette - used for floating toolbars and palettes Modal - used for modal dialogs Popup - used for popup windows, combo
boxes, tooltips, help text Drag - for displaying components while they
are being dragged across the screen
© B
en
ko 2
00
1
Additional Swing Containers
JPanel (default container) JSplitPane Box container JTabbedPane JDesktopPane JScrollPane JViewport JTextPane JEditorPane
© B
en
ko 2
00
1
Java2™ Layout Managers
Arrange GUI components on a container for presentation purposes
Abstract the idea of positioning widgets on the screen
Enables the programmer to concentrate on the functionality of their program and lets the layout managers process most of the layout details
© B
en
ko 2
00
1
Java2™ Layout Managers
FlowLayout BorderLayout CardLayout BoxLayout GridLayout GridBagLayout
© B
en
ko 2
00
1
FlowLayout
Default for java.awt.Applet, java.awt.Panel and javax.swing.JPanel
Places components sequentially (left to right) in the order they were added
© B
en
ko 2
00
1
BorderLayout
Default for content panes of JFrames and JApplets
Places components into five areas:
– “Center” <default>– “North”– “South”– “East”– “West”
© B
en
ko 2
00
1
CardLayout
A layout manager that stacks components like a deck of cards
Only the component at the “top” of the deck is visible
Methods to use: first(), next(), previous(), last()
© B
en
ko 2
00
1
BoxLayout
Allows GUI components to be arranged in a container:
– left-to-right (horizontally)– top-to-bottom (vertically)
Class Box defines a container with BoxLayout as its default layout manager
© B
en
ko 2
00
1
GridLayout
Arranges the components into rows and columns
Order of addition to the container matters
© B
en
ko 2
00
1
GridBagLayout
Similar to GridLayout
Difference: each component size can vary and components can be added in any order desired
© B
en
ko 2
00
1
GridBagLayout
To use GridBagLayout you must construct GridBagConstraints object
GridBagConstraints instance variables:– gridx (column)– gridy (row)– gridwidth (column span)– gridheight (row span)– weightx (horizontal extra space allocation)– weighty (vertical extra space allocation)
© B
en
ko 2
00
1
Others…
OverlayLayout (used by JButton) ScrollPaneLayout (used by JScrollPane) ViewportLayout (used by JViewport) AbsoluteLayout (non-standard - Forte specific) You can write your own layout manager:
– All you have to do is implement correctly the LayoutManager interface