JComboBox 未在 jTable 中打开

发布于 2024-11-26 12:35:03 字数 1006 浏览 0 评论 0原文

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

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

I have created a table where in a cell of each row a combo box is displayed. I have used the following two classes as cell editor and cell renderer respectively. Somehow when the table is displayed, every combo box in a cell doesn't open when it's clicked. Can anyone give me a hint? Thanks in advance

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜`诱少女 2024-12-03 12:35:03

请阅读JTable的教程,有编辑器和渲染器使用组合框作为编辑器,此论坛上的一些示例(包括 JTable 中的 AutoCompleted JComboBox)或 此处此处

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

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

please reads JTable's tutorial, there are Editors and Renderers and Using a Combo Box as an Editor, some examples on this Forum (inc AutoCompleted JComboBox in the JTable) or here or here

but basically is your question about, (check if you set that)

public boolean isCellEditable(int row, int col) {
    if (col == someInt) {
        return true;
    } else if (col == TableColumnsStartsWithZero) {
        return true;
    } else {
        return false;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文