【问题标题】:JComboBox not opening in a jTableJComboBox 未在 jTable 中打开
【发布时间】:2011-07-25 18:12:28
【问题描述】:

我创建了一个表格,其中每行的一个单元格中都会显示一个组合框。我分别使用以下两个类作为单元格编辑器和单元格渲染器。以某种方式显示表格时,单击单元格中的每个组合框都不会打开。谁能给我一个提示?提前致谢

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;
}
}

【问题讨论】:

  • 请发SSCCE
  • 为什么要扩展 DefaultCellEditor?没有必要这样做。

标签: java swing jtable jcombobox tablecelleditor


【解决方案1】:

请阅读JTable的教程,有Editors and RenderersUsing a Combo Box as an Editor,这个论坛上的一些例子(包括JTable中的AutoCompleted JComboBox)或herehere

但基本上是你的问题,(检查你是否设置)

public boolean isCellEditable(int row, int col) {
    if (col == someInt) {
        return true;
    } else if (col == TableColumnsStartsWithZero) {
        return true;
    } else {
        return false;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    相关资源
    最近更新 更多