Java Swing Components and Containment 1 Components and

  • Slides: 25
Download presentation
Java Swing Components and Containment 1

Java Swing Components and Containment 1

Components and Containers n Components • The building blocks • Variety of uses and

Components and Containers n Components • The building blocks • Variety of uses and complexities n Containers • The cement • Hierarchical organisation • Distinction is not always drawn 2

Containment hierarchies n Top level containers • Intermediate containers n n Atomic components Viewing

Containment hierarchies n Top level containers • Intermediate containers n n Atomic components Viewing containment hierarchies • <Ctrl-Shift-F 1> 3

Top-level containers n n At the root of every containment hierarchy All Swing programs

Top-level containers n n At the root of every containment hierarchy All Swing programs have at least one Content panes Types of top-level containers • • • Frames Dialogs Applets 4

Frames n Window with border, title and buttons n Making frames • JFrame frame

Frames n Window with border, title and buttons n Making frames • JFrame frame = new JFrame(); Or extend JFrame class (often better code this way). n Style defined with UIManager. set. Look. And. Feel(looknfeel); Swing. Utilities. update. Component. Tree. UI(frame); frame. pack(); 5

a JFrame example //this won’t compile… public static void main(String[] args) { JFrame frame

a JFrame example //this won’t compile… public static void main(String[] args) { JFrame frame = new JFrame(“A JFrame"); //Just like any //other class // do things with frame. set. JMenu. Bar(menu. Bar); frame. set. Content. Pane(content. Pane); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE) ; // set frame size frame. pack(); // realize frame. set. Visible(true); } // end main 6

Examples 1 + 2 n Swing. Application. java • Messy way. n Better. Swing.

Examples 1 + 2 n Swing. Application. java • Messy way. n Better. Swing. App. java • Neater way. 7

Dialog boxes n n n More limited than frames Modality Types of dialogs •

Dialog boxes n n n More limited than frames Modality Types of dialogs • • JOption. Pane Progress. Monitor JColor. Chooser JDialog 8

Showing dialogs n JOption. Pane. show. XYZDialog(…) • Option and Message dialogs JOption. Pane.

Showing dialogs n JOption. Pane. show. XYZDialog(…) • Option and Message dialogs JOption. Pane. show. Message. Dialog(frame, ”Error!”, ”An error message”, JOption. Pane. ERROR_MESSAGE); JOption. Pane. show. Option. Dialog(frame, “Save? ”, “A save dialog”, JOption. Pane. YES_NO_CANCEL_OPTION); • Input, Confirm n Customisation • • show. Option. Dialog - Fairly customisable JDialog - Totally customisable 9

Example 3 n Dialog. Demo. java • Not looking at code in detail… 10

Example 3 n Dialog. Demo. java • Not looking at code in detail… 10

Applets n Not covered in great detail here n JApplet is a top-level container

Applets n Not covered in great detail here n JApplet is a top-level container • has menu bar and content pane support n n JApplet supports assistive technologies Requires Java plug-in for browser • consider user group 11

Intermediate containers – Panels (or ‘panes’) n Root panes • • • The content

Intermediate containers – Panels (or ‘panes’) n Root panes • • • The content pane Layered panes Glass panes 12

Root panes n ‘Invisibly’ attached to top-level container n Created by Swing on realizing

Root panes n ‘Invisibly’ attached to top-level container n Created by Swing on realizing frame n n Manages everything between top-level container and components Places menu bar and content pane in an instance of JLayered. Pane (see a couple of slides on) 13

Content panes n n n Usually use a JPanel Contains everything except menu bar

Content panes n n n Usually use a JPanel Contains everything except menu bar for most Swing applications Can be explicitly, or implicitly created, • see (simplified) code //Create a panel and add components to it. JPanel content. Pane = new JPanel(); content. Pane. add(some. Component); content. Pane. add(another. Componet); //Make it the content pane. content. Pane. set. Opaque(true); top. Level. Container. set. Content. Pane(content. Pane); 14

Example 4 n Top. Level. Demo. java • Illustrates the Content Pane, and Menu

Example 4 n Top. Level. Demo. java • Illustrates the Content Pane, and Menu Bar positioning. 15

Layered panes n n n Provided by root pane, but can also be created

Layered panes n n n Provided by root pane, but can also be created Provides depth (z-buffering) to components ‘Depth’ is specified as integer • • • Frame content (-30000, content pane, menu bar) Default (0, components) Palette (100, toolbars and palettes) Modal (200, internal dialogs) Popup (300, external dialogs) Drag (400, component when dragged) 16

Example 5 n Layered. Pane. Demo. java 17

Example 5 n Layered. Pane. Demo. java 17

Glass panes n Not structured into components • event catching • painting n n

Glass panes n Not structured into components • event catching • painting n n Used for ‘weird’ interface behavior, rarely used. Either created explicitly or root version used 18

Example 6 n Glass. Pane. Demo. java 19

Example 6 n Glass. Pane. Demo. java 19

Components n Content of your interface • http: //java. sun. com/docs/books/tutorial/uiswin g/components. html n

Components n Content of your interface • http: //java. sun. com/docs/books/tutorial/uiswin g/components. html n Created just like any class instance • JButton button_ok = new JButton(“OK”); n Range in complexity from very simple (e. g. JButton) to very detailed (e. g. JColor. Chooser) 20

Swing and AWT components a quick reminder n n Mix Swing and AWT components

Swing and AWT components a quick reminder n n Mix Swing and AWT components as little as possible (not at all in most cases) Put ‘J’ in front of everything AWT provides to get Swing’s counterpart • AWT: Button • Swing: JButton 21

Atomic components n n n n Buttons Combo boxes Lists Menus Sliders Text Fields

Atomic components n n n n Buttons Combo boxes Lists Menus Sliders Text Fields Labels 22

Atomic components n n n n Tool tips Progress bars Colour choosers File choosers

Atomic components n n n n Tool tips Progress bars Colour choosers File choosers Tables Text Trees 23

Atomic components n n n Impossible to teach the working of every type of

Atomic components n n n Impossible to teach the working of every type of component Very few people know it all! – Swing is HUGE. Remember to refer to: • • • Swing tutorial The Java 2 API Documentation. The Visual index to components & containers at java. sun. com: n http: //java. sun. com/docs/books/tutorial/uiswing/components/com ponents. html 24

Summary n Containers (Frames and Dialogs) • • • n Hierarchy Root Panes Layered

Summary n Containers (Frames and Dialogs) • • • n Hierarchy Root Panes Layered Panes Content Panes Glass Panes Components • Lots of ‘em… n Next time • Layout Management. 25