| |
"Trimming the Fat from Swing"
Vol. 8, Issue 7, p. 34
Listing 1
1 Thread worker = new Thread() {
2 public void run() {
3 //Do the database work here
4 Runnable r = new Runnable() {
5 public void run() {
6 //Update the GUI with
the data
7 }
8 };
9 SwingUtilities.invokeLater(r);
10 }
11 };
12 worker.start();
Listing 2
1 jButton1.setText("jButton1");
2 this.getContentPane().setLayout(
gridBagLayout1);
3 jButton2.setText("jButton2");
4 this.getContentPane().add(jButton1,
new GridBagConstraints(0, 1, 1,
1, 1.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE, new
Insets(0, 0, 0, 0), 0, 0));
5 this.getContentPane().add(jButton2,
new GridBagConstraints(1, 1, 1,
1, 1.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0));
6 this.getContentPane().add(jTable1,
new GridBagConstraints(0, 0, 2,
1, 1.0, 1.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
Listing 3
1 jButton1.setText("jButton1");
2 jButton2.setText("jButton2");
3 getContentPane().setLayout(
new GridBagLayout());
4 GridBagConstraints gbc =
new GridBagConstraints();
5 gbc.gridx =
GridBagConstraints.RELATIVE;
6 gbc.gridy = 0;
7 gbc.gridwidth = 2;
8 gbc.weightx = 1.0;
9 gbc.weighty = 1.0;
10 getContentPane().add(jTable1, gbc);
11 gbc.gridy++;
12 gbc.gridwidth=1;
13 gbc.weightx = 0.0;
14 gbc.weighty = 0.0;
15 getContentPane().add(jButton1,
gbc);
16 getContentPane().add(jButton2,
gbc);
Listing 4
1 public class ExampleTM
extends AbstractTableModel {
2 private String columnNames[] =
{"Widget Name", "Cost",
"Available", "Quantity"};
3 private Vector widgets;
4 public ExampleTM(Vector w) {
5 widgets = w;
6 }
7 public Class getColumnClass(
int c) {
8 switch (c) {
9 case 0:
10 return String.class;
11 case 1:
12 return Double.class;
13 case 2:
14 return Boolean.class;
15 case 3:
16 return int.class;
17 }
18 }
19 public int getColumnCount() {
20 return 4;
21 }
22 public int getColumnName(
int c) {
23 return columnNames[c];
24 }
25 public int getRowCount() {
26 return widgets.size();
27 }
28 public Object getValueAt(int r,
int c) {
29 Widget w = (Widget)
widgets.elementAt(r);
30 switch (c){
31 case 0:
32 return w.getName();
33 case 1:
34 return w.getCost();
35 case 2:
36 return w.isAvailable();
37 case 3:
38 return w.getQuantity();
39 default:
40 return null;
41 }
42 }
43 public boolean isCellEditable(
int r, int c) {
44 return false;
45 }
46 }
|
|