我在 JTable 的第 3 列和第 4 列中有一个 JComboBox,但我不知道如何获取它的项目...问题不是获取项目的方法,而是演员表
JComboBox combo=(JComboBox) jTable1.getColumnModel().getColumn(3).getCellEditor();
你能帮我吗?
我在 JTable 的第 3 列和第 4 列中有一个 JComboBox,但我不知道如何获取它的项目...问题不是获取项目的方法,而是演员表
JComboBox combo=(JComboBox) jTable1.getColumnModel().getColumn(3).getCellEditor();
你能帮我吗?
被JComboBox
包裹在一个CellEditor
. 您必须检索包装的组件,例如在使用时DefaultCellEditor
:
DefaultCellEditor editor = (DefaultCellEditor)table.getColumnModel().getColumn(3).getCellEditor();
JComboBox combo = (JComboBox)editor.getComponent();
阅读本教程,了解如何在 JTable 中使用 JCombobox 作为编辑器。
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#combobox
尝试这样的事情:
public void example(){
TableColumn tmpColum =table.getColumnModel().getColumn(1);
String[] DATA = { "Data 1", "Data 2", "Data 3", "Data 4" };
JComboBox comboBox = new JComboBox(DATA);
DefaultCellEditor defaultCellEditor=new DefaultCellEditor(comboBox);
tmpColum.setCellEditor(defaultCellEditor);
tmpColum.setCellRenderer(new CheckBoxCellRenderer(comboBox));
table.repaint();
}
/**
Custom class for adding elements in the JComboBox.
*/
class CheckBoxCellRenderer implements TableCellRenderer {
JComboBox combo;
public CheckBoxCellRenderer(JComboBox comboBox) {
this.combo = new JComboBox();
for (int i=0; i<comboBox.getItemCount(); i++){
combo.addItem(comboBox.getItemAt(i));
}
}
public Component getTableCellRendererComponent(JTable jtable, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
combo.setSelectedItem(value);
return combo;
}
}