JTable 更改列顺序然后编辑单元格引发错误

JTable changing column order and then editing cell raising bug

我对 JTable 有一个奇怪的问题。

  1. 我有什么

一个两列的 JTable。第二列使用JComboBox作为单元格编辑器。

  1. 问题

当我使用原始列顺序编辑 JComboBox 单元格时,它工作正常。 但是当我先更改列时,例如切换第 2 列(JComboBox 列变成拳头),然后我编辑 JComboBox 单元格引发 Bug。 我必须单击该单元格两次才能触发编辑事件,否则,JComboBox 不会被激活。

  1. 我试过的

我尝试使用 TableCellListener,但它引发了异常,例如列索引为 -1;

我的 SSCCE:

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.AbstractAction;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import java.awt.GridLayout;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;

public class MainTable extends JDialog {
    private static final long serialVersionUID = 156332386872772726L;

    private final JPanel contentPanel = new JPanel();
    private DefaultTableModel tableModel;
    private JTable table;

    public static void main(String[] args) {
        try {
            MainTable dialog = new MainTable();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public MainTable() {
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(new GridLayout(1, 0, 0, 0));
        {
            JScrollPane scrollPane = new JScrollPane();
            contentPanel.add(scrollPane);
            {
                table = new JTable();
                table.setAutoCreateRowSorter(true);
                tableModel = new DefaultTableModel(new String[]{"first","second"},0);

                table.setModel(tableModel);
                {
                    //set comboBox to table
                    JComboBox<String> comboBox = new JComboBox<String>();
                    comboBox.addItem("Input");
                    comboBox.addItem("Output");

                    table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(comboBox));
                    table.getColumnModel().addColumnModelListener(new TableColumnModelListener(){
                        @Override
                        public void columnAdded(TableColumnModelEvent arg0) {

                        }

                        @Override
                        public void columnMarginChanged(ChangeEvent arg0) {

                        }

                        @Override
                        public void columnMoved(TableColumnModelEvent arg0) {
                            table.requestFocusInWindow();
                            comboBox.setFocusable(true);
                            comboBox.requestFocus();
                            comboBox.grabFocus();
                            comboBox.requestFocusInWindow();//try to get focus, not worked.
                        }

                        @Override
                        public void columnRemoved(TableColumnModelEvent arg0) {

                        }

                        @Override
                        public void columnSelectionChanged(ListSelectionEvent arg0) {

                        }
                    });
                }
//              TableCellListener tcl = new TableCellListener(table, new AbstractAction(){
//                  @Override
//                  public void actionPerformed(ActionEvent e) {
//                  }
//              });
                tableModel.addRow(new Object[]{"1","Input"});
                tableModel.addRow(new Object[]{"2","Output"});//init. add 2 rows
                scrollPane.setViewportView(table);
            }
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("Add");
                okButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        int row = tableModel.getRowCount();
                        tableModel.addRow(new Object[]{row,"Input"});//click button to add a row
                    }
                });
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
        }
    }

}

I must click that cell twice to fire the editing event, otherwise, JComboBox isn't activated.

这只会在您更改列顺序后第一次尝试编辑列时发生。如果您将列切换回其原始位置,也会发生这种情况。

这就像 table 没有焦点,所以第一次鼠标点击给予 table 焦点。

因此您可以尝试在 TableColumModel 中添加一个 TableColumnModelListener。然后在 columnMoved 事件中你可以尝试使用 table.requestFocusInWindow()