1 / 11

Example Program

DemoTree.java. DemoTree2.java. Example Program. ComponentUI Class. The delegate part of a component is derived from an abstract class named ComponentUI Naming convention: remove the “J” from the component’s class name, then add “UI” to the end (e.g., JButton  ButtonUI). ComponentUI.

Download Presentation

Example Program

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. DemoTree.java DemoTree2.java Example Program

  2. ComponentUI Class • The delegate part of a component is derived from an abstract class named ComponentUI • Naming convention: remove the “J” from the component’s class name, then add “UI” to the end (e.g., JButton  ButtonUI) ComponentUI ButtonUI BasicButtonUI MenuButtonUI MultiButtonUI CustomButtonUI

  3. Design Challenge • A corporation specializing in children’s games wishes to use a custom “corporate style” in all their applications • As one example, they’d like the buttons for their applications to look as follows… • Design a custom L&F for a JButton, as above Normal Armed Pressed FUNNY STUFF FUNNY STUFF FUNNY STUFF KIDS STUFF KIDS STUFF KIDS STUFF

  4. Example Program DemoCustomButtonUI.java

  5. Preferred Size Property • An important task for a window manager is determining the size of widgets • What happens when getPreferredSize is invoked on a JButton? • JButton’s getPreferredSize method is inherited from JComponent • Let’s see…

  6. getPreferredSize (from JComponent.java) /** * If the preferredSize has been set to a non-null value * just returns it.If the UI delegate's getPreferredSize() * method returns a non null value then return that;otherwise * defer to the component's layout manager. * * @return the value of the preferredSize property * @see #setPreferredSize */ public Dimension getPreferredSize() {if (preferredSize != null) { return preferredSize; }Dimension size = null; if (ui != null) { size = ui.getPreferredSize(this); } return (size != null) ? size : super.getPreferredSize(); } • Returns either… • Value set with setPreferredSize, • Value from UI delegate, or • Value from Container

  7. Programming Challenge Solution • Create a new version of DemoSizeProperties that presents the component name, class, and size properties in a JTable • Name the new program… PC_SizeProperties.java

  8. Undo/Redo in Swing • Swing provides facilities for generalized, unlimited Undo/Redo • Relevant classes: • UndoManager • AbstractUndoableEdit(default implementation of the UndoableEdit interface)

  9. Undo/Redo Setup (step 1) • For each operation that you would like your user to be able to undo, create a subclass of AbstractUndoableEdit • You may need to provide implementation for the following methods: undo() redo() canUndo() canRedo() getPresentationName()

  10. Undo/Redo Setup (step 2) • Create an instance of the UndoManager class • You can call the following methods on this instance: undo() redo() addEdit(UndoableEdit) canUndo() canRedo() getUndoPresentationName() getRedoPresentationName()

  11. Example program DemoUndo.java

More Related