Top Banner
SWING WITH STYLE Karsten Lentzsch – JGoodies
108

Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Mar 27, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

SWING WITH STYLE

Karsten Lentzsch – JGoodies

Page 2: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

JGoodies: Karsten Lentzsch

Open source Swing libraries

Example applications

Consultant for Java desktop

Design assistance

Training for visual design and implementation

Study GUI production process

Page 3: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Before

Page 4: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

After

Page 5: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Before

Page 6: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

After

Page 7: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

See success factors for Swing projects

How to implement Swing clients?

Learn criteria for good writing style

Goal

Page 8: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Agenda Visual Dos and Don’ts

Implementation Patterns

Writing Style

Visual Patterns

Page 9: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Visual Don‘ts

Color

Fonts

Icons

Page 10: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance
Page 11: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance
Page 12: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Visual Dos

Align

Remove unnecessary borders

Remove all unnecessary elements

Use white space

Learn about contrast, balance

Page 13: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance
Page 14: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance
Page 15: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Visual Dos and Don‘ts

For details see:

JGoodies.com -> Downloads -> Presentations

„First Aid for Swing UIs“

Page 16: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Agenda Visual Dos and Don’ts

Implementation Patterns

Writing Style

Visual Patterns

Page 17: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Example: Contact Manager

Page 18: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Contact Editor

Page 19: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Legend

Domain Object

Presentation Logic

Presentation (View)‏

Refers to Fires

Page 20: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 1

Separate domain data

Page 21: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Pattern: Separated Presentation

Domain

Presentation Logic

Presentation (View)

Page 22: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Domain Logic in Presentation

Business Logic

Presentation Logic

Presentation (View)

Domain

Page 23: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 1

Separate domain data

Separate domain logic

Consider object orientation

Page 24: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Domain Class

public class Contact {

private String givenName;

private String surname;

private String phone;

public String getPhone() {

return phone;

}

public void setPhone(String newPhone) {

phone = newPhone;

}

Page 25: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

With Bound Properties

public class Contact extends Bean {

public static final String PROPERTY_PHONE

= “phone”;

public void setPhone(String newPhone) {

String oldPhone = getPhone();

phone = newPhone;

firePropertyChange(

PROPERTY_PHONE, oldPhone, newPhone);

}

Page 26: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Pattern: Autonomous View

Presentation Logic Presentation (View)

Page 27: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Autonomous View I

public class ContactEditorDialog extends JDialog {

private final Contact contact;

private JTextField givenNameField;

...

public ContactEditorDialog(Contact contact) { ... }

private void initComponents() { ... }

private void initEventHandling() { ... }

private JComponent buildContent() { ... }

Page 28: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Autonomous View II

class OKAction extends AbstractAction {

private OKAction() {

super(“OK”);

}

public void actionPerformed(ActionEvent e) {

}

}

Page 29: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 2

Separate presentation from presentation logic

Page 30: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Separated Presentation Logic

Domain

Presentation Logic Presentation (View)

Page 31: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Pattern: Model-View-Presenter

Model

Presenter View

Page 32: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Pattern: Presentation Model

Domain

Presentation Model View View View

Page 33: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Action with Multiple Views

Action •Text •Icon •Enablement •Mnemonic

JButton JButton JMenuItem

Page 34: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

PM: Multiple Views

Presentation Model

Panel with List and Button

JList

ListModel

JButton

Action

Domain

PopupMenu

JMenuItem

Page 35: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Implementation Patterns

For details see:

JGoodies.com -> Downloads -> Presentations

„Desktop Patterns & Data Binding“

Page 36: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Agenda Visual Dos and Don’ts

Implementation Patterns

Writing Style

Visual Patterns

Page 37: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Contact Example

Contact

ContactEditorModel

ContactHomeView

ContactHomeModel

ContactEditorView

Page 38: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactEditorView

Page 39: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactEditorView (1/4)

final class ContactEditorView {

private final ContactEditorModel model;

private JTextField givenNameField;

private JTextField surnameField;

private JTextField phoneField;

private JButton okButton;

ContactEditorView(ContactEditorModel model) {

this.model = model;

initComponents();

initBindings();

}

Page 40: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 3

Build dialogs, frames, panels

Extend JDialog, JFrame, JPanel if necessary. Do you extend or use HashMap?

Page 41: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 4

Use view superclasses judicously

Page 42: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Too Many Superclasses

JGoodiesAbstractDialog

JGoodiesDefaultDialog

CompanyAbstractDialog

CompanyDefaultDialog

CompanyValidationDialog

ProjectDefaultDialog

Page 43: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 4

Use view superclasses judicously

Favor composition over inheritance

Consider interfaces or a single superclass for:

ensuring consistency

life-cycle

Page 44: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 5

Write for the reader

Reduce visibilities

Classes

Fields

Methods

Mark as final

Follow Joshua Blochs „Effective Java“ tips

Page 45: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactEditorView (2/4)

private void initComponents() {

JGComponentFactory factory =

JGComponentFactory.getCurrent();

givenNameField = factory.createTextField();

surnameField = factory.createTextField();

phoneField = factory.createPhoneField();

faxField = factory.createFaxField();

emailField = factory.createEmailField();

tagsField = factory.createTextField();

Options.setSelectOnFocusGainEnabled(

tagsField, false);

okButton = factory.createButton(); // No text

}

Page 46: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 6

Use a component factory

extensible

exchangeable

shared by multiple projects

project specific

Consider special component types, e.g. for: Email, Money, Power, Weight, Lengths, etc.

Page 47: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactEditorView (3/4)

private void initBindings() {

Binder binder = Binders.binderFor(model);

binder.bindBeanProperty(“givenName”)

.to(givenNameField);

binder.bindBeanProperty(“surname”)

.to(surnameField);

binder.bindBeanProperty(“phone”)

.to(phoneField);

binder.bindAction(“OK”).to(okButton);

}

Page 48: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactEditorView (3a/4)

private void initBindings() {

Binder binder = Binders.binderFor(model);

binder.bindBeanProperty(PROPERTY_GIVEN_NAME)

.to(givenNameField);

binder.bindBeanProperty(PROPERTY_SURNAME)

.to(surnameField);

binder.bindBeanProperty(PROPERTY_PHONE)

.to(phoneField);

binder.bindAction(ACTION_OK).to(okButton);

}

Page 49: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Contact Editor

Page 50: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactEditorView (3/4)

private JComponent buildContent() {

FormLayout layout = new FormLayout(

"pref, $lcgap, 74dlu, 2dlu, 74dlu",

"p, $lg, p, $lg, p, $lg, p, $lg, p");

PanelBuilder builder = new PanelBuilder(layout);

builder.addLabel("&Given name:", CC.xy (1, 1));

builder.add(givenNameField, CC.xyw(3, 1, 3));

builder.addLabel("&Surname:", CC.xy (1, 3));

builder.add(surnameField, CC.xyw(3, 3, 3));

builder.addLabel("&Phone, Fax:", CC.xy (1, 5));

builder.add(phoneField, CC.xy (3, 5));

builder.add(faxField, CC.xy (5, 5));

return builder.getPanel();

}

Page 51: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 7

Use a grid-based layout

MIGLayout

JGoodies FormLayout

Avoid 2-pass-code

Describe the grid first

Then fill it with components – without state

Page 52: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 8

Favor flat over nested layouts

Nest sublayouts that are not aligned

Write #build-methods for sublayouts

Page 53: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactEditorView (4/4)

void showDialog(EventObject e) {

PropertyPane pane = new PropertyPane(

buildContent(),

okButton, CommandValue.CANCEL);

pane.showDialog(e, “Contact Editor”);

}

Page 54: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 9

Separate content from decoration

Here:

Editor content

Property dialog command area, borders, etc.

Page 55: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Contact Editor

Page 56: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Contact Example

Contact

ContactEditorModel

ContactHomeView

ContactHomeModel

ContactEditorView

Page 57: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactEditorModel (1/2)

public final class ContactEditorModel

extends ActionPresentationModel<Contact> {

private final ContactService service;

private final EventBus eventBus;

public ContactEditorModel(

Contact contact,

ContactService service,

EventBus eventBus) {

super(contact);

}

Page 58: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 10

Illustrate the object context

Which objects are used?

Who reads/writes data?

How will changes be reported to others?

E.g. use Constructor Injection

Page 59: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactEditorModel

Page 60: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 11

Consider a shorthand for Actions

Know the @Action mechanism of the JSR 296 – Swing Application Framework

Page 61: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactEditorModel (2/2)

@Action

public void onOKPerformed(ActionEvent e) {

// Save the data

}

Page 62: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactEditorModel (2a/2)

@Action(text=“OK”)

public void onOKPerformed(ActionEvent e) {

// Commit pending edits

TextComponentUtils.commitImmediately();

// Save the data

}

Page 63: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 12

Know the JSR 296 – Swing App Framework

organizes, simplifies, standardizes

Resource management

Action management

Background tasks

Page 64: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Swing Application Framework

For details see:

JGoodies.com -> Downloads -> Presentations

„JSR 296 –Swing App Framework“

Page 65: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 13

Show clearly, which events are handled by the presentation logic

Use a shorthand similar to @Action

PropertyChangeListener

ListSelectionListener

Popup trigger

Double-click

Page 66: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Contact Overview

Page 67: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactHomeModel

Page 68: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactHomeModel

@ListSelectionListener

public void onListSelectionChanged(

ListSelectionEvent e) {

// Enable/disable the Edit and Delete Action

}

@DoubleClickListener

public void onDoubleClickPerformed(MouseEvent e) {

editItem(e, getSelection());

}

Page 69: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 14

Standardize the client source structure

Sections

(Method) names

Method order

API

Page 70: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactHomeModel

Page 71: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Duty

Perform time-consuming tasks outside the Event-Dispatch-Thread

I-/O-operations

Server access

Service calls

Page 72: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Duty

Perform time-consuming tasks outside the Event-Dispatch-Thread

I-/O-operations

Server access

Service calls

Page 73: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 15

Consider performing long tasks in the Event-Dispatch-Thread, if

most tasks perform in the twinkling of an eye

you cannot block the GUI [correctly]

your team doesn‘t master Swing‘s 1-thread-rule

Page 74: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 16

Write short

Favor clear over short code

Page 75: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

More vs. Less

private void initComponents() {

givenNameField = factory.createTextField();

}

private void initBindings() {

binder.bindBeanProperty(PROPERTY_GIVEN_NAME)

.to(givenNameField);

}

private void initComponents() {

givenNameField = bindingFactory

.createBoundTextField(PROPERTY_GIVEN_NAME);

}

Page 76: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Very Short

private JComponent buildContent() {

return BeanBuilder.createEditor(Contact.class);

}

Page 77: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 17

Avoid implicit operations, that

Nobody knows

Nobody understands

Nobody documents

Nobody maintains

You can‘t debug

Automatic data binding

Automatic form construction using reflection

Page 78: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 18

Use views, that can be shown without context

Already done in MVP

Possible in Presentation Model

Page 79: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Model Access in PM-View

private void initComponents() {

okButton = new JButton(

// NPE if model == null

model.getAction(ACTION_OK));

}

ContactEditorView(ContactEditorModel model) {

this.model = model;

initComponents();

if (model != null) {

initBindings();

}

}

Page 80: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 19

Write JavaDocs that increase the readability

Avoid repeating method names

Avoid empty tags

Follow standards (write in 3rd person, etc.)

Does the comment add value or blur sources?

Use static checks (by the IDE, CheckStyle)

Page 81: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Criteria for Good Style

Short

Simple, easy to understand

Stable against changes Pattern – MVP vs. PM

With or without visual editor

Component factory

Binding system

Layout

Toolkit

Language

Page 82: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 20: Clean Up

Factor out:

Domain data, business logic, services

Component construction

Action construction, Listener construction

Resource management

Event system (EventBus)

Mechanism for background tasks

Standard layouts

Standard dialogs

Page 83: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Agenda Visual Dos and Don’ts

Implementation Patterns

Writing Style

Visual Patterns

Page 84: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Contact Overview

Page 85: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactHomeView

Page 86: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactHomeView Panel Layout

private JComponent buildPanel() {

FormLayout layout = new FormLayout(

"fill:250dlu:grow",

"p, $lcg, fill:200dlu, $rgap, p");

PanelBuilder builder = new PanelBuilder(layout);

builder.addLabel("&Contacts:", CC.xy(1, 1));

builder.add(

new JScrollPane(contactsTable), CC.xy(1, 3));

builder.add(buildButtonBar(), CC.xy(1, 5));

return builder.getPanel();

}

Page 87: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactHomeView Button Bar I

private JComponent buildButtonBar() {

FormLayout layout = new FormLayout(

"pref, 4px, pref, 4px, pref",

"p");

PanelBuilder builder = new PanelBuilder(layout);

builder.add(newButton, CC.xy(1, 1));

builder.add(editButton, CC.xy(3, 1));

builder.add(deleteButton, CC.xy(5, 1));

return builder.getPanel();

}

Page 88: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactHomeView Button Bar II

private JComponent buildButtonBar() {

return new ButtonBarBuilder()

.addButton(newButton, editButton, deleteButton)

.getPanel();

}

Page 89: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Tip 21

Search for similar panels, subpanels, forms, …

Build it consistently with:

Panel building code (nested)

Grid filling code (embedded)

Visual components (Beans)

Page 90: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance
Page 91: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance
Page 92: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ListViewBuilder

private JComponent buildPanel() {

return new ListViewBuilder()

.title("&Contacts:")

.listView(contactsTable))

.listBar(newButton, editButton, deleteButton)

// .detailsView(…)

.build();

}

Page 93: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactHomeModel

@Action

public void onDeletePerformed(ActionEvent e) {

Contact selection = view.getSelection();

String objectName = selection.getDisplayString();

TaskPane pane = new TaskPane(

MessageType.QUESTION,

“Do you want to delete ” + objectName + “?”,

CommandValue.YES, CommandValue.NO);

pane.showDialog(e, “Delete Contact”);

if (pane.getCommitValue() == CommandValue.YES) {

// Delete the selection

}

}

Page 94: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance
Page 95: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance
Page 96: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance
Page 97: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance
Page 98: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance
Page 99: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Style Guide Compliance API

@ActionListener

public void onDeletePerformed(ActionEvent e) {

Contact selection = view.getSelection();

String objectName = selection.getDisplayString();

boolean proceed = MessagePanes.getCurrent()

.showConfirmation(e,

“Delete Contact”,

“Do you want to delete ” + objectName + “?”);

if (proceed) {

// Delete the selection

}

}

Page 100: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Class MessagePanes

#showError(…)

#showAwarenessWarning(…)

#showImminentProblem(…)

#showInformation(…)

#showConfirmation(…)

#showRiskyActionConfirmation(…)

#showHelp(…)

Page 101: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Standard Dialogs

@ActionListener

public void onDeletePerformed(ActionEvent e) {

Contact selection = view.getSelection();

String objectName = selection.getDisplayString();

boolean proceed = StandardPanes.getCurrent()

.showDeleteConfirmation(e, objectName);

if (proceed) {

// Delete the selection

}

}

Page 102: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Class StandardPanes

#showDeleteConfirmation(…)

#showRiskyDeleteConfirmation(…)

#showExitConfirmation(…)

#showSaveChangesConfirmation(…)

#showSaveAllChangesConfirmation(…)

#showNotYetImplemented(…)

#showNotYetTestedConfirmation(…)

Page 103: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

Visual Patterns (Meta Design)

For more information see:

JGoodies.com -> Downloads -> Presentations

„Efficient Swing Design“

Page 104: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

More Information

Martin Fowler: Further P of EAA

google "Organizing Presentation Logic"

Chris Ramsdale: Large scale application development and MVP

Page 105: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

More Information: Human Factors

See the video:

„Warum so viele kluge Leute so schlechte Oberflächen entwickeln“ – German only

„Why so many smart people develop poor GUIs“

Page 106: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

ContactHomeModel

Page 107: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

QUESTIONS AND ANSWERS

Page 108: Karsten Lentzsch JGoodies SWING WITH STYLE with Style.pdf · JGoodies: Karsten Lentzsch Open source Swing libraries Example applications Consultant for Java desktop Design assistance

SWING WITH STYLE

Karsten Lentzsch – JGoodies