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  
40  
41  import javax.swing.*;
42  import javax.swing.event.*;
43  import javax.swing.text.*;
44  import javax.swing.table.*;
45  import javax.swing.border.*;
46  import javax.swing.colorchooser.*;
47  import javax.swing.filechooser.*;
48  import javax.accessibility.*;
49  
50  import java.awt.*;
51  import java.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 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[] 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     /*
201         isRowAndColumnSelectionAllowedCheckBox = new JCheckBox(getString("TableDemo.cell_selection"), false);
202         column2.add(isRowAndColumnSelectionAllowedCheckBox);
203         isRowAndColumnSelectionAllowedCheckBox.addActionListener(new ActionListener() {
204         public void actionPerformed(ActionEvent e) {
205             boolean flag = ((JCheckBox)e.getSource()).isSelected();
206                 tableView.setCellSelectionEnabled(flag); ;
207                 tableView.repaint();
208         }
209         });
210     */
211 
212         rowHeightLabel = new JLabel(getString("TableDemo.row_height_colon"));
213     column2.add(rowHeightLabel);
214 
215         rowHeightSlider = new JSlider(JSlider.HORIZONTAL, 5, 100, INITIAL_ROWHEIGHT);
216     rowHeightSlider.getAccessibleContext().setAccessibleName(getString("TableDemo.row_height"));
217     rowHeightLabel.setLabelFor(rowHeightSlider);
218         column2.add(rowHeightSlider);
219         rowHeightSlider.addChangeListener(new ChangeListener() {
220         public void stateChanged(ChangeEvent e) {
221             int height = ((JSlider)e.getSource()).getValue();
222                 tableView.setRowHeight(height);
223                 tableView.repaint();
224         }
225         });
226 
227         controlPanel.add(column2);
228         
229     // Show that spacing controls are related
230     relatedComponents.removeAllElements();
231     relatedComponents.add(interCellSpacingSlider);
232     relatedComponents.add(rowHeightSlider);
233     buildAccessibleGroup(relatedComponents);
234 
235         // Create the table.
236         tableAggregate = createTable();
237         getDemoPanel().add(tableAggregate, BorderLayout.CENTER);
238 
239 
240         // ComboBox for selection modes.
241     JPanel selectMode = new JPanel();
242         column3.setLayout(new ColumnLayout());
243         selectMode.setBorder(new TitledBorder(getString("TableDemo.selection_mode")));
244 
245 
246         selectionModeComboBox = new JComboBox();
247         selectionModeComboBox.addItem(getString("TableDemo.single"));
248         selectionModeComboBox.addItem(getString("TableDemo.one_range"));
249         selectionModeComboBox.addItem(getString("TableDemo.multiple_ranges"));
250         selectionModeComboBox.setSelectedIndex(tableView.getSelectionModel().getSelectionMode());
251         selectionModeComboBox.addItemListener(new ItemListener() {
252         public void itemStateChanged(ItemEvent e) {
253             JComboBox source = (JComboBox)e.getSource();
254                 tableView.setSelectionMode(source.getSelectedIndex());
255         }
256         });
257 
258     selectMode.add(selectionModeComboBox);
259         column3.add(selectMode);
260 
261         // Combo box for table resize mode.
262 
263     JPanel resizeMode = new JPanel();
264 
265     resizeMode.setBorder(new TitledBorder(getString("TableDemo.autoresize_mode")));
266 
267 
268         resizeModeComboBox = new JComboBox();
269         resizeModeComboBox.addItem(getString("TableDemo.off"));
270         resizeModeComboBox.addItem(getString("TableDemo.column_boundries"));
271         resizeModeComboBox.addItem(getString("TableDemo.subsequent_columns"));
272         resizeModeComboBox.addItem(getString("TableDemo.last_column"));
273         resizeModeComboBox.addItem(getString("TableDemo.all_columns"));
274         resizeModeComboBox.setSelectedIndex(tableView.getAutoResizeMode());
275         resizeModeComboBox.addItemListener(new ItemListener() {
276         public void itemStateChanged(ItemEvent e) {
277             JComboBox source = (JComboBox)e.getSource();
278                 tableView.setAutoResizeMode(source.getSelectedIndex());
279         }
280         });
281 
282     resizeMode.add(resizeModeComboBox);
283         column3.add(resizeMode);
284 
285         controlPanel.add(column3);
286 
287     setTableControllers(); // Set accessibility information
288         
289     } // TableDemo()
290 
291     /**
292      * Sets the Accessibility MEMBER_OF property to denote that
293      * these components work together as a group. Each object 
294      * is set to be a MEMBER_OF an array that contains all of
295      * the objects in the group, including itself.
296      *
297      * @param components The list of objects that are related
298      */
299     void buildAccessibleGroup(Vector components) {
300 
301     AccessibleContext context = null;
302     int numComponents = components.size();
303     Object[] group = components.toArray();
304     Object object = null;
305     for (int i = 0; i < numComponents; ++i) {
306         object = components.elementAt(i);
307         if (object instanceof Accessible) {
308             context = ((Accessible)components.elementAt(i)).
309                          getAccessibleContext();
310         context.getAccessibleRelationSet().add(
311             new AccessibleRelation(
312             AccessibleRelation.MEMBER_OF, group));
313         }
314     }
315     } // buildAccessibleGroup()
316 
317     /**
318      * This sets CONTROLLER_FOR on the controls that manipulate the
319      * table and CONTROLLED_BY relationships on the table to point
320      * back to the controllers.
321      */
322     private void setTableControllers() {
323 
324     // Set up the relationships to show what controls the table
325     setAccessibleController(isColumnReorderingAllowedCheckBox, 
326                 tableAggregate);
327     setAccessibleController(showHorizontalLinesCheckBox,
328                 tableAggregate);
329     setAccessibleController(showVerticalLinesCheckBox,
330                 tableAggregate);
331     setAccessibleController(isColumnSelectionAllowedCheckBox,
332                 tableAggregate);
333     setAccessibleController(isRowSelectionAllowedCheckBox,
334                 tableAggregate);
335     setAccessibleController(interCellSpacingSlider,
336                 tableAggregate);
337     setAccessibleController(rowHeightSlider,
338                 tableAggregate);
339     setAccessibleController(selectionModeComboBox,
340                 tableAggregate);
341     setAccessibleController(resizeModeComboBox,
342                 tableAggregate);
343     } // setTableControllers()
344 
345     /**
346      * Sets up accessibility relationships to denote that one 
347      * object controls another. The CONTROLLER_FOR property is
348      * set on the controller object, and the CONTROLLED_BY
349      * property is set on the target object.
350      */
351     private void setAccessibleController(JComponent controller,
352                     JComponent target) {
353     AccessibleRelationSet controllerRelations = 
354         controller.getAccessibleContext().getAccessibleRelationSet();
355     AccessibleRelationSet targetRelations = 
356         target.getAccessibleContext().getAccessibleRelationSet();
357 
358     controllerRelations.add(
359         new AccessibleRelation(
360         AccessibleRelation.CONTROLLER_FOR, target));
361     targetRelations.add(
362         new AccessibleRelation(
363         AccessibleRelation.CONTROLLED_BY, controller));
364     } // setAccessibleController()
365 
366     public JScrollPane createTable() {
367 
368         // final
369         final String[] names = {
370       getString("TableDemo.first_name"),
371       getString("TableDemo.last_name"),
372       getString("TableDemo.favorite_color"),
373       getString("TableDemo.favorite_movie"),
374       getString("TableDemo.favorite_number"),
375       getString("TableDemo.favorite_food")
376     };
377 
378     ImageIcon apple        = createImageIcon("ImageClub/food/apple.jpg",      getString("TableDemo.apple"));
379     ImageIcon asparagus    = createImageIcon("ImageClub/food/asparagus.jpg",  getString("TableDemo.asparagus"));
380     ImageIcon banana       = createImageIcon("ImageClub/food/banana.jpg",     getString("TableDemo.banana"));
381     ImageIcon broccoli     = createImageIcon("ImageClub/food/broccoli.jpg",   getString("TableDemo.broccoli"));
382     ImageIcon cantaloupe   = createImageIcon("ImageClub/food/cantaloupe.jpg", getString("TableDemo.cantaloupe"));
383     ImageIcon carrot       = createImageIcon("ImageClub/food/carrot.jpg",     getString("TableDemo.carrot"));
384     ImageIcon corn         = createImageIcon("ImageClub/food/corn.jpg",       getString("TableDemo.corn"));
385     ImageIcon grapes       = createImageIcon("ImageClub/food/grapes.jpg",     getString("TableDemo.grapes"));
386     ImageIcon grapefruit   = createImageIcon("ImageClub/food/grapefruit.jpg", getString("TableDemo.grapefruit"));
387     ImageIcon kiwi         = createImageIcon("ImageClub/food/kiwi.jpg",       getString("TableDemo.kiwi"));
388     ImageIcon onion        = createImageIcon("ImageClub/food/onion.jpg",      getString("TableDemo.onion"));
389     ImageIcon pear         = createImageIcon("ImageClub/food/pear.jpg",       getString("TableDemo.pear"));
390     ImageIcon peach        = createImageIcon("ImageClub/food/peach.jpg",      getString("TableDemo.peach"));
391     ImageIcon pepper       = createImageIcon("ImageClub/food/pepper.jpg",     getString("TableDemo.pepper"));
392     ImageIcon pickle       = createImageIcon("ImageClub/food/pickle.jpg",     getString("TableDemo.pickle"));
393     ImageIcon pineapple    = createImageIcon("ImageClub/food/pineapple.jpg",  getString("TableDemo.pineapple"));
394     ImageIcon raspberry    = createImageIcon("ImageClub/food/raspberry.jpg",  getString("TableDemo.raspberry"));
395     ImageIcon sparegrass   = createImageIcon("ImageClub/food/asparagus.jpg",  getString("TableDemo.sparegrass"));
396     ImageIcon strawberry   = createImageIcon("ImageClub/food/strawberry.jpg", getString("TableDemo.strawberry"));
397     ImageIcon tomato       = createImageIcon("ImageClub/food/tomato.jpg",     getString("TableDemo.tomato"));
398     ImageIcon watermelon   = createImageIcon("ImageClub/food/watermelon.jpg", getString("TableDemo.watermelon"));
399 
400     NamedColor aqua        = new NamedColor(new Color(127, 255, 212), getString("TableDemo.aqua"));
401     NamedColor beige       = new NamedColor(new Color(245, 245, 220), getString("TableDemo.beige"));
402     NamedColor black       = new NamedColor(Color.black, getString("TableDemo.black"));
403     NamedColor blue        = new NamedColor(new Color(0, 0, 222), getString("TableDemo.blue"));
404     NamedColor eblue       = new NamedColor(Color.blue, getString("TableDemo.eblue"));
405     NamedColor jfcblue     = new NamedColor(new Color(204, 204, 255), getString("TableDemo.jfcblue"));
406     NamedColor jfcblue2    = new NamedColor(new Color(153, 153, 204), getString("TableDemo.jfcblue2"));
407     NamedColor cybergreen  = new NamedColor(Color.green.darker().brighter(), getString("TableDemo.cybergreen"));
408     NamedColor darkgreen   = new NamedColor(new Color(0, 100, 75), getString("TableDemo.darkgreen"));
409     NamedColor forestgreen = new NamedColor(Color.green.darker(), getString("TableDemo.forestgreen"));
410     NamedColor gray        = new NamedColor(Color.gray, getString("TableDemo.gray"));
411     NamedColor green       = new NamedColor(Color.green, getString("TableDemo.green"));
412     NamedColor orange      = new NamedColor(new Color(255, 165, 0), getString("TableDemo.orange"));
413     NamedColor purple      = new NamedColor(new Color(160, 32, 240),  getString("TableDemo.purple"));
414     NamedColor red         = new NamedColor(Color.red, getString("TableDemo.red"));
415     NamedColor rustred     = new NamedColor(Color.red.darker(), getString("TableDemo.rustred"));
416     NamedColor sunpurple   = new NamedColor(new Color(100, 100, 255), getString("TableDemo.sunpurple"));
417     NamedColor suspectpink = new NamedColor(new Color(255, 105, 180), getString("TableDemo.suspectpink"));
418     NamedColor turquoise   = new NamedColor(new Color(0, 255, 255), getString("TableDemo.turquoise"));
419     NamedColor violet      = new NamedColor(new Color(238, 130, 238), getString("TableDemo.violet"));
420     NamedColor yellow      = new NamedColor(Color.yellow, getString("TableDemo.yellow"));
421 
422         // Create the dummy data (a few rows of names)
423         final Object[][] data = {
424       {"Mike", "Albers",      green,       getString("TableDemo.brazil"), new Double(44.0), strawberry},
425       {"Mark", "Andrews",     blue,        getString("TableDemo.curse"), new Double(3), grapes},
426       {"Brian", "Beck",       black,       getString("TableDemo.bluesbros"), new Double(2.7182818285), raspberry},
427       {"Lara", "Bunni",       red,         getString("TableDemo.airplane"), new Double(15), strawberry},
428       {"Roger", "Brinkley",   blue,        getString("TableDemo.man"), new Double(13), peach},
429       {"Brent", "Christian",  black,       getString("TableDemo.bladerunner"), new Double(23), broccoli},
430       {"Mark", "Davidson",    darkgreen,   getString("TableDemo.brazil"), new Double(27), asparagus},
431       {"Jeff", "Dinkins",     blue,        getString("TableDemo.ladyvanishes"), new Double(8), kiwi},
432       {"Ewan", "Dinkins",     yellow,      getString("TableDemo.bugs"), new Double(2), strawberry},
433       {"Amy", "Fowler",       violet,      getString("TableDemo.reservoir"), new Double(3), raspberry},
434       {"Hania", "Gajewska",   purple,      getString("TableDemo.jules"), new Double(5), raspberry},
435       {"David", "Geary",      blue,        getString("TableDemo.pulpfiction"), new Double(3), watermelon},
436 //    {"James", "Gosling",    pink,        getString("TableDemo.tennis"), new Double(21), donut},
437       {"Eric", "Hawkes",      blue,        getString("TableDemo.bladerunner"), new Double(.693), pickle},
438           {"Shannon", "Hickey",   green,       getString("TableDemo.shawshank"), new Integer(2), grapes},
439       {"Earl", "Johnson",     green,       getString("TableDemo.pulpfiction"), new Double(8), carrot},
440       {"Robi", "Kahn",        green,       getString("TableDemo.goodfellas"), new Double(89), apple},
441       {"Robert", "Kim",       blue,        getString("TableDemo.mohicans"), new Double(655321), strawberry},
442       {"Janet", "Koenig",     turquoise,   getString("TableDemo.lonestar"), new Double(7), peach},
443       {"Jeff", "Kesselman",   blue,        getString("TableDemo.stuntman"), new Double(17), pineapple},
444       {"Onno", "Kluyt",       orange,      getString("TableDemo.oncewest"), new Double(8), broccoli},
445       {"Peter", "Korn",       sunpurple,   getString("TableDemo.musicman"), new Double(12), sparegrass},
446 
447       {"Rick", "Levenson",    black,       getString("TableDemo.harold"), new Double(1327), raspberry},
448       {"Brian", "Lichtenwalter", jfcblue,  getString("TableDemo.fifthelement"), new Double(22), pear},
449       {"Malini", "Minasandram", beige,     getString("TableDemo.joyluck"), new Double(9), corn},
450       {"Michael", "Martak",   green,       getString("TableDemo.city"), new Double(3), strawberry},
451       {"David", "Mendenhall", forestgreen, getString("TableDemo.schindlerslist"), new Double(7), peach},
452       {"Phil", "Milne",       suspectpink, getString("TableDemo.withnail"), new Double(3), banana},
453       {"Lynn", "Monsanto",    cybergreen,  getString("TableDemo.dasboot"), new Double(52), peach},
454       {"Hans", "Muller",      rustred,     getString("TableDemo.eraserhead"), new Double(0), pineapple},
455           {"Joshua", "Outwater",  blue,        getString("TableDemo.labyrinth"), new Integer(3), pineapple},
456       {"Tim", "Prinzing",     blue,        getString("TableDemo.firstsight"), new Double(69), pepper},
457       {"Raj", "Premkumar",    jfcblue2,    getString("TableDemo.none"), new Double(7), broccoli},
458       {"Howard", "Rosen",     green,       getString("TableDemo.defending"), new Double(7), strawberry},
459       {"Ray", "Ryan",         black,       getString("TableDemo.buckaroo"),
460        new Double(3.141592653589793238462643383279502884197169399375105820974944), banana},
461       {"Georges", "Saab",     aqua,        getString("TableDemo.bicycle"), new Double(290), cantaloupe},
462       {"Tom", "Santos",       blue,        getString("TableDemo.spinaltap"), new Double(241), pepper},
463       {"Rich", "Schiavi",     blue,        getString("TableDemo.repoman"), new Double(0xFF), pepper},
464       {"Nancy", "Schorr",     green,       getString("TableDemo.fifthelement"), new Double(47), watermelon},
465       {"Keith", "Sprochi",    darkgreen,   getString("TableDemo.2001"), new Integer(13), watermelon},
466       {"Matt", "Tucker",      eblue,       getString("TableDemo.starwars"), new Integer(2), broccoli},
467       {"Dimitri", "Trembovetsky", red,     getString("TableDemo.aliens"), new Integer(222), tomato},
468       {"Scott", "Violet",     violet,      getString("TableDemo.raiders"), new Integer(-97), banana},
469       {"Kathy", "Walrath",    blue,        getString("TableDemo.thinman"), new Integer(8), pear},
470       {"Nathan", "Walrath",   black,       getString("TableDemo.chusingura"), new Integer(3), grapefruit},
471       {"Steve", "Wilson",     green,       getString("TableDemo.raiders"), new Integer(7), onion},
472       {"Kathleen", "Zelony",  gray,        getString("TableDemo.dog"), new Integer(13), grapes}
473         };
474 
475         // Create a model of the data.
476         TableModel dataModel = new AbstractTableModel() {
477             public int getColumnCount() { return names.length; }
478             public int getRowCount() { return data.length;}
479             public Object getValueAt(int row, int col) {return data[row][col];}
480             public String getColumnName(int column) {return names[column];}
481             public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
482         public boolean isCellEditable(int row, int col) {return col != 5;}
483             public void setValueAt(Object aValue, int row, int column) { data[row][column] = aValue; }
484          };
485 
486 
487         // Create the table
488         tableView = new JTable(dataModel);
489 
490         // Show colors by rendering them in their own color.
491         DefaultTableCellRenderer colorRenderer = new DefaultTableCellRenderer() {
492         public void setValue(Object value) {
493             if (value instanceof NamedColor) {
494             NamedColor c = (NamedColor) value;
495                 setBackground(c);
496                 setForeground(c.getTextColor());
497                 setText(c.toString());
498         } else {
499             super.setValue(value);
500         }
501         }
502         };
503 
504     // Create a combo box to show that you can use one in a table.
505         JComboBox comboBox = new JComboBox();
506     comboBox.addItem(aqua);
507     comboBox.addItem(beige);
508     comboBox.addItem(black);
509     comboBox.addItem(blue);
510     comboBox.addItem(eblue);
511     comboBox.addItem(jfcblue);
512     comboBox.addItem(jfcblue2);
513     comboBox.addItem(cybergreen);
514     comboBox.addItem(darkgreen);
515     comboBox.addItem(forestgreen);
516     comboBox.addItem(gray);
517     comboBox.addItem(green);
518     comboBox.addItem(orange);
519     comboBox.addItem(purple);
520     comboBox.addItem(red);
521     comboBox.addItem(rustred);
522     comboBox.addItem(sunpurple);
523     comboBox.addItem(suspectpink);
524     comboBox.addItem(turquoise);
525     comboBox.addItem(violet);
526     comboBox.addItem(yellow);
527 
528         TableColumn colorColumn = tableView.getColumn(getString("TableDemo.favorite_color"));
529         // Use the combo box as the editor in the "Favorite Color" column.
530         colorColumn.setCellEditor(new DefaultCellEditor(comboBox));
531 
532         colorRenderer.setHorizontalAlignment(JLabel.CENTER);
533         colorColumn.setCellRenderer(colorRenderer);
534 
535         tableView.setRowHeight(INITIAL_ROWHEIGHT);
536 
537         scrollpane = new JScrollPane(tableView);
538         return scrollpane;
539     }
540 
541     class NamedColor extends Color {
542     String name;
543     public NamedColor(Color color, String name) {
544         super(color.getRGB());
545         this.name = name;
546     }
547     
548     public Color getTextColor() {
549         int r = getRed();
550         int g = getGreen();
551         int b = getBlue();
552         if(r > 240 || g > 240) {
553         return Color.black;
554         } else {
555         return Color.white;
556         }
557     }
558     
559     public String toString() {
560         return name;
561     }
562     }
563     
564     class ColumnLayout implements LayoutManager {
565     int xInset = 5;
566     int yInset = 5;
567     int yGap = 2;
568     
569     public void addLayoutComponent(String s, Component c) {}
570     
571     public void layoutContainer(Container c) {
572         Insets insets = c.getInsets();
573         int height = yInset + insets.top;
574         
575         Component[] children = c.getComponents();
576         Dimension compSize = null;
577         for (int i = 0; i < children.length; i++) {
578         compSize = children[i].getPreferredSize();
579         children[i].setSize(compSize.width, compSize.height);
580         children[i].setLocation( xInset + insets.left, height);
581         height += compSize.height + yGap;
582         }
583         
584     }
585     
586     public Dimension minimumLayoutSize(Container c) {
587         Insets insets = c.getInsets();
588         int height = yInset + insets.top;
589         int width = 0 + insets.left + insets.right;
590         
591         Component[] children = c.getComponents();
592         Dimension compSize = null;
593         for (int i = 0; i < children.length; i++) {
594         compSize = children[i].getPreferredSize();
595         height += compSize.height + yGap;
596         width = Math.max(width, compSize.width + insets.left + insets.right + xInset*2);
597         }
598         height += insets.bottom;
599         return new Dimension( width, height);
600     }
601     
602     public Dimension preferredLayoutSize(Container c) {
603         return minimumLayoutSize(c);
604     }
605     
606     public void removeLayoutComponent(Component c) {}
607     }
608     
609     
610 }
611