List of usage examples for javax.swing JTable getDefaultEditor
public TableCellEditor getDefaultEditor(Class<?> columnClass)
TableColumn
. From source file:Main.java
public static void main(String[] argv) { DefaultTableModel model = new DefaultTableModel() { public Class getColumnClass(int columnIndex) { Object o = getValueAt(0, columnIndex); if (o == null) { return Object.class; } else { return o.getClass(); }/* w ww . jav a 2s .c om*/ } }; JTable table = new JTable(model); model.addColumn("Boolean", new Object[] { Boolean.TRUE }); model.addColumn("Date", new Object[] { new Date() }); model.addColumn("Double", new Object[] { new Double(Math.PI) }); model.addColumn("Float", new Object[] { new Float(1.2) }); model.addColumn("Icon", new Object[] { new ImageIcon("icon.gif") }); model.addColumn("Number", new Object[] { new Integer(1) }); model.addColumn("Object", new Object[] { "object" }); Enumeration e = table.getColumnModel().getColumns(); TableColumn col = (TableColumn) e.nextElement(); col.setCellRenderer(table.getDefaultRenderer(Boolean.class)); col.setCellEditor(table.getDefaultEditor(Boolean.class)); JFrame f = new JFrame(); f.setSize(300, 300); f.add(new JScrollPane(table)); f.setVisible(true); }
From source file:org.yccheok.jstock.gui.JTableUtilities.java
public static void insertTableColumnFromModel(JTable jTable, Object value, int clickedColumnIndex) { boolean isVisible = true; try {//from w w w. j av a 2s.c om TableColumn tableColumn = jTable.getColumn(value); } catch (java.lang.IllegalArgumentException exp) { isVisible = false; } if (isVisible) return; TableModel tableModel = jTable.getModel(); final int modelIndex = getModelColumnIndex(jTable, value); Class c = tableModel.getColumnClass(modelIndex); TableColumn tableColumn = new javax.swing.table.TableColumn(modelIndex, 0, jTable.getDefaultRenderer(c), jTable.getDefaultEditor(c)); jTable.addColumn(tableColumn); makeTableColumnWidthFit(jTable, jTable.getColumnCount() - 1, 5); // If we right clicked on the 3rd column, and select a new column, we // would like the new column to be inserted into 4th column. Note that, // clickedColumnIndex will be < 0, if we right clicked on empty area. if (clickedColumnIndex < 0) { // Have it in the last column when we right clicked on empty area. jTable.moveColumn(jTable.getColumnCount() - 1, jTable.getColumnCount() - 1); } else { // +1, as we want our newly inserted column to be at the right of // clicked column. jTable.moveColumn(jTable.getColumnCount() - 1, Math.min(jTable.getColumnCount() - 1, clickedColumnIndex + 1)); } }