Custom Components - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Custom Components

Description:

Button groups. Listeners. JHU/APL EN 605.482. 9. Tri-State Check Box ... New class, does not inherit from AbstractButton. Worked great, but one big limitation ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 20
Provided by: robert361
Category:

less

Transcript and Presenter's Notes

Title: Custom Components


1
Custom Components
  • Understand what your users what to accomplish
  • Provide the correct UI components
  • Several issues when creating custom components
  • Do you combine existing components, start from
    scratch, or subclass other components
  • Is this a one shot component?
  • Do you need to support multiple look and feels?
  • How does it react with other components

2
Where does your component come from?
  • As you have seen, many Swing components have a
    lot of functionality already built into them
  • If you inherit from an existing component, you
    may get a lot of complex behavior
  • Tradeoffs
  • Inherit from existing Swing GUI component
  • Event handling
  • Complex behavior
  • May support multiple look and feels
  • Inherit from JComponent
  • You do everything
  • May be more efficient
  • Could be more work to implement

3
Combining Existing Components
  • Safest way to create complex, re-usable
    components.
  • Use Existing panels, labels, controls to come up
    with a more complex component.
  • Wizard
  • SliderPanel (SliderPanel.java, SliderWithReference
    Mark.java)
  • ListBuilder (ListBuilder.java)
  • Tree or List Panel (ListPanel.java,
    TreePanel.java)

4
FancyLabel
  • Wanted to display the following
  • Selecting box brings up new window
  • Show multi-line text
  • Try to make it look nice

Really basic
A little nicer
5
Design decisions
  • JButton or JLabel to show
  • JLabel
  • Need to implement its own mouse listeners
  • Supports multi-line, lthtmlgt
  • JButton
  • Could disable border and draw its own

6
How to implement
  • First look at API
  • Two candidates, change the renderer or the UI
  • Renderer modification
  • This almost works, except that the width of the
    displayed text field in the combo box is
    determined by querying each rendered value size.
  • So if we make the rendered value wider the
    entire component gets wider.
  • UI modification
  • Look at javax.swing.plaf
  • Three methods in class
  • Several from ComponentUI
  • Not much clues
  • Review of BasicComboBoxUI and MetalComboBoxUI
    show little new information

7
Look at source code
  • Lots of methods for setting text, icons,
    alignment
  • No paint() methods???
  • BasicLabelUI to the rescue
  • We want this to be a standard JLabel, but with a
    new background.
  • So
  • Call custom paint first to paint background
  • Call super.paint() to paint text!
  • FancyLabel.java

8
Example Three state check box
  • Common component is a three state check box
  • Cleared
  • Set
  • Indeterminate
  • How to approach solution
  • Subclass Button
  • New component
  • Tradeoffs
  • Button groups
  • Listeners

9
Tri-State Check Box
  • There is no easy way to subclass a JCheckBox to
    accomplish this
  • AbstractButtons model has two states, no support
    for three
  • TriCheckBox.java is one solution
  • Written with Java look and feel in mind
  • New class, does not inherit from AbstractButton
  • Worked great, but one big limitation

10
How TriCheckBox was made
  • What did it need to do?
  • Display Icon (checkbox)
  • Display Text
  • Handle HTML as text
  • Enable/Disable
  • Work with Buttongroup
  • Handle listeners
  • Initial decision
  • Subclass JCheckBox
  • Oops!

11
Use JLabel as base
  • Provides a lot of support required
  • Icon
  • Text
  • HTML
  • Enable/disable
  • To make JLabel work we need the following
  • MouseListener
  • Icon to reflect state
  • Event Generator

12
TriCheckBoxIcon
  • Found code for drawing Metal LF check box icon
  • Created TriCheckBoxIcon class
  • Issues
  • Great for Metal LF
  • Didnt support other LF
  • Decision was made to just worry about Metal LF

13
TriCheckBox
  • MouseWatcher to listen to mouse events
  • Methods to keep track of state
  • Methods to support an item listener
  • JLabel
  • Property listener
  • Enable disable
  • Looked good, functioned well, had to make a
    ButtonGroup object specifically for the
    TriCheckBox
  • Mistake! As JDK 1.5 showed

14
TriCheckBox2
  • Need the look and feel of a check box
  • Alternate method is to subclass the appropriate
    UI
  • Need to subclass all of the appropriate UIs
  • JCheckBox
  • ButtonModel has two states
  • Our model needs three states
  • Can ButtonModel be used as a trigger to increment
    our state
  • CustomUI that extends the CheckBoxUIs
  • Look through API no joy
  • Look through MetalCheckBoxUI.java no joy
  • Code draws icon then text in one method.
  • Calls getDefaultIcon() to get Icon to draw
  • Looks like we need to make a custom Icon

15
TriCheckBoxIcon2.java
  • The only way to do this properly is to provide
    Icons for all supported LFs
  • Metal
  • Windows
  • Motif
  • GTK
  • Synth
  • Mac
  • The Icon is used by the UI class, which is also
    LF based. In the UI, it wants to get model
    information from a ButtonModel.
  • We cant use a button model
  • So. Need to also write UI classes for all of the
    LF as well
  • Decision Very significant effort to implement
    this object correctly. Code still pending.

16
CustomComboBox
  • User wanted ComboBox showing small amount of info
    when closed, but extra info when opened
  • Used Custom Renderer for combo box
  • Used custom objects added to combo box
  • CustomComboBox.Location

17
ListBuilder
  • Requirement
  • Class that lets you build a list of objects, and
    prioritize their order. When done with the
    builder, you can call iterator() to get an
    Iteration of the objects in the list you built.
  • There are add and delete buttons, in addition
    there are up and down buttons which move the
    selected item in the list accordingly
  • Solution ListBuilder.java
  • Combines two JLists, buttons, Iterators and
    several listeners

18
Screen
  • Most Screen methods in Java dont work well with
    multiple monitors
  • Requirement
  • Develop a utility class that allows better
    management of window locations on multiple
    monitors
  • Solution Screen.java
  • Designed to handle Dual monitor systems
  • Additional monitors would require some more
    tweaks to code
  • Handles the following cases
  • Get number of monitors
  • Gets the monitor a java window, or rectangle, is
    in
  • Gets the next monitor
  • Gets a default monitor
  • Centers component on a monitor

19
SliderWithReferenceMark.java
  • Requirement
  • Create a JSlider with a reference mark on the
    scale. The mark could indicate a default, or
    pre-existing value.
  • The reference value should be able to be changed
    at any time
  • The reference mark color should be able to be
    changed at any time
  • Tooltip text should give what reference value
    currently is
  • Should work on all LFs
  • Solution
  • Modify UI of slider
  • Only two UIs used, BasicSliderUI and
    MetalSliderUI
Write a Comment
User Comments (0)
About PowerShow.com