Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / examples / gnu / classpath / examples / swing / TableDemo.java
blobe661ee3adc694bcecfcbafb6fc607a13d5830183
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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.Dimension;
44 import javax.swing.Icon;
45 import javax.swing.JComponent;
46 import javax.swing.JFrame;
47 import javax.swing.JPanel;
48 import javax.swing.JScrollPane;
49 import javax.swing.JTable;
50 import javax.swing.SwingUtilities;
51 import javax.swing.plaf.metal.MetalIconFactory;
52 import javax.swing.table.DefaultTableColumnModel;
53 import javax.swing.table.DefaultTableModel;
54 import javax.swing.table.TableColumn;
56 /**
57 * Displays the editable table. The first column consists of check boxes.
59 * @author Audrius Meskauskas (audriusa@bioinformatics.org)
61 public class TableDemo extends JPanel
63 /**
64 * The initial row count for this table.
65 */
66 static int rows = 32;
68 /**
69 * The initial column count for this table.
70 */
71 static int cols = 7;
74 /**
75 * The table model.
77 class TModel extends DefaultTableModel
80 /**
81 * Return true if the cell is editable.
82 * Icons are not editable, other cells are editable.
84 public boolean isCellEditable(int row, int column)
86 return column!=1;
89 /**
90 * Get the number of the table rows.
92 public int getRowCount()
94 return rows;
97 /**
98 * Get the number of the table columns.
100 public int getColumnCount()
102 return cols;
106 * Set the value at the given position
108 public void setValueAt(Object aValue, int aRow, int aColumn)
110 values[aRow][aColumn] = aValue;
114 * Get the value at the given position.
116 public Object getValueAt(int aRow, int aColumn)
118 return values[aRow][aColumn];
122 * The column name, as suggested by model. This header should not be
123 * visible, as it is overridden by setting the header name with
124 * {@link TableColumn#setHeaderValue} in {@link TableDemo#createContent}.
126 public String getColumnName(int column)
128 return "Error "+column;
132 * The first column contains booleans, the second - icons,
133 * others - default class.
135 public Class getColumnClass(int column)
137 if (column == 0)
138 return Boolean.class;
139 else if (column == 1)
140 return Icon.class;
141 else
142 return super.getColumnClass(column);
147 * The table being displayed.
149 JTable table = new JTable();
152 * The table model.
154 TModel model = new TModel();
157 * The table value array.
159 Object[][] values;
162 * Create the table demo with the given titel.
164 public TableDemo()
166 super();
167 createContent();
171 * Returns a panel with the demo content. The panel uses a BorderLayout(), and
172 * the BorderLayout.SOUTH area is empty, to allow callers to add controls to
173 * the bottom of the panel if they want to (a close button is added if this
174 * demo is being run as a standalone demo).
176 private void createContent()
178 setLayout(new BorderLayout());
179 values = new Object[rows][];
181 // The icons that appear in the icon column.
182 Icon[] icons = new Icon[]
184 MetalIconFactory.getTreeComputerIcon(),
185 MetalIconFactory.getTreeHardDriveIcon(),
186 MetalIconFactory.getTreeFolderIcon(),
189 for (int i = 0; i < values.length; i++)
191 values[i] = new Object[cols];
192 for (int j = 2; j < cols; j++)
194 values[i][j] = "" + ((char) ('a' + j)) + i;
196 values [i][0] = i % 2 == 0? Boolean.TRUE : Boolean.FALSE;
197 values [i][1] = icons [ i % icons.length ];
200 table.setModel(model);
202 // Make the columns with gradually increasing width:
203 DefaultTableColumnModel cm = new DefaultTableColumnModel();
204 for (int i = 0; i < cols; i++)
206 TableColumn column = new TableColumn(i);
208 // Showing the variable width columns.
209 int width = 100+10*i;
210 column.setPreferredWidth(width);
212 // If we do not set the header value here, the value, returned
213 // by model, is used.
214 column.setHeaderValue("Width +"+(20*i));
216 cm.addColumn(column);
219 table.setColumnModel(cm);
221 // Create the table, place it into scroll pane and place
222 // the pane into this frame.
223 JScrollPane scroll = new JScrollPane();
225 // The horizontal scroll bar is never needed.
226 scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
227 scroll.getViewport().add(table);
228 add(scroll, BorderLayout.CENTER);
232 * The executable method to display the editable table.
234 * @param args
235 * unused.
237 public static void main(String[] args)
239 SwingUtilities.invokeLater
240 (new Runnable()
242 public void run()
244 TableDemo demo = new TableDemo();
245 JFrame frame = new JFrame();
246 frame.getContentPane().add(demo);
247 frame.setSize(new Dimension(640, 100));
248 frame.setVisible(true);
254 * Returns a DemoFactory that creates a TableDemo.
256 * @return a DemoFactory that creates a TableDemo
258 public static DemoFactory createDemoFactory()
260 return new DemoFactory()
262 public JComponent createDemo()
264 return new TableDemo();