Professional Documents
Culture Documents
Karsten Lentzsch
www.JGoodies.com
Presentation Goals
Introduction
MVC and Swing
How to bind single values?
How to bind lists
A 3-tier Swing architecture
How binding works in projects
Painting Code
State Operations
(Control Behavior)
State
Painting Code
State
Server
Advantages:
– Each part is easier to understand
– Each part is easier to change
Painting Code
Presentation Layer
State Modifications
Advantages:
– Reduces complexity
– Allows to build multiple presentations
of a single domain object
Painting Code
State Modifications
State
View Controller
reports changes
Model
View Controller
reports changes
Model
● Search values
M M
Model Layer
M
Domain Object
DO DO Domain Layer
View
JComponent
Controller
Swing Model(s)
Painting
JCheckBox
Event
Handling
MetalCheckBoxUI
ToggleButtonModel
Change-
Listener MouseListener
Event
Handling MouseMotionListener
FocusListener
MetalCheckBoxUI
PropertyChangeListener
ToggleButtonModel
ChangeListener
ToggleButtonModel
ToggleButtonModel
anAlbum
title=”Preludes” Domain object
classical=true
aToggleButtonModel
connect,
synchronize
anAlbum
title=”Preludes” Domain Object
classical=true
aToggleButtonModel
selected=false
anAlbum
1. Read title=”Preludes”
#isClassical classical=true
aToggleButtonModel
anAlbum
title=”Preludes”
classical=true
anAlbum
2. Write
title=”Preludes”
classical=false #setClassical
aToggleButtonModel
selected=true
anAlbum
title=”Preludes”
classical=true 3. Value changed
classicalBox.setSelected(
anAlbum.isClassical());
titleField.setText(
anAlbum.getTitle());
...
}
anAlbum.setClassical(
classicalBox.isSelected();
anAlbum.setTitle(
titleField.getText();
...
}
aToggleButtonModel
selected=false
anAlbum
title=”Preludes”
classical=true
Note: you can't share the model :: JGOODIES :: Java User Interface Design
Direct Adapter: TableModel
aJTable
implements TableModel
myAlbumTableAdapter
Converts Album properties
to TableModel
aBook
aBook
title=”Swing”
anAlbum
title=”Swing”
available=true
title=”Preludes”
available=true
classical=true
implements ToggleButtonModel
anAlbumClassical-
ToggleButtonAdapter
Converts Album#classical
to ToggleButtonModel
anAlbum
title=”Preludes”
classical=true
aToggleButtonAdapter
aValueModel
anAlbum
title=”Preludes”
classical=true
Object getValue();
PropertyChangeEvent
provides the old and new value;
both can be null
implements
aToggleButtonAdapter ToggleButtonModel
aPropertyAdapter
ValueModel
propertyName=”classical” implements
anAlbum
title=”Preludes”
classical=true
aToggleButtonAdapter
aPropertyAdapter
ValueModel
propertyName=”classical”
implements
anAlbum (Bean)
title=”Preludes” get/set
classical=true
ValueModel aValueModel =
new PropertyAdapter(album, “classical”);
JCheckBox classicalBox =
ComponentFactory.createCheckBox(
album,
Album.PROPERTYNAME_CLASSICAL);
}
aBufferedValueModel implements
ValueModel
aPropertyAdapter implements
anAlbum
classical=true
aToggleButtonAdapter aDocumentAdapter
aTrigger-
Channel
aBufferedValueModel aBufferedValueModel
aPropertyAdapter aPropertyAdapter
anAlbum
title=”Preludes”
classical=true
aDocumentAdapter aPropertyConnector
aPropertyAdapter aPropertyAdapter
for Album#title for Album#releaseDate
anAlbum
title=”Preludes”
releaseDate=Dec-5-1967
aPropertyAdapter aPropertyAdapter
for Album#releaseDate for Album#releaseDate
anAlbum
releaseDate=Dec-5-1967
aToggleButtonAdapter aDocumentAdapter
aPropertyAdapter aPropertyAdapter
propertyName=”classical” Holds the propertyName=”title”
edited album
aBeanChannel
anAlbum anAlbum
title=”Preludes” title=”Etudes”
classical=true classical=true
aToggleButtonAdapter aDocumentAdapter
aPropertyAdapter aPropertyAdapter
propertyName=”classical” Holds the propertyName=”title”
edited album
aBeanChannel
anAlbum anAlbum
title=”Preludes” title=”Etudes”
classical=true classical=true
ListModel
anArrayListModel implements
List
aBook
aBook
anAlbum
implements
anAlbumTableAdapter TableModel
ListModel
anArrayListModel implements
List
aBook
aBook
anAlbum
SelectionAdapter
aSelectionInList
ListModel SelectionHolder
aBook
aBook
anAlbum
aDocumentAdapter
SelectionAdapter
aPropertyAdapter
aSelectionInList propertyName=”title”
ListModel SelectionHolder
beanChannel
aBook
aBook
anAlbum
Model Layer
ListModel Action ValueModel
Domain Object
Domain Layer
DO DO
JList
Model Layer
ListModel Action ValueModel
JButton
#enabled Updates button property
if the Action has changed
ButtonActionPropertyChangeListener
#enabled
JList
Can provide
GUI Model ValueModel bound bean properties
Album
#title Domain Layer
#artist
#classical
2) Constructors
// UI components
private JTextField titleField;
private JCheckBox classicalBox;
private JButton buyNowButton;
private JList referencesList;
...
...
:: JGOODIES :: Java User Interface Design
Example View 6/7
PanelBuilder builder =
new PanelBuilder(layout);
CellConstraints cc = new CellConstraints();
return builder.getPanel();
}
:: JGOODIES :: Java User Interface Design
Example View 7/7
/* Listens to #composerEnabled,
changes #enabled of the composerField. */
private class ComposerEnablementHandler
implements PropertyChangeListener {
composerField.setEnabled(
model.isComposerEnabled());
}
}
Adapter Binding:
– increases learning costs
– decreases production costs a little
– can significantly reduce change costs
Good Luck!