Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Use high-level MVC and POJOs with Swing

An introduction to the TikeSwing framework

  • Print
  • Feedback

Page 3 of 6

  • Simple: A component is connected to a model field directly; for example, field1
  • Nested: A component is connected to a JavaBeans field inside the model; for example, field1.field2
  • Indexed: A component is connected to an array field inside the model; for example, myArray[1]
  • Mapped: A component is connected to a map field inside the model; for example, myHashMap("foo")
  • Combined: A component is connected to a field inside the model via combined notations; for example, field.myArray[1].myHashMap["foo"]

In addition to get and set methods in a model class, a view class must also contain a get method for every view component.

The following example is a view class for FindCustomerModel. The view uses TikeSwing components that extend base Swing classes (JLabel to YLabel, JTextField to YTextField, etc.). The code looks like a standard Swing view, only the setMVCNames() method contains TikeSwing-specific code. It sets up the component-model connection according to the principles described above. The resultTable columns are connected to POJO fields in the customers collection via YColumn objects. findButton doesn't show any data from the model, but the MVC name is set for TikeSwing event handling (described later).

 public class FindCustomerView extends YPanel {
        
    private YLabel idLabel = new YLabel("Id");

private YLabel nameLabel = new YLabel ("Name"); private YTextField idField = new YTextField(); private YTextField nameField = new YTextField(); private YPanel criteriaPanel = new YPanel(); private YTable resultTable = new YTable(); private YButton findButton = new YButton("Find"); public FindCustomerView () { addComponents(); setMVCNames(); } private void setMVCNames() { idField.getYProperty().put(YIComponent.MVC_NAME,"id"); nameField.getYProperty().put(YIComponent.MVC_NAME,"name"); resultTable.getYProperty().put(YIComponent.MVC_NAME,"customers"); findButton.getYProperty().put(YIComponent.MVC_NAME,"findButton"); YColumn[] columns = { new YColumn("id"), new YColumn("name")}; resultTable.setColumns(columns); } private void addComponents() { this.setLayout(new BorderLayout()); this.add(criteriaPanel, BorderLayout.NORTH); idField.setPreferredSize(new Dimension(100, 19)); nameField.setPreferredSize(new Dimension(100, 19)); criteriaPanel.add(idLabel); criteriaPanel.add(idField); criteriaPanel.add(nameLabel); criteriaPanel.add(nameField); criteriaPanel.add(findButton); this.add(resultTable, BorderLayout.CENTER); }

public YTextField getIdField() { return idField; } public YLabel getIdLabel() { return idLabel; } public YTextField getNameField() { return nameField; } public YLabel getNameLabel() { return nameLabel; } public YTable getResultTable() { return resultTable; } public YButton getFindButton() { return findButton; } }


Now every time the user edits the idField or the nameField, the changes are automatically updated to the model. Also, when notifyObservers() is called in the FindCustomerModel, the framework updates changes to the resultTable. Yet a controller must be specified to wire up the structure.

  • Print
  • Feedback

Resources