繁体   English   中英

如何检索JTable中特定单元格上使用的组件类型?

[英]How do I retrieve the component type used at a specific cell in a JTable?

我创建了一个JTable其列包含各种不同的组件。 最后五列要么不包含任何内容(没有值的DefaultTableCellRenderer ),要么使用自定义渲染器和编辑器的JRadioButton 渲染器在我需要的单元格中创建并返回一个新的radioButton,我想从面板类访问这些单选按钮以将它们添加到ButtonGroup 我正在使用下面编写的代码:

protected void setButtonGroups() {
    buttonGroups = new ArrayList<ButtonGroup>();
    for (int i = 0; i < tableModel.getRowCount(); i++) {
        ButtonGroup currentGroup = new ButtonGroup();
        for (int j = 0; j < tableModel.getColumnCount(); j++) {
            if (table.getComponentAt(i, j) != null) {
                    currentGroup.add((JRadioButton)table.getComponentAt(i, j));
                }
            }
        }
        buttonGroups.add(currentGroup);
    }
}

无论单元格中包含什么内容,无论它是JCheckBoxJRadioButtonJComboBoxgetComponentAt()始终返回null。所有内容JComboBox null返回。

有没有其他方法来获取单元的组件? 还是我有办法让它正常工作? 谢谢!

如果您有来自table.getModel()的tableModel 然后可以调用model.getValueAt(row,col)

我不确定为什么要将Swing组件放在JTable中。 您是否正在使用它进行格式化? 如果是这样,请查看LayoutManagers 如果要编辑表中的值,则可能需要研究扩展TableCellEditor。 如果您确实要在表中放置组件。 考虑扩展DefaultTableCellRenderer以返回您的组件。 您将要重写getTableCellRendererComponent()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM