Presentation is loading. Please wait.

Presentation is loading. Please wait.

Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.

Similar presentations

Presentation on theme: "Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001."— Presentation transcript:

1 Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001

2 © Benko 2001 Containers

3 © Benko 2001 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

4 javax.swing

5 © Benko 2001 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

6 © Benko 2001 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!!!

7 © Benko 2001 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


9 © Benko 2001 Why is JLayeredPane needed? It provides depth to the JFC/Swing container Allows components to overlap each other when needed

10 © Benko 2001 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

11 © Benko 2001 Frame Hierarchy - Summary

12 © Benko 2001 Additional Swing Containers JPanel (default container) JSplitPane Box container JTabbedPane JDesktopPane JScrollPane JViewport JTextPane JEditorPane

13 Layout Managers

14 © Benko 2001 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

15 © Benko 2001 Java2™ Layout Managers FlowLayout BorderLayout CardLayout BoxLayout GridLayout GridBagLayout

16 © Benko 2001 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

17 © Benko 2001 BorderLayout Default for content panes of JFrames and JApplets Places components into five areas: – “Center” – “North” – “South” – “East” – “West”

18 © Benko 2001 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()

19 © Benko 2001 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

20 © Benko 2001 GridLayout Arranges the components into rows and columns Order of addition to the container matters

21 © Benko 2001 GridBagLayout Similar to GridLayout Difference: each component size can vary and components can be added in any order desired

22 © Benko 2001 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)

23 © Benko 2001 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

24 © Benko 2001 “Look-and-feel”

25 © Benko 2001 Swing pluggable “look-and-feel” Swing provides flexibility to customize the “look-and-feel” automatically: – MS Windows style – Motif style (UNIX) – Metal (Java default) – or any other custom l&f UIManager.setLookAndFeel()

Download ppt "Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001."

Similar presentations

Ads by Google