1 /* TableDemo.java -- Demonstrates the use of JTable.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.classpath.examples.swing;
41 import java.awt.BorderLayout;
42 import java.awt.Component;
43 import java.awt.Dimension;
44 import javax.swing.AbstractCellEditor;
45 import javax.swing.BorderFactory;
46 import javax.swing.DefaultCellEditor;
47 import javax.swing.Icon;
48 import javax.swing.JComboBox;
49 import javax.swing.JComponent;
50 import javax.swing.JFrame;
51 import javax.swing.JPanel;
52 import javax.swing.JScrollBar;
53 import javax.swing.JScrollPane;
54 import javax.swing.JSlider;
55 import javax.swing.JTable;
56 import javax.swing.SwingUtilities;
57 import javax.swing.border.Border;
58 import javax.swing.plaf.metal.MetalIconFactory;
59 import javax.swing.table.DefaultTableColumnModel;
60 import javax.swing.table.DefaultTableModel;
61 import javax.swing.table.TableCellEditor;
62 import javax.swing.table.TableCellRenderer;
63 import javax.swing.table.TableColumn;
64 import javax.swing.table.TableColumnModel;
67 * Displays the editable table. The first column consists of check boxes.
69 * @author Audrius Meskauskas (audriusa@bioinformatics.org)
71 public class TableDemo extends JPanel
74 * The initial row count for this table.
79 * The initial column count for this table.
87 class TModel extends DefaultTableModel
91 * All cells are editable in our table.
93 public boolean isCellEditable(int row, int column)
99 * Get the number of the table rows.
101 public int getRowCount()
107 * Get the number of the table columns.
109 public int getColumnCount()
115 * Set the value at the given position
117 public void setValueAt(Object aValue, int aRow, int aColumn)
119 values[aRow][aColumn] = aValue;
123 * Get the value at the given position.
125 public Object getValueAt(int aRow, int aColumn)
127 return values[aRow][aColumn];
131 * The column name, as suggested by model. This header should not be
132 * visible, as it is overridden by setting the header name with
133 * {@link TableColumn#setHeaderValue} in {@link TableDemo#createContent}.
135 public String getColumnName(int column)
137 return "Error "+column;
141 * The first column contains booleans, the second - icons,
142 * others - default class.
144 public Class getColumnClass(int column)
147 return Boolean.class;
148 else if (column == 1)
151 return super.getColumnClass(column);
156 * The scroll bar renderer.
159 extends AbstractCellEditor
160 implements TableCellEditor, TableCellRenderer
173 * The border around the bar, if required.
175 Border border = BorderFactory.createLineBorder(table.getGridColor());
180 bar.setOrientation(JScrollBar.HORIZONTAL);
182 bar.setMaximum(rows);
183 bar.setBorder(border);
185 rendererBar = new JSlider();
186 rendererBar.setMinimum(0);
187 rendererBar.setMaximum(rows);
188 rendererBar.setEnabled(false);
194 public Component getTableCellEditorComponent(JTable table, Object value,
195 boolean isSelected, int row,
198 if (value instanceof Integer)
199 bar.setValue(((Integer) value).intValue());
206 public Component getTableCellRendererComponent(JTable table, Object value,
208 boolean hasFocus, int row,
211 rendererBar.setValue(((Integer) value).intValue());
213 rendererBar.setBorder(border);
215 rendererBar.setBorder(null);
219 public Object getCellEditorValue()
221 return new Integer(bar.getValue());
227 * The table being displayed.
229 JTable table = new JTable();
234 TModel model = new TModel();
237 * The table value array.
242 * The icons that appear in the icon column.
244 Icon[] icons = new Icon[]
246 MetalIconFactory.getTreeComputerIcon(),
247 MetalIconFactory.getTreeHardDriveIcon(),
248 MetalIconFactory.getTreeFolderIcon(),
252 * The choices in the combo boxes
254 String [] sides = new String[]
256 "north", "south", "east", "west"
261 * Create the table demo with the given titel.
270 * Returns a panel with the demo content. The panel uses a BorderLayout(), and
271 * the BorderLayout.SOUTH area is empty, to allow callers to add controls to
272 * the bottom of the panel if they want to (a close button is added if this
273 * demo is being run as a standalone demo).
275 private void createContent()
277 setLayout(new BorderLayout());
278 values = new Object[rows][];
280 for (int i = 0; i < values.length; i++)
282 values[i] = new Object[cols];
283 for (int j = 3; j < cols; j++)
285 values[i][j] = "" + ((char) ('a' + j)) + i;
287 values [i][0] = i % 2 == 0? Boolean.TRUE : Boolean.FALSE;
288 values [i][1] = icons [ i % icons.length ];
289 values [i][2] = sides [ i % sides.length ];
290 values [i][4] = new Integer(i);
293 table.setModel(model);
295 // Make the columns with gradually increasing width:
296 DefaultTableColumnModel cm = new DefaultTableColumnModel();
297 table.setColumnModel(cm);
299 for (int i = 0; i < cols; i++)
301 TableColumn column = new TableColumn(i);
303 // Showing the variable width columns.
304 int width = 100+10*i;
305 column.setPreferredWidth(width);
307 // If we do not set the header value here, the value, returned
308 // by model, is used.
309 column.setHeaderValue("Width +"+(20*i));
311 cm.addColumn(column);
315 setInformativeHeaders();
317 // Create the table, place it into scroll pane and place
318 // the pane into this frame.
319 JScrollPane scroll = new JScrollPane();
321 // The horizontal scroll bar is never needed.
322 scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
323 scroll.getViewport().add(table);
324 add(scroll, BorderLayout.CENTER);
326 // Increase the row height to make the icons and sliders look better.
327 table.setRowHeight(table.getRowHeight()+2);
331 * Set the more informative column headers for specific columns.
333 void setInformativeHeaders()
335 TableColumnModel cm = table.getColumnModel();
337 cm.getColumn(0).setHeaderValue("check");
338 cm.getColumn(1).setHeaderValue("icon");
339 cm.getColumn(2).setHeaderValue("combo");
340 cm.getColumn(3).setHeaderValue("edit combo");
341 cm.getColumn(4).setHeaderValue("slider");
345 * Set the custom editors for combo boxes. This method also sets one
348 void setCustomEditors()
350 TableColumnModel cm = table.getColumnModel();
352 // Set combo-box based editor for icons (note that no custom
353 // renderer is needed for JComboBox to work with icons.
354 JComboBox combo0 = new JComboBox(icons);
355 cm.getColumn(1).setCellEditor(new DefaultCellEditor(combo0));
357 // Set the simple combo box editor for the third column:
358 JComboBox combo1 = new JComboBox(sides);
359 cm.getColumn(2).setCellEditor(new DefaultCellEditor(combo1));
361 // Set the editable combo box for the forth column:
362 JComboBox combo2 = new JComboBox(sides);
363 combo2.setEditable(true);
364 cm.getColumn(3).setCellEditor(new DefaultCellEditor(combo2));
366 SliderCell scrollView = new SliderCell();
367 cm.getColumn(4).setCellEditor(scrollView);
368 cm.getColumn(4).setCellRenderer(scrollView);
370 table.setColumnModel(cm);
374 * The executable method to display the editable table.
379 public static void main(String[] args)
381 SwingUtilities.invokeLater
386 TableDemo demo = new TableDemo();
387 JFrame frame = new JFrame();
388 frame.getContentPane().add(demo);
389 frame.setSize(new Dimension(640, 100));
390 frame.setVisible(true);
396 * Returns a DemoFactory that creates a TableDemo.
398 * @return a DemoFactory that creates a TableDemo
400 public static DemoFactory createDemoFactory()
402 return new DemoFactory()
404 public JComponent createDemo()
406 return new TableDemo();