KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > swingset > TableDemo


1 /*
2  * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * -Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * -Redistribution in binary form must reproduct the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the distribution.
14  *
15  * Neither the name of Sun Microsystems, Inc. or the names of contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * This software is provided "AS IS," without a warranty of any kind. ALL
20  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
23  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
26  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30  *
31  * You acknowledge that Software is not designed, licensed or intended for
32  * use in the design, construction, operation or maintenance of any nuclear
33  * facility.
34  */

35
36 /*
37  * @(#)TableDemo.java 1.12 03/01/23
38  */

39 package demo.swingset;
40
41 import swingwtx.swing.*;
42 import swingwtx.swing.event.*;
43 import swingwtx.swing.text.*;
44 import swingwtx.swing.table.*;
45 import swingwtx.swing.border.*;
46 import swingwtx.swing.colorchooser.*;
47 import swingwtx.swing.filechooser.*;
48 import javax.accessibility.*;
49
50 import swingwt.awt.*;
51 import swingwt.awt.event.*;
52 import java.beans.*;
53 import java.util.*;
54 import java.io.*;
55 import java.applet.*;
56 import java.net.*;
57
58 /**
59  * Table demo
60  *
61  * @version 1.12 01/23/03
62  * @author Philip Milne
63  * @author Steve Wilson
64  */

65 public class TableDemo extends DemoModule {
66     JTable tableView;
67     JScrollPane scrollpane;
68     Dimension origin = new Dimension(0, 0);
69     
70     JCheckBox isColumnReorderingAllowedCheckBox;
71     JCheckBox showHorizontalLinesCheckBox;
72     JCheckBox showVerticalLinesCheckBox;
73
74     JCheckBox isColumnSelectionAllowedCheckBox;
75     JCheckBox isRowSelectionAllowedCheckBox;
76     JCheckBox isRowAndColumnSelectionAllowedCheckBox;
77
78     JLabel interCellSpacingLabel;
79     JLabel rowHeightLabel;
80
81     JSlider interCellSpacingSlider;
82     JSlider rowHeightSlider;
83
84     JComboBox selectionModeComboBox = null;
85     JComboBox resizeModeComboBox = null;
86
87     JPanel controlPanel;
88     JScrollPane tableAggregate;
89
90     String JavaDoc path = "ImageClub/food/";
91
92     final int INITIAL_ROWHEIGHT = 33;
93
94     /**
95      * main method allows us to run as a standalone demo.
96      */

97     public static void main(String JavaDoc[] args) {
98     TableDemo demo = new TableDemo(null);
99     demo.mainImpl();
100     }
101
102     /**
103      * TableDemo Constructor
104      */

105     public TableDemo(SwingSet2 swingset) {
106     super(swingset, "TableDemo", "toolbar/JTable.gif");
107     
108     getDemoPanel().setLayout(new BorderLayout());
109     controlPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
110     JPanel column1 = new JPanel (new ColumnLayout() );
111     JPanel column2 = new JPanel (new ColumnLayout() );
112     JPanel column3 = new JPanel (new ColumnLayout() );
113     
114     getDemoPanel().add(controlPanel, BorderLayout.NORTH);
115     Vector relatedComponents = new Vector();
116     
117     // start column 1
118
isColumnReorderingAllowedCheckBox = new JCheckBox(getString("TableDemo.reordering_allowed"), true);
119         column1.add(isColumnReorderingAllowedCheckBox);
120         isColumnReorderingAllowedCheckBox.addActionListener(new ActionListener() {
121         public void actionPerformed(ActionEvent e) {
122             boolean flag = ((JCheckBox)e.getSource()).isSelected();
123                 tableView.getTableHeader().setReorderingAllowed(flag);
124                 tableView.repaint();
125         }
126         });
127     
128     
129         showHorizontalLinesCheckBox = new JCheckBox(getString("TableDemo.horz_lines"), true);
130         column1.add(showHorizontalLinesCheckBox);
131         showHorizontalLinesCheckBox.addActionListener(new ActionListener() {
132         public void actionPerformed(ActionEvent e) {
133             boolean flag = ((JCheckBox)e.getSource()).isSelected();
134                 tableView.setShowHorizontalLines(flag); ;
135                 tableView.repaint();
136         }
137         });
138     
139         showVerticalLinesCheckBox = new JCheckBox(getString("TableDemo.vert_lines"), true);
140         column1.add(showVerticalLinesCheckBox);
141         showVerticalLinesCheckBox.addActionListener(new ActionListener() {
142         public void actionPerformed(ActionEvent e) {
143             boolean flag = ((JCheckBox)e.getSource()).isSelected();
144                 tableView.setShowVerticalLines(flag); ;
145                 tableView.repaint();
146         }
147         });
148
149     // Show that showHorizontal/Vertical controls are related
150
relatedComponents.removeAllElements();
151     relatedComponents.add(showHorizontalLinesCheckBox);
152     relatedComponents.add(showVerticalLinesCheckBox);
153     buildAccessibleGroup(relatedComponents);
154     
155         interCellSpacingLabel = new JLabel(getString("TableDemo.intercell_spacing_colon"));
156     column1.add(interCellSpacingLabel);
157     
158         interCellSpacingSlider = new JSlider(JSlider.HORIZONTAL, 0, 10, 1);
159     //interCellSpacingSlider.getAccessibleContext().setAccessibleName(getString("TableDemo.intercell_spacing"));
160
interCellSpacingLabel.setLabelFor(interCellSpacingSlider);
161         column1.add(interCellSpacingSlider);
162         interCellSpacingSlider.addChangeListener(new ChangeListener() {
163         public void stateChanged(ChangeEvent e) {
164             int spacing = ((JSlider)e.getSource()).getValue();
165                 tableView.setIntercellSpacing(new Dimension(spacing, spacing));
166                 tableView.repaint();
167         }
168         });
169     
170         controlPanel.add(column1);
171     
172     // start column 2
173

174     isColumnSelectionAllowedCheckBox = new JCheckBox(getString("TableDemo.column_selection"), false);
175         column2.add(isColumnSelectionAllowedCheckBox);
176         isColumnSelectionAllowedCheckBox.addActionListener(new ActionListener() {
177         public void actionPerformed(ActionEvent e) {
178             boolean flag = ((JCheckBox)e.getSource()).isSelected();
179                 tableView.setColumnSelectionAllowed(flag); ;
180                 tableView.repaint();
181         }
182         });
183     
184         isRowSelectionAllowedCheckBox = new JCheckBox(getString("TableDemo.row_selection"), true);
185         column2.add(isRowSelectionAllowedCheckBox);
186         isRowSelectionAllowedCheckBox.addActionListener(new ActionListener() {
187         public void actionPerformed(ActionEvent e) {
188             boolean flag = ((JCheckBox)e.getSource()).isSelected();
189                 tableView.setRowSelectionAllowed(flag); ;
190                 tableView.repaint();
191         }
192         });
193
194     // Show that row/column selections are related
195
relatedComponents.removeAllElements();
196     relatedComponents.add(isColumnSelectionAllowedCheckBox);
197     relatedComponents.add(isRowSelectionAllowedCheckBox);
198     buildAccessibleGroup(relatedComponents);
199
200         isRowAndColumnSelectionAllowedCheckBox = new JCheckBox(getString("TableDemo.cell_selection"), false);
201         column2.add(isRowAndColumnSelectionAllowedCheckBox);
202         isRowAndColumnSelectionAllowedCheckBox.addActionListener(new ActionListener() {
203         public void actionPerformed(ActionEvent e) {
204             boolean flag = ((JCheckBox)e.getSource()).isSelected();
205                 tableView.setCellSelectionEnabled(flag); ;
206                 tableView.repaint();
207         }
208         });
209
210         rowHeightLabel = new JLabel(getString("TableDemo.row_height_colon"));
211     column2.add(rowHeightLabel);
212
213         rowHeightSlider = new JSlider(JSlider.HORIZONTAL, 5, 100, INITIAL_ROWHEIGHT);
214     //rowHeightSlider.getAccessibleContext().setAccessibleName(getString("TableDemo.row_height"));
215
rowHeightLabel.setLabelFor(rowHeightSlider);
216         column2.add(rowHeightSlider);
217         rowHeightSlider.addChangeListener(new ChangeListener() {
218         public void stateChanged(ChangeEvent e) {
219             int height = ((JSlider)e.getSource()).getValue();
220                 tableView.setRowHeight(height);
221                 tableView.repaint();
222         }
223         });
224
225         controlPanel.add(column2);
226         
227     // Show that spacing controls are related
228
relatedComponents.removeAllElements();
229     relatedComponents.add(interCellSpacingSlider);
230     relatedComponents.add(rowHeightSlider);
231     buildAccessibleGroup(relatedComponents);
232
233         // Create the table.
234
tableAggregate = createTable();
235         getDemoPanel().add(tableAggregate, BorderLayout.CENTER);
236
237
238         // ComboBox for selection modes.
239
JPanel selectMode = new JPanel();
240         column3.setLayout(new ColumnLayout());
241         selectMode.setBorder(new TitledBorder(getString("TableDemo.selection_mode")));
242
243
244         selectionModeComboBox = new JComboBox();
245         selectionModeComboBox.addItem(getString("TableDemo.single"));
246         selectionModeComboBox.addItem(getString("TableDemo.one_range"));
247         selectionModeComboBox.addItem(getString("TableDemo.multiple_ranges"));
248         
249         selectionModeComboBox.addItemListener(new ItemListener() {
250         public void itemStateChanged(ItemEvent e) {
251             JComboBox source = (JComboBox)e.getSource();
252                 tableView.setSelectionMode(source.getSelectedIndex());
253         }
254         });
255
256     selectMode.add(selectionModeComboBox);
257         column3.add(selectMode);
258
259         // Combo box for table resize mode.
260

261     JPanel resizeMode = new JPanel();
262
263     resizeMode.setBorder(new TitledBorder(getString("TableDemo.autoresize_mode")));
264
265
266         resizeModeComboBox = new JComboBox();
267         resizeModeComboBox.addItem(getString("TableDemo.off"));
268         resizeModeComboBox.addItem(getString("TableDemo.column_boundries"));
269         resizeModeComboBox.addItem(getString("TableDemo.subsequent_columns"));
270         resizeModeComboBox.addItem(getString("TableDemo.last_column"));
271         resizeModeComboBox.addItem(getString("TableDemo.all_columns"));
272         resizeModeComboBox.setSelectedIndex(tableView.getAutoResizeMode());
273         resizeModeComboBox.addItemListener(new ItemListener() {
274         public void itemStateChanged(ItemEvent e) {
275             JComboBox source = (JComboBox)e.getSource();
276                 tableView.setAutoResizeMode(source.getSelectedIndex());
277         }
278         });
279
280     resizeMode.add(resizeModeComboBox);
281         column3.add(resizeMode);
282
283         controlPanel.add(column3);
284
285     setTableControllers(); // Set accessibility information
286

287     } // TableDemo()
288

289     /**
290      * Sets the Accessibility MEMBER_OF property to denote that
291      * these components work together as a group. Each object
292      * is set to be a MEMBER_OF an array that contains all of
293      * the objects in the group, including itself.
294      *
295      * @param components The list of objects that are related
296      */

297     void buildAccessibleGroup(Vector components) {
298
299     AccessibleContext context = null;
300     int numComponents = components.size();
301     Object JavaDoc[] group = components.toArray();
302     Object JavaDoc object = null;
303     for (int i = 0; i < numComponents; ++i) {
304         object = components.elementAt(i);
305         //if (object instanceof Accessible) {
306
//context = ((Accessible)components.elementAt(i)).
307
getAccessibleContext();
308         //context.getAccessibleRelationSet().add(
309
// new AccessibleRelation(
310
// AccessibleRelation.MEMBER_OF, group));
311
//}
312
}
313     } // buildAccessibleGroup()
314

315     /**
316      * This sets CONTROLLER_FOR on the controls that manipulate the
317      * table and CONTROLLED_BY relationships on the table to point
318      * back to the controllers.
319      */

320     private void setTableControllers() {
321
322     // Set up the relationships to show what controls the table
323
setAccessibleController(isColumnReorderingAllowedCheckBox,
324                 tableAggregate);
325     setAccessibleController(showHorizontalLinesCheckBox,
326                 tableAggregate);
327     setAccessibleController(showVerticalLinesCheckBox,
328                 tableAggregate);
329     setAccessibleController(isColumnSelectionAllowedCheckBox,
330                 tableAggregate);
331     setAccessibleController(isRowSelectionAllowedCheckBox,
332                 tableAggregate);
333     setAccessibleController(interCellSpacingSlider,
334                 tableAggregate);
335     setAccessibleController(rowHeightSlider,
336                 tableAggregate);
337     setAccessibleController(selectionModeComboBox,
338                 tableAggregate);
339     setAccessibleController(resizeModeComboBox,
340                 tableAggregate);
341     } // setTableControllers()
342

343     /**
344      * Sets up accessibility relationships to denote that one
345      * object controls another. The CONTROLLER_FOR property is
346      * set on the controller object, and the CONTROLLED_BY
347      * property is set on the target object.
348      */

349     private void setAccessibleController(JComponent controller,
350                     JComponent target) {
351     //AccessibleRelationSet controllerRelations =
352
// controller.getAccessibleContext().getAccessibleRelationSet();
353
//AccessibleRelationSet targetRelations =
354
// target.getAccessibleContext().getAccessibleRelationSet();
355

356     //controllerRelations.add(
357
// new AccessibleRelation(
358
// AccessibleRelation.CONTROLLER_FOR, target));
359
//targetRelations.add(
360
// new AccessibleRelation(
361
// AccessibleRelation.CONTROLLED_BY, controller));
362
} // setAccessibleController()
363

364     public JScrollPane createTable() {
365
366         // final
367
final String JavaDoc[] names = {
368       getString("TableDemo.first_name"),
369       getString("TableDemo.last_name"),
370       getString("TableDemo.favorite_color"),
371       getString("TableDemo.favorite_movie"),
372       getString("TableDemo.favorite_number"),
373       getString("TableDemo.favorite_food")
374     };
375
376     ImageIcon apple = createImageIcon("ImageClub/food/apple.jpg", getString("TableDemo.apple"));
377     ImageIcon asparagus = createImageIcon("ImageClub/food/asparagus.jpg", getString("TableDemo.asparagus"));
378     ImageIcon banana = createImageIcon("ImageClub/food/banana.jpg", getString("TableDemo.banana"));
379     ImageIcon broccoli = createImageIcon("ImageClub/food/broccoli.jpg", getString("TableDemo.broccoli"));
380     ImageIcon cantaloupe = createImageIcon("ImageClub/food/cantaloupe.jpg", getString("TableDemo.cantaloupe"));
381     ImageIcon carrot = createImageIcon("ImageClub/food/carrot.jpg", getString("TableDemo.carrot"));
382     ImageIcon corn = createImageIcon("ImageClub/food/corn.jpg", getString("TableDemo.corn"));
383     ImageIcon grapes = createImageIcon("ImageClub/food/grapes.jpg", getString("TableDemo.grapes"));
384     ImageIcon grapefruit = createImageIcon("ImageClub/food/grapefruit.jpg", getString("TableDemo.grapefruit"));
385     ImageIcon kiwi = createImageIcon("ImageClub/food/kiwi.jpg", getString("TableDemo.kiwi"));
386     ImageIcon onion = createImageIcon("ImageClub/food/onion.jpg", getString("TableDemo.onion"));
387     ImageIcon pear = createImageIcon("ImageClub/food/pear.jpg", getString("TableDemo.pear"));
388     ImageIcon peach = createImageIcon("ImageClub/food/peach.jpg", getString("TableDemo.peach"));
389     ImageIcon pepper = createImageIcon("ImageClub/food/pepper.jpg", getString("TableDemo.pepper"));
390     ImageIcon pickle = createImageIcon("ImageClub/food/pickle.jpg", getString("TableDemo.pickle"));
391     ImageIcon pineapple = createImageIcon("ImageClub/food/pineapple.jpg", getString("TableDemo.pineapple"));
392     ImageIcon raspberry = createImageIcon("ImageClub/food/raspberry.jpg", getString("TableDemo.raspberry"));
393     ImageIcon sparegrass = createImageIcon("ImageClub/food/asparagus.jpg", getString("TableDemo.sparegrass"));
394     ImageIcon strawberry = createImageIcon("ImageClub/food/strawberry.jpg", getString("TableDemo.strawberry"));
395     ImageIcon tomato = createImageIcon("ImageClub/food/tomato.jpg", getString("TableDemo.tomato"));
396     ImageIcon watermelon = createImageIcon("ImageClub/food/watermelon.jpg", getString("TableDemo.watermelon"));
397
398     NamedColor aqua = new NamedColor(new Color(127, 255, 212), getString("TableDemo.aqua"));
399     NamedColor beige = new NamedColor(new Color(245, 245, 220), getString("TableDemo.beige"));
400     NamedColor black = new NamedColor(Color.black, getString("TableDemo.black"));
401     NamedColor blue = new NamedColor(new Color(0, 0, 222), getString("TableDemo.blue"));
402     NamedColor eblue = new NamedColor(Color.blue, getString("TableDemo.eblue"));
403     NamedColor jfcblue = new NamedColor(new Color(204, 204, 255), getString("TableDemo.jfcblue"));
404     NamedColor jfcblue2 = new NamedColor(new Color(153, 153, 204), getString("TableDemo.jfcblue2"));
405     NamedColor cybergreen = new NamedColor(Color.green.darker().brighter(), getString("TableDemo.cybergreen"));
406     NamedColor darkgreen = new NamedColor(new Color(0, 100, 75), getString("TableDemo.darkgreen"));
407     NamedColor forestgreen = new NamedColor(Color.green.darker(), getString("TableDemo.forestgreen"));
408     NamedColor gray = new NamedColor(Color.gray, getString("TableDemo.gray"));
409     NamedColor green = new NamedColor(Color.green, getString("TableDemo.green"));
410     NamedColor orange = new NamedColor(new Color(255, 165, 0), getString("TableDemo.orange"));
411     NamedColor purple = new NamedColor(new Color(160, 32, 240), getString("TableDemo.purple"));
412     NamedColor red = new NamedColor(Color.red, getString("TableDemo.red"));
413     NamedColor rustred = new NamedColor(Color.red.darker(), getString("TableDemo.rustred"));
414     NamedColor sunpurple = new NamedColor(new Color(100, 100, 255), getString("TableDemo.sunpurple"));
415     NamedColor suspectpink = new NamedColor(new Color(255, 105, 180), getString("TableDemo.suspectpink"));
416     NamedColor turquoise = new NamedColor(new Color(0, 255, 255), getString("TableDemo.turquoise"));
417     NamedColor violet = new NamedColor(new Color(238, 130, 238), getString("TableDemo.violet"));
418     NamedColor yellow = new NamedColor(Color.yellow, getString("TableDemo.yellow"));
419
420         // Create the dummy data (a few rows of names)
421
final Object JavaDoc[][] data = {
422       {"Mike", "Albers", green, getString("TableDemo.brazil"), new Double JavaDoc(44.0), strawberry},
423       {"Mark", "Andrews", blue, getString("TableDemo.curse"), new Double JavaDoc(3), grapes},
424       {"Brian", "Beck", black, getString("TableDemo.bluesbros"), new Double JavaDoc(2.7182818285), raspberry},
425       {"Lara", "Bunni", red, getString("TableDemo.airplane"), new Double JavaDoc(15), strawberry},
426       {"Roger", "Brinkley", blue, getString("TableDemo.man"), new Double JavaDoc(13), peach},
427       {"Brent", "Christian", black, getString("TableDemo.bladerunner"), new Double JavaDoc(23), broccoli},
428       {"Mark", "Davidson", darkgreen, getString("TableDemo.brazil"), new Double JavaDoc(27), asparagus},
429       {"Jeff", "Dinkins", blue, getString("TableDemo.ladyvanishes"), new Double JavaDoc(8), kiwi},
430       {"Ewan", "Dinkins", yellow, getString("TableDemo.bugs"), new Double JavaDoc(2), strawberry},
431       {"Amy", "Fowler", violet, getString("TableDemo.reservoir"), new Double JavaDoc(3), raspberry},
432       {"Hania", "Gajewska", purple, getString("TableDemo.jules"), new Double JavaDoc(5), raspberry},
433       {"David", "Geary", blue, getString("TableDemo.pulpfiction"), new Double JavaDoc(3), watermelon},
434 // {"James", "Gosling", pink, getString("TableDemo.tennis"), new Double(21), donut},
435
{"Eric", "Hawkes", blue, getString("TableDemo.bladerunner"), new Double JavaDoc(.693), pickle},
436           {"Shannon", "Hickey", green, getString("TableDemo.shawshank"), new Integer JavaDoc(2), grapes},
437       {"Earl", "Johnson", green, getString("TableDemo.pulpfiction"), new Double JavaDoc(8), carrot},
438       {"Robi", "Kahn", green, getString("TableDemo.goodfellas"), new Double JavaDoc(89), apple},
439       {"Robert", "Kim", blue, getString("TableDemo.mohicans"), new Double JavaDoc(655321), strawberry},
440       {"Janet", "Koenig", turquoise, getString("TableDemo.lonestar"), new Double JavaDoc(7), peach},
441       {"Jeff", "Kesselman", blue, getString("TableDemo.stuntman"), new Double JavaDoc(17), pineapple},
442       {"Onno", "Kluyt", orange, getString("TableDemo.oncewest"), new Double JavaDoc(8), broccoli},
443       {"Peter", "Korn", sunpurple, getString("TableDemo.musicman"), new Double JavaDoc(12), sparegrass},
444
445       {"Rick", "Levenson", black, getString("TableDemo.harold"), new Double JavaDoc(1327), raspberry},
446       {"Brian", "Lichtenwalter", jfcblue, getString("TableDemo.fifthelement"), new Double JavaDoc(22), pear},
447       {"Malini", "Minasandram", beige, getString("TableDemo.joyluck"), new Double JavaDoc(9), corn},
448       {"Michael", "Martak", green, getString("TableDemo.city"), new Double JavaDoc(3), strawberry},
449       {"David", "Mendenhall", forestgreen, getString("TableDemo.schindlerslist"), new Double JavaDoc(7), peach},
450       {"Phil", "Milne", suspectpink, getString("TableDemo.withnail"), new Double JavaDoc(3), banana},
451       {"Lynn", "Monsanto", cybergreen, getString("TableDemo.dasboot"), new Double JavaDoc(52), peach},
452       {"Hans", "Muller", rustred, getString("TableDemo.eraserhead"), new Double JavaDoc(0), pineapple},
453           {"Joshua", "Outwater", blue, getString("TableDemo.labyrinth"), new Integer JavaDoc(3), pineapple},
454       {"Tim", "Prinzing", blue, getString("TableDemo.firstsight"), new Double JavaDoc(69), pepper},
455       {"Raj", "Premkumar", jfcblue2, getString("TableDemo.none"), new Double JavaDoc(7), broccoli},
456       {"Howard", "Rosen", green, getString("TableDemo.defending"), new Double JavaDoc(7), strawberry},
457       {"Ray", "Ryan", black, getString("TableDemo.buckaroo"),
458        new Double JavaDoc(3.141592653589793238462643383279502884197169399375105820974944), banana},
459       {"Georges", "Saab", aqua, getString("TableDemo.bicycle"), new Double JavaDoc(290), cantaloupe},
460       {"Tom", "Santos", blue, getString("TableDemo.spinaltap"), new Double JavaDoc(241), pepper},
461       {"Rich", "Schiavi", blue, getString("TableDemo.repoman"), new Double JavaDoc(0xFF), pepper},
462       {"Nancy", "Schorr", green, getString("TableDemo.fifthelement"), new Double JavaDoc(47), watermelon},
463       {"Keith", "Sprochi", darkgreen, getString("TableDemo.2001"), new Integer JavaDoc(13), watermelon},
464       {"Matt", "Tucker", eblue, getString("TableDemo.starwars"), new Integer JavaDoc(2), broccoli},
465       {"Dimitri", "Trembovetsky", red, getString("TableDemo.aliens"), new Integer JavaDoc(222), tomato},
466       {"Scott", "Violet", violet, getString("TableDemo.raiders"), new Integer JavaDoc(-97), banana},
467       {"Kathy", "Walrath", blue, getString("TableDemo.thinman"), new Integer JavaDoc(8), pear},
468       {"Nathan", "Walrath", black, getString("TableDemo.chusingura"), new Integer JavaDoc(3), grapefruit},
469       {"Steve", "Wilson", green, getString("TableDemo.raiders"), new Integer JavaDoc(7), onion},
470       {"Kathleen", "Zelony", gray, getString("TableDemo.dog"), new Integer JavaDoc(13), grapes}
471         };
472
473         // Create a model of the data.
474
TableModel dataModel = new AbstractTableModel() {
475             public int getColumnCount() { return names.length; }
476             public int getRowCount() { return data.length;}
477             public Object JavaDoc getValueAt(int row, int col) {return data[row][col];}
478             public String JavaDoc getColumnName(int column) {return names[column];}
479             public Class JavaDoc getColumnClass(int c) {return getValueAt(0, c).getClass();}
480         public boolean isCellEditable(int row, int col) {return col != 5;}
481             public void setValueAt(Object JavaDoc aValue, int row, int column) { data[row][column] = aValue; }
482          };
483
484
485         // Create the table
486
tableView = new JTable(dataModel);
487
488         // Show colors by rendering them in their own color.
489
DefaultTableCellRenderer colorRenderer = new DefaultTableCellRenderer() {
490         public void setValue(Object JavaDoc value) {
491             if (value instanceof NamedColor) {
492             NamedColor c = (NamedColor) value;
493                 setBackground(c);
494                 setForeground(c.getTextColor());
495                 setText(c.toString());
496         } else {
497             super.setValue(value);
498         }
499         }
500         };
501
502     // Create a combo box to show that you can use one in a table.
503
JComboBox comboBox = new JComboBox();
504     comboBox.addItem(aqua);
505     comboBox.addItem(beige);
506     comboBox.addItem(black);
507     comboBox.addItem(blue);
508     comboBox.addItem(eblue);
509     comboBox.addItem(jfcblue);
510     comboBox.addItem(jfcblue2);
511     comboBox.addItem(cybergreen);
512     comboBox.addItem(darkgreen);
513     comboBox.addItem(forestgreen);
514     comboBox.addItem(gray);
515     comboBox.addItem(green);
516     comboBox.addItem(orange);
517     comboBox.addItem(purple);
518     comboBox.addItem(red);
519     comboBox.addItem(rustred);
520     comboBox.addItem(sunpurple);
521     comboBox.addItem(suspectpink);
522     comboBox.addItem(turquoise);
523     comboBox.addItem(violet);
524     comboBox.addItem(yellow);
525
526         TableColumn colorColumn = tableView.getColumn(2);
527         // Use the combo box as the editor in the "Favorite Color" column.
528
colorColumn.setCellEditor(new DefaultCellEditor(comboBox));
529
530         colorRenderer.setHorizontalAlignment(JLabel.CENTER);
531         colorColumn.setCellRenderer(colorRenderer);
532
533         tableView.setRowHeight(INITIAL_ROWHEIGHT);
534
535         scrollpane = new JScrollPane(tableView);
536         return scrollpane;
537     }
538
539     class NamedColor extends Color {
540     String JavaDoc name;
541     public NamedColor(Color color, String JavaDoc name) {
542         super(color.getRed(), color.getGreen(), color.getBlue());
543         this.name = name;
544     }
545     
546     public Color getTextColor() {
547         int r = getRed();
548         int g = getGreen();
549         int b = getBlue();
550         if(r > 240 || g > 240) {
551         return Color.black;
552         } else {
553         return Color.white;
554         }
555     }
556     
557     public String JavaDoc toString() {
558         return name;
559     }
560     }
561     
562     class ColumnLayout implements LayoutManager {
563     int xInset = 5;
564     int yInset = 5;
565     int yGap = 2;
566     
567     public void addLayoutComponent(String JavaDoc s, Component c) {}
568     
569     public void layoutContainer(Container c) {
570         Insets insets = c.getInsets();
571         int height = yInset + insets.top;
572         
573         Component[] children = c.getComponents();
574         Dimension compSize = null;
575         for (int i = 0; i < children.length; i++) {
576         compSize = children[i].getPreferredSize();
577         //children[i].setSize(compSize.width, compSize.height);
578
//children[i].setLocation( xInset + insets.left, height);
579
children[i].setBounds(xInset + insets.left, height, compSize.width, compSize.height);
580         height += compSize.height + yGap;
581         }
582         
583     }
584     
585     public Dimension minimumLayoutSize(Container c) {
586         Insets insets = c.getInsets();
587         int height = yInset + insets.top;
588         int width = 0 + insets.left + insets.right;
589         
590         Component[] children = c.getComponents();
591         Dimension compSize = null;
592         for (int i = 0; i < children.length; i++) {
593         compSize = children[i].getPreferredSize();
594         height += compSize.height + yGap;
595         width = Math.max(width, compSize.width + insets.left + insets.right + xInset*2);
596         }
597         height += insets.bottom;
598         return new Dimension( width, height);
599     }
600     
601     public Dimension preferredLayoutSize(Container c) {
602         return minimumLayoutSize(c);
603     }
604     
605     public void removeLayoutComponent(Component c) {}
606     }
607     
608     
609 }
610
Popular Tags