1
36
37
40
41
42 import javax.swing.*;
43 import javax.swing.event.*;
44 import javax.swing.text.*;
45 import javax.swing.table.*;
46 import javax.swing.border.*;
47 import javax.swing.colorchooser.*;
48 import javax.swing.filechooser.*;
49 import javax.accessibility.*;
50
51 import java.awt.*;
52 import java.awt.event.*;
53 import java.awt.print.PrinterException;
54 import java.beans.*;
55 import java.util.*;
56 import java.io.*;
57 import java.applet.*;
58 import java.net.*;
59
60 import java.text.MessageFormat;
61
62
69 public class TableDemo extends DemoModule {
70 JTable tableView;
71 JScrollPane scrollpane;
72 Dimension origin = new Dimension(0, 0);
73
74 JCheckBox isColumnReorderingAllowedCheckBox;
75 JCheckBox showHorizontalLinesCheckBox;
76 JCheckBox showVerticalLinesCheckBox;
77
78 JCheckBox isColumnSelectionAllowedCheckBox;
79 JCheckBox isRowSelectionAllowedCheckBox;
80
81 JLabel interCellSpacingLabel;
82 JLabel rowHeightLabel;
83
84 JSlider interCellSpacingSlider;
85 JSlider rowHeightSlider;
86
87 JComboBox selectionModeComboBox = null;
88 JComboBox resizeModeComboBox = null;
89
90 JLabel headerLabel;
91 JLabel footerLabel;
92
93 JTextField headerTextField;
94 JTextField footerTextField;
95
96 JCheckBox fitWidth;
97 JButton printButton;
98
99 JPanel controlPanel;
100 JScrollPane tableAggregate;
101
102 String path = "ImageClub/food/";
103
104 final int INITIAL_ROWHEIGHT = 33;
105
106
109 public static void main(String[] args) {
110 TableDemo demo = new TableDemo(null);
111 demo.mainImpl();
112 }
113
114
117 public TableDemo(SwingSet2 swingset) {
118 super(swingset, "TableDemo", "toolbar/JTable.gif");
119
120 getDemoPanel().setLayout(new BorderLayout());
121 controlPanel = new JPanel();
122 controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.X_AXIS));
123 JPanel cbPanel = new JPanel(new GridLayout(3, 2));
124 JPanel labelPanel = new JPanel(new GridLayout(2, 1)) {
125 public Dimension getMaximumSize() {
126 return new Dimension(getPreferredSize().width, super.getMaximumSize().height);
127 }
128 };
129 JPanel sliderPanel = new JPanel(new GridLayout(2, 1)) {
130 public Dimension getMaximumSize() {
131 return new Dimension(getPreferredSize().width, super.getMaximumSize().height);
132 }
133 };
134 JPanel comboPanel = new JPanel(new GridLayout(2, 1));
135 JPanel printPanel = new JPanel(new ColumnLayout());
136
137 getDemoPanel().add(controlPanel, BorderLayout.NORTH);
138 Vector relatedComponents = new Vector();
139
140
141 isColumnReorderingAllowedCheckBox = new JCheckBox(getString("TableDemo.reordering_allowed"), true);
143 isColumnReorderingAllowedCheckBox.addActionListener(new ActionListener() {
144 public void actionPerformed(ActionEvent e) {
145 boolean flag = ((JCheckBox)e.getSource()).isSelected();
146 tableView.getTableHeader().setReorderingAllowed(flag);
147 tableView.repaint();
148 }
149 });
150
151 showHorizontalLinesCheckBox = new JCheckBox(getString("TableDemo.horz_lines"), true);
152 showHorizontalLinesCheckBox.addActionListener(new ActionListener() {
153 public void actionPerformed(ActionEvent e) {
154 boolean flag = ((JCheckBox)e.getSource()).isSelected();
155 tableView.setShowHorizontalLines(flag); ;
156 tableView.repaint();
157 }
158 });
159
160 showVerticalLinesCheckBox = new JCheckBox(getString("TableDemo.vert_lines"), true);
161 showVerticalLinesCheckBox.addActionListener(new ActionListener() {
162 public void actionPerformed(ActionEvent e) {
163 boolean flag = ((JCheckBox)e.getSource()).isSelected();
164 tableView.setShowVerticalLines(flag); ;
165 tableView.repaint();
166 }
167 });
168
169 relatedComponents.removeAllElements();
171 relatedComponents.add(showHorizontalLinesCheckBox);
172 relatedComponents.add(showVerticalLinesCheckBox);
173 buildAccessibleGroup(relatedComponents);
174
175 isRowSelectionAllowedCheckBox = new JCheckBox(getString("TableDemo.row_selection"), true);
176 isRowSelectionAllowedCheckBox.addActionListener(new ActionListener() {
177 public void actionPerformed(ActionEvent e) {
178 boolean flag = ((JCheckBox)e.getSource()).isSelected();
179 tableView.setRowSelectionAllowed(flag); ;
180 tableView.repaint();
181 }
182 });
183
184 isColumnSelectionAllowedCheckBox = new JCheckBox(getString("TableDemo.column_selection"), false);
185 isColumnSelectionAllowedCheckBox.addActionListener(new ActionListener() {
186 public void actionPerformed(ActionEvent e) {
187 boolean flag = ((JCheckBox)e.getSource()).isSelected();
188 tableView.setColumnSelectionAllowed(flag); ;
189 tableView.repaint();
190 }
191 });
192
193 relatedComponents.removeAllElements();
195 relatedComponents.add(isColumnSelectionAllowedCheckBox);
196 relatedComponents.add(isRowSelectionAllowedCheckBox);
197 buildAccessibleGroup(relatedComponents);
198
199 cbPanel.add(isColumnReorderingAllowedCheckBox);
200 cbPanel.add(isRowSelectionAllowedCheckBox);
201 cbPanel.add(showHorizontalLinesCheckBox);
202 cbPanel.add(isColumnSelectionAllowedCheckBox);
203 cbPanel.add(showVerticalLinesCheckBox);
204
205
206 interCellSpacingLabel = new JLabel(getString("TableDemo.intercell_spacing_colon"));
208 labelPanel.add(interCellSpacingLabel);
209
210 rowHeightLabel = new JLabel(getString("TableDemo.row_height_colon"));
211 labelPanel.add(rowHeightLabel);
212
213
214 interCellSpacingSlider = new JSlider(JSlider.HORIZONTAL, 0, 10, 1);
216 interCellSpacingSlider.getAccessibleContext().setAccessibleName(getString("TableDemo.intercell_spacing"));
217 interCellSpacingLabel.setLabelFor(interCellSpacingSlider);
218 sliderPanel.add(interCellSpacingSlider);
219 interCellSpacingSlider.addChangeListener(new ChangeListener() {
220 public void stateChanged(ChangeEvent e) {
221 int spacing = ((JSlider)e.getSource()).getValue();
222 tableView.setIntercellSpacing(new Dimension(spacing, spacing));
223 tableView.repaint();
224 }
225 });
226
227 rowHeightSlider = new JSlider(JSlider.HORIZONTAL, 5, 100, INITIAL_ROWHEIGHT);
228 rowHeightSlider.getAccessibleContext().setAccessibleName(getString("TableDemo.row_height"));
229 rowHeightLabel.setLabelFor(rowHeightSlider);
230 sliderPanel.add(rowHeightSlider);
231 rowHeightSlider.addChangeListener(new ChangeListener() {
232 public void stateChanged(ChangeEvent e) {
233 int height = ((JSlider)e.getSource()).getValue();
234 tableView.setRowHeight(height);
235 tableView.repaint();
236 }
237 });
238
239 relatedComponents.removeAllElements();
241 relatedComponents.add(interCellSpacingSlider);
242 relatedComponents.add(rowHeightSlider);
243 buildAccessibleGroup(relatedComponents);
244
245
246 tableAggregate = createTable();
248 getDemoPanel().add(tableAggregate, BorderLayout.CENTER);
249
250
251 JPanel selectMode = new JPanel();
253 selectMode.setLayout(new BoxLayout(selectMode, BoxLayout.X_AXIS));
254 selectMode.setBorder(new TitledBorder(getString("TableDemo.selection_mode")));
255
256
257 selectionModeComboBox = new JComboBox() {
258 public Dimension getMaximumSize() {
259 return getPreferredSize();
260 }
261 };
262 selectionModeComboBox.addItem(getString("TableDemo.single"));
263 selectionModeComboBox.addItem(getString("TableDemo.one_range"));
264 selectionModeComboBox.addItem(getString("TableDemo.multiple_ranges"));
265 selectionModeComboBox.setSelectedIndex(tableView.getSelectionModel().getSelectionMode());
266 selectionModeComboBox.addItemListener(new ItemListener() {
267 public void itemStateChanged(ItemEvent e) {
268 JComboBox source = (JComboBox)e.getSource();
269 tableView.setSelectionMode(source.getSelectedIndex());
270 }
271 });
272
273 selectMode.add(Box.createHorizontalStrut(2));
274 selectMode.add(selectionModeComboBox);
275 selectMode.add(Box.createHorizontalGlue());
276 comboPanel.add(selectMode);
277
278 JPanel resizeMode = new JPanel();
280 resizeMode.setLayout(new BoxLayout(resizeMode, BoxLayout.X_AXIS));
281 resizeMode.setBorder(new TitledBorder(getString("TableDemo.autoresize_mode")));
282
283
284 resizeModeComboBox = new JComboBox() {
285 public Dimension getMaximumSize() {
286 return getPreferredSize();
287 }
288 };
289 resizeModeComboBox.addItem(getString("TableDemo.off"));
290 resizeModeComboBox.addItem(getString("TableDemo.column_boundaries"));
291 resizeModeComboBox.addItem(getString("TableDemo.subsequent_columns"));
292 resizeModeComboBox.addItem(getString("TableDemo.last_column"));
293 resizeModeComboBox.addItem(getString("TableDemo.all_columns"));
294 resizeModeComboBox.setSelectedIndex(tableView.getAutoResizeMode());
295 resizeModeComboBox.addItemListener(new ItemListener() {
296 public void itemStateChanged(ItemEvent e) {
297 JComboBox source = (JComboBox)e.getSource();
298 tableView.setAutoResizeMode(source.getSelectedIndex());
299 }
300 });
301
302 resizeMode.add(Box.createHorizontalStrut(2));
303 resizeMode.add(resizeModeComboBox);
304 resizeMode.add(Box.createHorizontalGlue());
305 comboPanel.add(resizeMode);
306
307 printPanel.setBorder(new TitledBorder(getString("TableDemo.printing")));
309 headerLabel = new JLabel(getString("TableDemo.header"));
310 footerLabel = new JLabel(getString("TableDemo.footer"));
311 headerTextField = new JTextField(getString("TableDemo.headerText"), 15);
312 footerTextField = new JTextField(getString("TableDemo.footerText"), 15);
313 fitWidth = new JCheckBox(getString("TableDemo.fitWidth"), true);
314 printButton = new JButton(getString("TableDemo.print"));
315 printButton.addActionListener(new ActionListener() {
316 public void actionPerformed(ActionEvent ae) {
317 printTable();
318 }
319 });
320
321 printPanel.add(headerLabel);
322 printPanel.add(headerTextField);
323 printPanel.add(footerLabel);
324 printPanel.add(footerTextField);
325
326 JPanel buttons = new JPanel();
327 buttons.add(fitWidth);
328 buttons.add(printButton);
329
330 printPanel.add(buttons);
331
332 relatedComponents.removeAllElements();
334 relatedComponents.add(headerTextField);
335 relatedComponents.add(footerTextField);
336 relatedComponents.add(printButton);
337 buildAccessibleGroup(relatedComponents);
338
339 JPanel sliderWrapper = new JPanel();
341 sliderWrapper.setLayout(new BoxLayout(sliderWrapper, BoxLayout.X_AXIS));
342 sliderWrapper.add(labelPanel);
343 sliderWrapper.add(sliderPanel);
344 sliderWrapper.add(Box.createHorizontalGlue());
345 sliderWrapper.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 0));
346
347 JPanel leftWrapper = new JPanel();
348 leftWrapper.setLayout(new BoxLayout(leftWrapper, BoxLayout.Y_AXIS));
349 leftWrapper.add(cbPanel);
350 leftWrapper.add(sliderWrapper);
351
352 controlPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 2, 0));
354 controlPanel.add(leftWrapper);
355 controlPanel.add(comboPanel);
356 controlPanel.add(printPanel);
357
358 setTableControllers(); }
361
369 void buildAccessibleGroup(Vector components) {
370
371 AccessibleContext context = null;
372 int numComponents = components.size();
373 Object[] group = components.toArray();
374 Object object = null;
375 for (int i = 0; i < numComponents; ++i) {
376 object = components.elementAt(i);
377 if (object instanceof Accessible) {
378 context = ((Accessible)components.elementAt(i)).
379 getAccessibleContext();
380 context.getAccessibleRelationSet().add(
381 new AccessibleRelation(
382 AccessibleRelation.MEMBER_OF, group));
383 }
384 }
385 }
387
392 private void setTableControllers() {
393
394 setAccessibleController(isColumnReorderingAllowedCheckBox,
396 tableAggregate);
397 setAccessibleController(showHorizontalLinesCheckBox,
398 tableAggregate);
399 setAccessibleController(showVerticalLinesCheckBox,
400 tableAggregate);
401 setAccessibleController(isColumnSelectionAllowedCheckBox,
402 tableAggregate);
403 setAccessibleController(isRowSelectionAllowedCheckBox,
404 tableAggregate);
405 setAccessibleController(interCellSpacingSlider,
406 tableAggregate);
407 setAccessibleController(rowHeightSlider,
408 tableAggregate);
409 setAccessibleController(selectionModeComboBox,
410 tableAggregate);
411 setAccessibleController(resizeModeComboBox,
412 tableAggregate);
413 }
415
421 private void setAccessibleController(JComponent controller,
422 JComponent target) {
423 AccessibleRelationSet controllerRelations =
424 controller.getAccessibleContext().getAccessibleRelationSet();
425 AccessibleRelationSet targetRelations =
426 target.getAccessibleContext().getAccessibleRelationSet();
427
428 controllerRelations.add(
429 new AccessibleRelation(
430 AccessibleRelation.CONTROLLER_FOR, target));
431 targetRelations.add(
432 new AccessibleRelation(
433 AccessibleRelation.CONTROLLED_BY, controller));
434 }
436 public JScrollPane createTable() {
437
438 final String[] names = {
440 getString("TableDemo.first_name"),
441 getString("TableDemo.last_name"),
442 getString("TableDemo.favorite_color"),
443 getString("TableDemo.favorite_movie"),
444 getString("TableDemo.favorite_number"),
445 getString("TableDemo.favorite_food")
446 };
447
448 ImageIcon apple = createImageIcon("ImageClub/food/apple.jpg", getString("TableDemo.apple"));
449 ImageIcon asparagus = createImageIcon("ImageClub/food/asparagus.jpg", getString("TableDemo.asparagus"));
450 ImageIcon banana = createImageIcon("ImageClub/food/banana.jpg", getString("TableDemo.banana"));
451 ImageIcon broccoli = createImageIcon("ImageClub/food/broccoli.jpg", getString("TableDemo.broccoli"));
452 ImageIcon cantaloupe = createImageIcon("ImageClub/food/cantaloupe.jpg", getString("TableDemo.cantaloupe"));
453 ImageIcon carrot = createImageIcon("ImageClub/food/carrot.jpg", getString("TableDemo.carrot"));
454 ImageIcon corn = createImageIcon("ImageClub/food/corn.jpg", getString("TableDemo.corn"));
455 ImageIcon grapes = createImageIcon("ImageClub/food/grapes.jpg", getString("TableDemo.grapes"));
456 ImageIcon grapefruit = createImageIcon("ImageClub/food/grapefruit.jpg", getString("TableDemo.grapefruit"));
457 ImageIcon kiwi = createImageIcon("ImageClub/food/kiwi.jpg", getString("TableDemo.kiwi"));
458 ImageIcon onion = createImageIcon("ImageClub/food/onion.jpg", getString("TableDemo.onion"));
459 ImageIcon pear = createImageIcon("ImageClub/food/pear.jpg", getString("TableDemo.pear"));
460 ImageIcon peach = createImageIcon("ImageClub/food/peach.jpg", getString("TableDemo.peach"));
461 ImageIcon pepper = createImageIcon("ImageClub/food/pepper.jpg", getString("TableDemo.pepper"));
462 ImageIcon pickle = createImageIcon("ImageClub/food/pickle.jpg", getString("TableDemo.pickle"));
463 ImageIcon pineapple = createImageIcon("ImageClub/food/pineapple.jpg", getString("TableDemo.pineapple"));
464 ImageIcon raspberry = createImageIcon("ImageClub/food/raspberry.jpg", getString("TableDemo.raspberry"));
465 ImageIcon sparegrass = createImageIcon("ImageClub/food/asparagus.jpg", getString("TableDemo.sparegrass"));
466 ImageIcon strawberry = createImageIcon("ImageClub/food/strawberry.jpg", getString("TableDemo.strawberry"));
467 ImageIcon tomato = createImageIcon("ImageClub/food/tomato.jpg", getString("TableDemo.tomato"));
468 ImageIcon watermelon = createImageIcon("ImageClub/food/watermelon.jpg", getString("TableDemo.watermelon"));
469
470 NamedColor aqua = new NamedColor(new Color(127, 255, 212), getString("TableDemo.aqua"));
471 NamedColor beige = new NamedColor(new Color(245, 245, 220), getString("TableDemo.beige"));
472 NamedColor black = new NamedColor(Color.black, getString("TableDemo.black"));
473 NamedColor blue = new NamedColor(new Color(0, 0, 222), getString("TableDemo.blue"));
474 NamedColor eblue = new NamedColor(Color.blue, getString("TableDemo.eblue"));
475 NamedColor jfcblue = new NamedColor(new Color(204, 204, 255), getString("TableDemo.jfcblue"));
476 NamedColor jfcblue2 = new NamedColor(new Color(153, 153, 204), getString("TableDemo.jfcblue2"));
477 NamedColor cybergreen = new NamedColor(Color.green.darker().brighter(), getString("TableDemo.cybergreen"));
478 NamedColor darkgreen = new NamedColor(new Color(0, 100, 75), getString("TableDemo.darkgreen"));
479 NamedColor forestgreen = new NamedColor(Color.green.darker(), getString("TableDemo.forestgreen"));
480 NamedColor gray = new NamedColor(Color.gray, getString("TableDemo.gray"));
481 NamedColor green = new NamedColor(Color.green, getString("TableDemo.green"));
482 NamedColor orange = new NamedColor(new Color(255, 165, 0), getString("TableDemo.orange"));
483 NamedColor purple = new NamedColor(new Color(160, 32, 240), getString("TableDemo.purple"));
484 NamedColor red = new NamedColor(Color.red, getString("TableDemo.red"));
485 NamedColor rustred = new NamedColor(Color.red.darker(), getString("TableDemo.rustred"));
486 NamedColor sunpurple = new NamedColor(new Color(100, 100, 255), getString("TableDemo.sunpurple"));
487 NamedColor suspectpink = new NamedColor(new Color(255, 105, 180), getString("TableDemo.suspectpink"));
488 NamedColor turquoise = new NamedColor(new Color(0, 255, 255), getString("TableDemo.turquoise"));
489 NamedColor violet = new NamedColor(new Color(238, 130, 238), getString("TableDemo.violet"));
490 NamedColor yellow = new NamedColor(Color.yellow, getString("TableDemo.yellow"));
491
492 final Object[][] data = {
494 {"Mike", "Albers", green, getString("TableDemo.brazil"), new Double(44.0), strawberry},
495 {"Mark", "Andrews", blue, getString("TableDemo.curse"), new Double(3), grapes},
496 {"Brian", "Beck", black, getString("TableDemo.bluesbros"), new Double(2.7182818285), raspberry},
497 {"Lara", "Bunni", red, getString("TableDemo.airplane"), new Double(15), strawberry},
498 {"Roger", "Brinkley", blue, getString("TableDemo.man"), new Double(13), peach},
499 {"Brent", "Christian", black, getString("TableDemo.bladerunner"), new Double(23), broccoli},
500 {"Mark", "Davidson", darkgreen, getString("TableDemo.brazil"), new Double(27), asparagus},
501 {"Jeff", "Dinkins", blue, getString("TableDemo.ladyvanishes"), new Double(8), kiwi},
502 {"Ewan", "Dinkins", yellow, getString("TableDemo.bugs"), new Double(2), strawberry},
503 {"Amy", "Fowler", violet, getString("TableDemo.reservoir"), new Double(3), raspberry},
504 {"Hania", "Gajewska", purple, getString("TableDemo.jules"), new Double(5), raspberry},
505 {"David", "Geary", blue, getString("TableDemo.pulpfiction"), new Double(3), watermelon},
506 {"Eric", "Hawkes", blue, getString("TableDemo.bladerunner"), new Double(.693), pickle},
508 {"Shannon", "Hickey", green, getString("TableDemo.shawshank"), new Integer(2), grapes},
509 {"Earl", "Johnson", green, getString("TableDemo.pulpfiction"), new Double(8), carrot},
510 {"Robi", "Khan", green, getString("TableDemo.goodfellas"), new Double(89), apple},
511 {"Robert", "Kim", blue, getString("TableDemo.mohicans"), new Double(655321), strawberry},
512 {"Janet", "Koenig", turquoise, getString("TableDemo.lonestar"), new Double(7), peach},
513 {"Jeff", "Kesselman", blue, getString("TableDemo.stuntman"), new Double(17), pineapple},
514 {"Onno", "Kluyt", orange, getString("TableDemo.oncewest"), new Double(8), broccoli},
515 {"Peter", "Korn", sunpurple, getString("TableDemo.musicman"), new Double(12), sparegrass},
516
517 {"Rick", "Levenson", black, getString("TableDemo.harold"), new Double(1327), raspberry},
518 {"Brian", "Lichtenwalter", jfcblue, getString("TableDemo.fifthelement"), new Double(22), pear},
519 {"Malini", "Minasandram", beige, getString("TableDemo.joyluck"), new Double(9), corn},
520 {"Michael", "Martak", green, getString("TableDemo.city"), new Double(3), strawberry},
521 {"David", "Mendenhall", forestgreen, getString("TableDemo.schindlerslist"), new Double(7), peach},
522 {"Phil", "Milne", suspectpink, getString("TableDemo.withnail"), new Double(3), banana},
523 {"Lynn", "Monsanto", cybergreen, getString("TableDemo.dasboot"), new Double(52), peach},
524 {"Hans", "Muller", rustred, getString("TableDemo.eraserhead"), new Double(0), pineapple},
525 {"Joshua", "Outwater", blue, getString("TableDemo.labyrinth"), new Integer(3), pineapple},
526 {"Tim", "Prinzing", blue, getString("TableDemo.firstsight"), new Double(69), pepper},
527 {"Raj", "Premkumar", jfcblue2, getString("TableDemo.none"), new Double(7), broccoli},
528 {"Howard", "Rosen", green, getString("TableDemo.defending"), new Double(7), strawberry},
529 {"Ray", "Ryan", black, getString("TableDemo.buckaroo"),
530 new Double(3.141592653589793238462643383279502884197169399375105820974944), banana},
531 {"Georges", "Saab", aqua, getString("TableDemo.bicycle"), new Double(290), cantaloupe},
532 {"Tom", "Santos", blue, getString("TableDemo.spinaltap"), new Double(241), pepper},
533 {"Rich", "Schiavi", blue, getString("TableDemo.repoman"), new Double(0xFF), pepper},
534 {"Nancy", "Schorr", green, getString("TableDemo.fifthelement"), new Double(47), watermelon},
535 {"Keith", "Sprochi", darkgreen, getString("TableDemo.2001"), new Integer(13), watermelon},
536 {"Matt", "Tucker", eblue, getString("TableDemo.starwars"), new Integer(2), broccoli},
537 {"Dmitri", "Trembovetski", red, getString("TableDemo.aliens"), new Integer(222), tomato},
538 {"Scott", "Violet", violet, getString("TableDemo.raiders"), new Integer(-97), banana},
539 {"Kathy", "Walrath", blue, getString("TableDemo.thinman"), new Integer(8), pear},
540 {"Nathan", "Walrath", black, getString("TableDemo.chusingura"), new Integer(3), grapefruit},
541 {"Steve", "Wilson", green, getString("TableDemo.raiders"), new Integer(7), onion},
542 {"Kathleen", "Zelony", gray, getString("TableDemo.dog"), new Integer(13), grapes}
543 };
544
545 TableModel dataModel = new AbstractTableModel() {
547 public int getColumnCount() { return names.length; }
548 public int getRowCount() { return data.length;}
549 public Object getValueAt(int row, int col) {return data[row][col];}
550 public String getColumnName(int column) {return names[column];}
551 public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}
552 public boolean isCellEditable(int row, int col) {return col != 5;}
553 public void setValueAt(Object aValue, int row, int column) { data[row][column] = aValue; }
554 };
555
556
557 tableView = new JTable(dataModel);
559
560 DefaultTableCellRenderer colorRenderer = new DefaultTableCellRenderer() {
562 public void setValue(Object value) {
563 if (value instanceof NamedColor) {
564 NamedColor c = (NamedColor) value;
565 setBackground(c);
566 setForeground(c.getTextColor());
567 setText(c.toString());
568 } else {
569 super.setValue(value);
570 }
571 }
572 };
573
574 JComboBox comboBox = new JComboBox();
576 comboBox.addItem(aqua);
577 comboBox.addItem(beige);
578 comboBox.addItem(black);
579 comboBox.addItem(blue);
580 comboBox.addItem(eblue);
581 comboBox.addItem(jfcblue);
582 comboBox.addItem(jfcblue2);
583 comboBox.addItem(cybergreen);
584 comboBox.addItem(darkgreen);
585 comboBox.addItem(forestgreen);
586 comboBox.addItem(gray);
587 comboBox.addItem(green);
588 comboBox.addItem(orange);
589 comboBox.addItem(purple);
590 comboBox.addItem(red);
591 comboBox.addItem(rustred);
592 comboBox.addItem(sunpurple);
593 comboBox.addItem(suspectpink);
594 comboBox.addItem(turquoise);
595 comboBox.addItem(violet);
596 comboBox.addItem(yellow);
597
598 TableColumn colorColumn = tableView.getColumn(getString("TableDemo.favorite_color"));
599 colorColumn.setCellEditor(new DefaultCellEditor(comboBox));
601
602 colorRenderer.setHorizontalAlignment(JLabel.CENTER);
603 colorColumn.setCellRenderer(colorRenderer);
604
605 tableView.setRowHeight(INITIAL_ROWHEIGHT);
606
607 scrollpane = new JScrollPane(tableView);
608 return scrollpane;
609 }
610
611 private void printTable() {
612 MessageFormat headerFmt;
613 MessageFormat footerFmt;
614 JTable.PrintMode printMode = fitWidth.isSelected() ?
615 JTable.PrintMode.FIT_WIDTH :
616 JTable.PrintMode.NORMAL;
617
618 String text;
619 text = headerTextField.getText();
620 if (text != null && text.length() > 0) {
621 headerFmt = new MessageFormat(text);
622 } else {
623 headerFmt = null;
624 }
625
626 text = footerTextField.getText();
627 if (text != null && text.length() > 0) {
628 footerFmt = new MessageFormat(text);
629 } else {
630 footerFmt = null;
631 }
632
633 try {
634 boolean status = tableView.print(printMode, headerFmt, footerFmt);
635
636 if (status) {
637 JOptionPane.showMessageDialog(this, getString("TableDemo.printingComplete"),
638 getString("TableDemo.printingResult"),
639 JOptionPane.INFORMATION_MESSAGE);
640 } else {
641 JOptionPane.showMessageDialog(this, getString("TableDemo.printingCancelled"),
642 getString("TableDemo.printingResult"),
643 JOptionPane.INFORMATION_MESSAGE);
644 }
645 } catch (PrinterException pe) {
646 String errorMessage = MessageFormat.format(getString("TableDemo.printingFailed"),
647 new Object[] {pe.getMessage()});
648 JOptionPane.showMessageDialog(this, errorMessage,
649 getString("TableDemo.printingResult"),
650 JOptionPane.ERROR_MESSAGE);
651 }
652 }
653
654 class NamedColor extends Color {
655 String name;
656 public NamedColor(Color color, String name) {
657 super(color.getRGB());
658 this.name = name;
659 }
660
661 public Color getTextColor() {
662 int r = getRed();
663 int g = getGreen();
664 int b = getBlue();
665 if(r > 240 || g > 240) {
666 return Color.black;
667 } else {
668 return Color.white;
669 }
670 }
671
672 public String toString() {
673 return name;
674 }
675 }
676
677 class ColumnLayout implements LayoutManager {
678 int xInset = 5;
679 int yInset = 5;
680 int yGap = 2;
681
682 public void addLayoutComponent(String s, Component c) {}
683
684 public void layoutContainer(Container c) {
685 Insets insets = c.getInsets();
686 int height = yInset + insets.top;
687
688 Component[] children = c.getComponents();
689 Dimension compSize = null;
690 for (int i = 0; i < children.length; i++) {
691 compSize = children[i].getPreferredSize();
692 children[i].setSize(compSize.width, compSize.height);
693 children[i].setLocation( xInset + insets.left, height);
694 height += compSize.height + yGap;
695 }
696
697 }
698
699 public Dimension minimumLayoutSize(Container c) {
700 Insets insets = c.getInsets();
701 int height = yInset + insets.top;
702 int width = 0 + insets.left + insets.right;
703
704 Component[] children = c.getComponents();
705 Dimension compSize = null;
706 for (int i = 0; i < children.length; i++) {
707 compSize = children[i].getPreferredSize();
708 height += compSize.height + yGap;
709 width = Math.max(width, compSize.width + insets.left + insets.right + xInset*2);
710 }
711 height += insets.bottom;
712 return new Dimension( width, height);
713 }
714
715 public Dimension preferredLayoutSize(Container c) {
716 return minimumLayoutSize(c);
717 }
718
719 public void removeLayoutComponent(Component c) {}
720 }
721
722 void updateDragEnabled(boolean dragEnabled) {
723 tableView.setDragEnabled(dragEnabled);
724 headerTextField.setDragEnabled(dragEnabled);
725 footerTextField.setDragEnabled(dragEnabled);
726 }
727
728 }
729