1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
public class TableRenderDemo extends JPanel {
private static final long serialVersionUID = 1L;
public TableRenderDemo() {
super(new GridLayout(1,0));
JTable table = new JTable(new MyTableModel());
setUpSportColumn(table, table.getColumnModel().getColumn(1));
add(table);
}
public void setUpSportColumn(JTable table,
TableColumn sportColumn) {
P1JComboBoxPropertyType comboBox = new P1JComboBoxPropertyType();
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
}
class MyTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private String[] columnNames = {"Name","Type"};
private Object[] data = {
new P1TextPropertyType(""),
new P1NumberPropertyType(""),
new P1TextPropertyType(""),
new P1NumberPropertyType(""),
new P1TextPropertyType("")
};
public final Object[] longValues = {"None of the above"};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int arg0, int arg1) {
P1PropertyType pt = (P1PropertyType)data[arg0];
switch (arg1) {
case 0 :
return pt.getName();
case 1 :
return pt.getType();
}
return null;
}
public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void setValueAt(Object value, int row, int col) {
P1PropertyType pt = (P1PropertyType)data[row];
switch (col) {
case 0 :
pt.setName((String)value);
break;
case 1 :
String n = pt.getName();
pt = P1PropertyType.createPropertyType(((P1PropertyType)value).getType());
pt.setName(n);
data[row] = pt;
break;
}
fireTableCellUpdated(row, col);
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("TableRenderDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableRenderDemo newContentPane = new TableRenderDemo();
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
} |
Partager