JTable始终无法为内部ComboBox获取正确的rowIndex [英] JTable alway can not get correct rowIndex for inner ComboBox

查看:65
本文介绍了JTable始终无法为内部ComboBox获取正确的rowIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTable,它的一列单元格是JComboBox. 但是,当尝试单击表JComboBox单元格时获取行数时,我发现行索引始终返回错误值(总是最后单击的行索引).

I have a JTable which its one column cell is JComboBox. But when try to get row count when click the table JComboBox cell, I found the row index always return error value (alway is the last click row index).

public class TableComboBoxTest extends JFrame {

    private JTable table;
    private DefaultTableModel tableModel;
    private Object[][] tableCells;
    private final String[] TABLE_COLUMNS = {"No.1"};
    private final String[] YES_NO_SELECTION = {"Yes", "No"};

    public TableComboBoxTest() {
        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());
        tableModel = new DefaultTableModel(tableCells, TABLE_COLUMNS);

        table = new JTable(tableModel);

        DefaultCellEditor cellEditor = null;

        JComboBox selA = new JComboBox(YES_NO_SELECTION);
        cellEditor = new DefaultCellEditor(selA);
        cellEditor.setClickCountToStart(1);
        table.getColumn(TABLE_COLUMNS[0]).setCellEditor(cellEditor);

        JScrollPane jsp = new JScrollPane();
        jsp.getViewport().add(table, null);
        pane.add(jsp, BorderLayout.CENTER);

        TableCellEditor tce = null;
        addRow("Yes");
        outputDefaultSelection(0, 0);

        addRow("No");
        outputDefaultSelection(1, 0);

        System.out.println("");

        selA.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    JComboBox cb = (JComboBox) e.getSource();
                    String sel = (String) cb.getSelectedItem();
                    int rowIndex = table.getSelectedRow();
                    rowIndex = table.convertRowIndexToModel(rowIndex);

                    if (rowIndex == -1) {
                        return;
                    }

                    outputDefaultSelection(rowIndex, 0);
                    System.out.println("Select: " + sel + " at " + rowIndex);
                }
            }
        });
    }

    private void addRow(String v1) {
        Vector<String> vec = new Vector<String>();
        vec.add(v1);

        tableModel.addRow(vec);
        tableModel.fireTableDataChanged();
    }

    private void outputDefaultSelection(int row, int col) {
        TableCellEditor tce = table.getCellEditor(row, col);
        System.out.println("Default " + row + "-" + col + " Selection: " + tce.getCellEditorValue());
        System.out.println("Default " + row + "-" + col + " Value: " + table.getModel().getValueAt(row, col));
    }

    public static void main(String[] args) {
        TableComboBoxTest stt = new TableComboBoxTest();
        stt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        stt.setSize(200, 100);
        stt.setVisible(true);
    }
}


Default 0-0 Selection: Yes
Default 0-0 Value: Yes
Default 1-0 Selection: Yes
Default 1-0 Value: No*

当单击第一行并选择是"时,没有更改事件触发器. 单击第二行时,更改事件触发器!行号错误:0

When Click on first row and select "Yes", no change event trigger. When Click on second row, change event trigger! and row number is wrong: 0


Default 0-0 Selection: No
Default 0-0 Value: Yes
Select: No at 0*

当继续单击第一行时,更改事件触发器!行号错误:1

When continue to click on first row, change event trigger! and row number is wrong: 1


Default 1-0 Selection: Yes
Default 1-0 Value: No
Select: Yes at 1

如何获得正确的点击单元格编号?

How can I get correct clicking cell number?

对于itemStateChanged流程,我还发现单元格设置值是否与默认列值相同(是"),单击该事件时将不会触发.但是,如果将单元格设置值设置为否",则单击它会导致更改事件.这意味着模型数据与默认选择的数据不同.如何使它们一致?

And for the itemStateChanged process, I also found if cell set value is same with default column value ("Yes"), when click it event will not be trigger. But if cell set value to "No", click it will cause change event. That means model data is different with default selected data. How to make them consistent?

谢谢〜

推荐答案

这意味着模型数据与默认选择的数据不同.如何使它们一致?

That means model data is different with default selected data. How to make them consistent?

这仅表示该模型尚未使用组合框中的新选择值进行更新.

It just means that the model has not yet been updated with the newly selected value from the combo box.

这可以通过使用以下示例进行演示:

This can be demonstrated by using the following:

final String sel = (String) cb.getSelectedItem();
final int rowIndex = table.convertRowIndexToModel(table.getSelectedRow());

if (rowIndex == -1) {
    return;
}

SwingUtilities.invokeLater(new Runnable()
{
    public void run()
    {
        outputDefaultSelection(rowIndex, 0);
        System.out.println("Select: " + sel + " at " + rowIndex);
    }
});

现在,显示代码将被添加到事件调度线程的末尾,这意味着它将在所有其他事件完成执行之后执行,因此现在将更新TableModel.

Now, the display code will be added to the end of the Event Dispatch Thread, which means it will be executed after all other events are finished executing so the TableModel will now be updated.

但是,这不是最佳解决方案.如果您想知道何时更改了单元格中的数据,则将TableModelListener添加到TableModel. 不要使用ItemListener.

However, that is not the best solution. If you want to know when data has been changed in a cell then add a TableModelListener to the TableModel. Don't use an ItemListener.

这篇关于JTable始终无法为内部ComboBox获取正确的rowIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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