import java.awt.BorderLayout;
import java.awt.Dimension;
+import javax.swing.Icon;
+import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
+import javax.swing.SwingUtilities;
+import javax.swing.plaf.metal.MetalIconFactory;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
*
* @author Audrius Meskauskas (audriusa@bioinformatics.org)
*/
-public class TableDemo extends JFrame
+public class TableDemo extends JPanel
{
/**
* The initial row count for this table.
{
/**
- * Return true if the cell is editable. All cells are editable.
+ * Return true if the cell is editable.
+ * Icons are not editable, other cells are editable.
*/
- public boolean isCellEditable(int parm1, int parm2)
+ public boolean isCellEditable(int row, int column)
{
- return true;
+ return column!=1;
}
/**
}
/**
- * The first column contains booleans, others - default class.
+ * The first column contains booleans, the second - icons,
+ * others - default class.
*/
public Class getColumnClass(int column)
{
if (column == 0)
return Boolean.class;
+ else if (column == 1)
+ return Icon.class;
else
return super.getColumnClass(column);
}
}
- private JPanel content;
-
/**
* The table being displayed.
*/
/**
* Create the table demo with the given titel.
- *
- * @param title the frame title.
*/
- public TableDemo(String title)
+ public TableDemo()
{
- super(title);
- getContentPane().add(createContent(), BorderLayout.CENTER);
+ super();
+ createContent();
}
/**
* the bottom of the panel if they want to (a close button is added if this
* demo is being run as a standalone demo).
*/
- JPanel createContent()
+ private void createContent()
{
- if (content == null)
+ setLayout(new BorderLayout());
+ values = new Object[rows][];
+
+ // The icons that appear in the icon column.
+ Icon[] icons = new Icon[]
+ {
+ MetalIconFactory.getTreeComputerIcon(),
+ MetalIconFactory.getTreeHardDriveIcon(),
+ MetalIconFactory.getTreeFolderIcon(),
+ };
+
+ for (int i = 0; i < values.length; i++)
{
- JPanel p = new JPanel();
- p.setLayout(new BorderLayout());
- values = new Object[rows][];
- for (int i = 0; i < values.length; i++)
+ values[i] = new Object[cols];
+ for (int j = 2; j < cols; j++)
{
- values[i] = new Object[cols];
- for (int j = 1; j < cols; j++)
- {
- values[i][j] = "" + ((char) ('a' + j)) + i;
- }
- values [i][0] = i % 2 == 0? Boolean.TRUE : Boolean.FALSE;
+ values[i][j] = "" + ((char) ('a' + j)) + i;
}
+ values [i][0] = i % 2 == 0? Boolean.TRUE : Boolean.FALSE;
+ values [i][1] = icons [ i % icons.length ];
+ }
- table.setModel(model);
+ table.setModel(model);
- // Make the columns with gradually increasing width:
- DefaultTableColumnModel cm = new DefaultTableColumnModel();
- for (int i = 0; i < cols; i++)
- {
- TableColumn column = new TableColumn(i);
+ // Make the columns with gradually increasing width:
+ DefaultTableColumnModel cm = new DefaultTableColumnModel();
+ for (int i = 0; i < cols; i++)
+ {
+ TableColumn column = new TableColumn(i);
- // Showing the variable width columns.
- int width = 100+20*i;
- column.setPreferredWidth(width);
+ // Showing the variable width columns.
+ int width = 100+10*i;
+ column.setPreferredWidth(width);
- // If we do not set the header value here, the value, returned
- // by model, is used.
- column.setHeaderValue("Width +"+(20*i));
+ // If we do not set the header value here, the value, returned
+ // by model, is used.
+ column.setHeaderValue("Width +"+(20*i));
- cm.addColumn(column);
- }
+ cm.addColumn(column);
+ }
- table.setColumnModel(cm);
+ table.setColumnModel(cm);
- // Create the table, place it into scroll pane and place
- // the pane into this frame.
- JScrollPane scroll = new JScrollPane();
+ // Create the table, place it into scroll pane and place
+ // the pane into this frame.
+ JScrollPane scroll = new JScrollPane();
- // The horizontal scroll bar is never needed.
- scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
- scroll.getViewport().add(table);
- p.add(scroll, BorderLayout.CENTER);
- content = p;
- }
- return content;
+ // The horizontal scroll bar is never needed.
+ scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
+ scroll.getViewport().add(table);
+ add(scroll, BorderLayout.CENTER);
}
/**
*/
public static void main(String[] args)
{
- TableDemo frame = new TableDemo("Table double click on the cell to edit.");
- frame.setSize(new Dimension(640, 100));
- frame.validate();
- frame.setVisible(true);
+ SwingUtilities.invokeLater
+ (new Runnable()
+ {
+ public void run()
+ {
+ TableDemo demo = new TableDemo();
+ JFrame frame = new JFrame();
+ frame.getContentPane().add(demo);
+ frame.setSize(new Dimension(640, 100));
+ frame.setVisible(true);
+ }
+ });
+ }
+
+ /**
+ * Returns a DemoFactory that creates a TableDemo.
+ *
+ * @return a DemoFactory that creates a TableDemo
+ */
+ public static DemoFactory createDemoFactory()
+ {
+ return new DemoFactory()
+ {
+ public JComponent createDemo()
+ {
+ return new TableDemo();
+ }
+ };
}
}