Celleditor(JComboBox)在JTable的特定行中 [英] Celleditor (JComboBox) in a specific row of a JTable

查看:69
本文介绍了Celleditor(JComboBox)在JTable的特定行中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道怎么做在特定行中设置一个jcombobox ...现在我已经为所有行设置了这个jcombobox但是我只希望它在一行中:

I don't know how to do to set a jcombobox in a specific row...for now I've this jcombobox for all rows but I want it in only one row:

JComboBox cc = new JComboBox();
cc.addItem(jComboBox5.getSelectedItem()+"/"+jComboBox6.getSelectedItem()+"/"+jComboBox7.getSelectedItem()+" "+jComboBox1.getSelectedItem()+"."+jComboBox2.getSelectedItem());
jTable1.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(cc));
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setToolTipText("CLICCA PER LE DATE");
jTable1.getColumnModel().getColumn(3).setCellRenderer(renderer);


推荐答案

更新:正如我正在测试我的(可能是不完整的)答案,我遇到了一个非常好的SO问题,我认为它会比我能做得更好:将JComboBox放入JTable中

Update: As I was testing my (probably incomplete) answer out, I came across a very good SO Question that I think will help much better than I could: Putting JComboBox into JTable

另一个更新:我再次阅读你的问题,我意识到你要求特定行。我能想到的唯一方法就是创建一个自定义的CellEditor,类似于:

Another Update: I read your question again, and I realized you asked for a specific row. The only way I can think of doing this is to create a custom CellEditor, something like:

private static class MyCellEditor extends AbstractCellEditor implements TableCellEditor {

    DefaultCellEditor other = new DefaultCellEditor(new JTextField());
    DefaultCellEditor checkbox = new DefaultCellEditor(new JComboBox(new Object[] {"abc"}));

    private DefaultCellEditor lastSelected;

    @Override
    public Object getCellEditorValue() {

        return lastSelected.getCellEditorValue();
    }

    @Override
    public Component getTableCellEditorComponent(JTable table,
            Object value, boolean isSelected, int row, int column) {
        if(row == 0) {
            lastSelected = checkbox;
            return checkbox.getTableCellEditorComponent(table, value, isSelected, row, column);
        }
        lastSelected = other;
        return other.getTableCellEditorComponent(table, value, isSelected, row, column);
    }

}

在此示例中,自定义CellEditor实际上是两个编辑器,根据选择的行,特定的编辑器将接收调用(比喻和字面)。我承认 lastSelected 似乎有点笨拙,但老实说我找不到更简单的方法来知道返回哪个Editor值(因为 getCellEditorValue 没有args)。

In this example, the custom CellEditor is actually two Editors, and depending on the row selected, the particular Editor will get the call (both figuratively and literally). I admit that the lastSelected seemed a bit hokey, but I honestly could not find an easier way to know which Editor value to return (as the getCellEditorValue has no args).

为了让你的表显得正确,你可能还需要对渲染器做一些事情。 (因为渲染器可能知道也可能不知道将JComboBox的选定值显示为初始值)。这取决于您如何初始化实际表中的数据。

To make your Table appear "correct", you're probably going to have to do something with the Renderer as well (because the Renderer may or may not know to show the JComboBox's selected value as the initial value). This depends on how you're initializing the data in the actual table.

为了完整起见,我的原始答案如下:

您可以在TableModel上使用 addRow 将JComboBox组件添加到,如下所示:如何在JTable中添加行?

You can add the JComboBox component to the row using addRow on the TableModel as shown here: How to add row in JTable?

另请参阅: http://docs.oracle.com/javase/ tutorial / uiswing / components / table.html

我认为主要问题是你将列编辑器/渲染器的想法与实际数据混合在一起将存储在每一行中。

I think the main issue is you're mixing the idea of Column Editors/Renderers with the actual data that will be stored in each row.

这篇关于Celleditor(JComboBox)在JTable的特定行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆