Nov 181 Example Program DemoTree2.java DemoTree.java
Nov 182 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) ComponentUIButtonUIBasicButtonUI MenuButtonUI MultiButtonUI CustomButtonUI
Nov 183 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 FUNNY STUFF KIDS STUFF FUNNY STUFF KIDS STUFF Pressed FUNNY STUFF KIDS STUFF
Nov 184 Example Program DemoCustomButtonUI.java
Nov 185 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…
Nov 186 /** * 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. * the value of the preferredSize property #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(); } getPreferredSize (from JComponent.java) Returns either… Value set with setPreferredSize, Value from UI delegate, or Value from Container
Nov 187 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
Nov 188 Undo/Redo in Swing Swing provides facilities for generalized, unlimited Undo/Redo Relevant classes: UndoManager AbstractUndoableEdit(default implementation of the UndoableEdit interface)
Nov 189 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()
Nov 1810 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()
Nov 1811 Example program DemoUndo.java