我创建了一个表格,其中每行的单元格中都会显示一个组合框。我分别使用了以下两个类作为单元格编辑器和单元格渲染器。以某种方式显示表格时,单元格中的每个组合框在单击时都不会打开。任何人都可以给我一个提示吗?提前致谢
public class CellEditor extends DefaultCellEditor{
private static final long serialVersionUID = 1L;
public CellEditor(String[] items) {
super(new JComboBox(items));
}
}
public class ComboBoxRenderer extends JComboBox implements TableCellRenderer {
/****/
private static final long serialVersionUID = 1L;
public ComboBoxRenderer(String[] items) {
super(items);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
this.setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
this.setForeground(table.getForeground());
this.setBackground(table.getBackground());
}
this.setSelectedItem(value);// Select the current value
return this;
}
}
答案 0 :(得分:1)
请阅读JTable的教程,Editors and Renderers和Using a Combo Box as an Editor,此论坛上的一些示例(包含JTable中的AutoCompleted JComboBox)或here或{{ 3}}
但基本上是你的问题,(检查你是否设定了)
public boolean isCellEditable(int row, int col) {
if (col == someInt) {
return true;
} else if (col == TableColumnsStartsWithZero) {
return true;
} else {
return false;
}
}