Design Patterns Java™ Workbook

(Michael S) #1
Chapter 3. Adapter

package com.oozinoz.applications;
import java.awt.;
import javax.swing.
;
import java.awt.event.;
import com.oozinoz.fireworks.
;
import com.oozinoz.units.*;
public class ShowRocketTable implements UnitConstants
{
//....
}


The main() method of ShowRocketTable sets up default table fonts, creates a Swing
table, and displays it:


public static void main(String[] args)
{
setFonts();
JTable t = new JTable(getRocketTable());
t.setRowHeight(36);
JScrollPane jsp = new JScrollPane(t);
jsp.setPreferredSize(
new java.awt.Dimension(300, 100));
display(jsp, " Rockets");
}


The setFonts() methods establishes the fonts to use for table cells and column headers:


private static void setFonts()
{
Font f = new Font("Dialog", Font.PLAIN, 18);
UIManager.put("Table.font", f);
UIManager.put("TableHeader.font", f);
}


The getRocketTable() method returns a RocketTable object that fulfills the role of a
TableModel. Essentially, the RocketTable object adapts a (row, column) request into a
(rocket, attribute) request:


private static RocketTable getRocketTable()
{
Rocket r1 = new Rocket(
"Shooter", 3.95, (Length) METER.times(50));
Rocket r2 = new Rocket(
"Orbit", 29.95, (Length) METER.times(5000));
return new RocketTable(new Rocket[] { r1, r2 });
}


The display() method contains reusable code:

Free download pdf