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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| import ihm.CIHMComboBoxFond;
import ihm.CIHMTableDef;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ScrollPaneLayout;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class Cmain extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
private DefaultTableModel model;
public static void main(String[] args)
{
Cmain main = new Cmain();
main.setVisible(true);
}
public Cmain()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
model = new DefaultTableModel(new Object[] { "item1", "item2", "item3" },
0);
CIHMTableDef table = new CIHMTableDef();
table.setModel(model);
// These are the combobox values
Object[] values = new Object[] { "item1", "item2", "item3" };
CIHMComboBoxFond[] objs = new CIHMComboBoxFond[] {
new CIHMComboBoxFond(values), new CIHMComboBoxFond(values),
new CIHMComboBoxFond(values) };
model.addRow(new Object[] { objs, null, null });
MyComboBoxEditor cbe = new MyComboBoxEditor(new Object[] { "it1", "it2",
"it3" });
MyComboBoxRenderer cbr = new MyComboBoxRenderer();
// Set the combobox editor on the 1st visible column
for (int vColIndex = 0; vColIndex < model.getColumnCount(); vColIndex++)
{
TableColumn col = table.getColumnModel().getColumn(vColIndex);
col.setCellEditor(cbe);
col.setCellRenderer(cbr);
}
JScrollPane table_pane = new JScrollPane();
table_pane.setLayout(new ScrollPaneLayout());
table_pane.getViewport().add(table);
Button b = new Button("Test");
b.addActionListener(this);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(table_pane, BorderLayout.CENTER);
this.getContentPane().add(b, BorderLayout.SOUTH);
this.setSize(800, 600);
}
public class MyComboBoxRenderer implements TableCellRenderer
{
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column)
{
Component ret = null;
Object o = table.getValueAt(row, 0);
if ((o != null) && (o instanceof CIHMComboBoxFond[]) && (column > 0))
{
ret = ((CIHMComboBoxFond[]) o)[column - 1];
}
return ret;
}
}
public class MyComboBoxEditor extends DefaultCellEditor
{
private static final long serialVersionUID = 1L;
public MyComboBoxEditor(Object[] items)
{
super(new JTextField());
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column)
{
Component ret = null;
Object o = table.getValueAt(row, 0);
if ((o != null) && (o instanceof CIHMComboBoxFond[]))
{
ret = ((CIHMComboBoxFond[]) o)[column - 1];
}
return ret;
}
@Override
public boolean isCellEditable(EventObject anEvent)
{
return true;
}
}
@Override
public void actionPerformed(ActionEvent e)
{
// These are the combobox values
Object[] values = new Object[] { "item1", "item2", "item3" };
CIHMComboBoxFond[] objs = new CIHMComboBoxFond[] {
new CIHMComboBoxFond(values), new CIHMComboBoxFond(values),
new CIHMComboBoxFond(values) };
model.addRow(new Object[] { objs, null, null });
}
} |
Partager