1 / 67

Swing Set

Swing Set. By Theparit Peerasathien. Features of the Java Foundation Classes. New GUI called swing set Graphic API called 2D graphics Accessibility API Drag and Drop API. Heavyweights & Lightweights component. Heavyweight components (peer)

perry-avery
Download Presentation

Swing Set

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Swing Set By Theparit Peerasathien

  2. Features of the Java Foundation Classes • New GUI called swing set • Graphic API called 2D graphics • Accessibility API • Drag and Drop API

  3. Heavyweights & Lightweights component • Heavyweight components (peer) • Associated with its own  native screen resource • Connect with JVM & API • Slow • Example • Top-level swing: JFrame, JDialog, JApplet

  4. Heavyweights & Lightweights component • Lightweight components • borrows" the screen resource of an ancestor  • no native resource of its own • Example • All Swing set except Top-level swing

  5. How to use Components • Top-level component is a root of every containment hierarchy • All Swing programs have at least one Top-level component • Add Lightweight component to content Panes

  6. Swing Set • Top-Level Containrs • General-Purpose Containers • Special-Purpose Containers • Basic Controls • Uneditable Information Displays • Interactive Displays of Highly Formatted Information

  7. Top-level Components • Types of top-level containers • JFrames • JDialogs • JApplets • JWindow

  8. Top-Level Component • Add() components to Content pane • setLayout() content pane • Calling by • public Container getContentPane(); • Feature • support for adding a menu bar • need to use a content pane .

  9. JFrame • Window with border, title and buttons • Making frames • JFrame frame = new JFrame(); Or a extend JFrame class (often better code this way). • Style defined with UIManager.setLookAndFeel(looknfeel); SwingUtilities.updateComponentTreeUI(frame); frame.pack();

  10. Sample code

  11. Specifying Window Decorations

  12. Specifying Window Decorations

  13. Responding to Window-Closing Events • By default, when the user closes a frame onscreen, the frame is only hidden. • register a window listener that handles window-closing events • Use setDefaultCloseOperation method • DO_NOTHING_ON_CLOSE • HIDE_ON_CLOSE • DISPOSE_ON_CLOSE • EXIT_ON_CLOSE

  14. JApplet • a subclass of java.applet.Applet • Features • top-level Swing container • Swing applet has a root pane- support for adding a menu bar • has a single content pane.

  15. JApplet • add components to a Swing applet's content pane, not directly to the applet • set the layout manager on a Swing applet's content pane, not directly on the applet • default layout manager for a Swing applet's content pane is BorderLayout • should not put painting code directly in a JApplet object

  16. JApplet • import javax.swing.JApplet;import java.awt.*; • public class JAppletTest extends JApplet { • public void int() { • Container cp = getCntentPane(); • cp.setLayout (new FlowLayput); • cp.add(new JLabel(“Hello”); • }}

  17. windows that are more limited than frames To create simple, standard dialogs, you use the JOptionPane class. The ProgressMonitor class can put up a dialog that shows the progress of an operation JColorChooser and JFileChooser supply standard dialogs for Color chooser and File Chooser JDialog

  18. can create and customize several different kinds of dialogs provides support for laying out standard dialogs, providing icons, specifying the dialog's title and text, and customizing the button text JOptionPane

  19. Sample JDialog • //default title and iconJOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green."); • //custom title, warning iconJOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green.", "Inane warning", JOptionPane.WARNING_MESSAGE); • //custom title, custom iconJOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green.", "Inane custom dialog", JOptionPane.INFORMATION_MESSAGE, icon);

  20. JDialog

  21. Swing Set • Top-Level Containrs • General-Purpose Containers • Special-Purpose Containers • Basic Controls • Uneditable Information Displays • Interactive Displays of Highly Formatted Information

  22. JComponents • Except top-level container, All Swing components names begin with “J” descend from the JComponent class • Example JPanel, JButton, JTable • Except JFrame JDialog (Top-level container)

  23. JComponent Features • Tool tips - When the cursor pauses over the component, the specified string is displayed in a small window • Painting and Border • allows you to specify the border that a component displays around its edges • Look and feel • has a corresponding ComponentUI • Custom properties - associate one or more properties (name/object pairs) with any JComponent

  24. JComponent Features • Support for Layout • setter methods — setPreferredSize, setMinimumSize, setMaximumSize, setAlignmentX, and setAlignmentY • Support drag and Drop • provides API to set a component's transfer handler, which is the basis for Swing's drag and drop support • Double buffering • Double buffering smooths on-screen painting • Key Bindings • components react when the user presses a key on the keyboard Example when a button has the focus, typing the Space key is equivalent to a mouse click on the the button

  25. General-Purpose Containers • Example • Panel • Scroll pane • Split pane • Tabbed pane • Tool bar

  26. Sample

  27. Panel • JPanel class provides general-purpose containers for lightweight components • By default, panels don't paint anything except for their background • customize their painting • In many look and feels (but not GTK+), panels are opaque by default • You can change a panel's transparency by invoking setOpaque

  28. Setting the Layout Manager • By default, a panel's layout manager is an instance of FlowLayout • Example • JPanel p = new JPanel(new BorderLayout()); • JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));

  29. Adding Components • FlowLayout • aFlowPanel.add(aComponent); aFlowPanel.add(anotherComponent); • BorderLayout • aBorderPanel.add(aComponent, BorderLayout.CENTER); aBorderPanel.add(anotherComponent, BorderLayout.PAGE_END);

  30. Example

  31. Swing Set • Top-Level Containrs • General-Purpose Containers • Special-Purpose Containers • Basic Controls • Uneditable Information Displays • Interactive Displays of Highly Formatted Information

  32. Special-Purpose Containers • Intermediate containers that play specific roles in the UI. • Example • Internal Frame • Layered Pane • Root Pane

  33. Special-Purpose Containers

  34. Internal Frames • JInternalFrame class you can display a JFrame-like window within another window • add internal frames to a desktop pane • The desktop pane is an instance of JDesktopPane

  35. Example • ...//In the constructor of InternalFrameDemo, a JFrame subclass: • desktop = new JDesktopPane(); createFrame(); • //Create first window setContentPane(desktop); • //Make dragging a little faster but perhaps uglier. desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); • protected void createFrame() { MyInternalFrame frame = new MyInternalFrame(); frame.setVisible(true); desktop.add(frame); try { frame.setSelected(true); } catch (java.beans.PropertyVetoException e) {}

  36. Internal Frames vs. Regular Frames • internal frames is similar in many ways to the code for using regular Swing frames • internal frames have root panes • also provides other API, such as pack • Internal frames aren't windows or top-level containers • You can programatically iconify or maximize an internal frame. You can also specify what icon goes in the internal frame's title bar. You can even specify whether the internal frame has the window decorations to support resizing, iconifying, closing, and maximizing.

  37. Layered Panes • Swing container that provides a third dimension for positioning components • specify its depth as an integer. The higher the number, the higher the depth. • If components overlap, components at a higher depth are drawn on top of components at a lower depth

  38. Layered Panes

  39. Example Code

  40. Adding Components and Setting Component Depth

  41. Position Depth • Positions are specified with an int between -1 and (n - 1), where n is number of component • the smaller the position number, the higher the component within its depth exception (-1) is deepest too

  42. Component's position change

  43. Layer pane

  44. Root Panes • don't directly create a JRootPane object • instantiate JInternalFrame or one of the top-level Swing containers • Use Top-Level Container • getting the content pane • setting its layout manager • adding Swing components

  45. four parts of root pane • The glass pane • completely transparent unless you implement the glass pane's paintComponent method so that it does something, and it intercepts input events for the root pane. • The layered pane • Serves to position its contents, which consist of the content pane and the optional menu bar. Can also hold other components in a specified Z order.. • The content pane • The container of the root pane's visible components, excluding the menu bar.. • The optional menu bar • The home for the root pane's container's menus. If the container has a menu bar, you generally use the container's setJMenuBar method to put the menu bar in the appropriate place.

  46. The Glass Pane • is useful when you want to be able to catch events or paint over an area that already contains one or more components • It contains a check box that lets you set whether the glass pane is "visible" — whether it can get events and paint itself onscreen. • When the glass pane is visible, it blocks all input events from reaching the components

  47. Sample Code

  48. Swing Set • Top-Level Containrs • General-Purpose Containers • Special-Purpose Containers • Basic Controls • Uneditable Information Displays • Interactive Displays of Highly Formatted Information

  49. Basic Control Component • Atomic components that exist primarily to get input from the user • show simple state

  50. Basic Control Component

More Related