JComboBox
TableCellEditor
異なる行、さらには異なる行の中で最後に選択された値を覚えておいてTableModels
ください。たとえば、ある行の値を選択してから別の行に移動し、セルの編集を開始するとJComboBox
、現在の値として前の行の最後の選択値が表示されます。
どうすれば修正できますか?
JComboBox
TableCellEditor
異なる行、さらには異なる行の中で最後に選択された値を覚えておいてTableModels
ください。たとえば、ある行の値を選択してから別の行に移動し、セルの編集を開始するとJComboBox
、現在の値として前の行の最後の選択値が表示されます。
どうすれば修正できますか?
メソッドに値を設定しますgetTableCellEditorComponent(..)
。
例:
public static void main(String... args) {
JFrame frame = new JFrame("Test");
JTable table = new JTable(10, 2);
JComboBox box = new JComboBox(new String[] {"A", "B", "C"});
table.setDefaultEditor(Object.class, new DefaultCellEditor(box) {
@Override
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
return super.getTableCellEditorComponent(
table,
table.getValueAt(Math.max(row-1, 0), column),
isSelected,
row,
column);
}
});
frame.add(table);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}