简体   繁体   English

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

[英]JTable changing column order and then editing cell raising bug

I have a strange problem with JTable. 我对JTable有一个奇怪的问题。

  1. What I have 我有的

A two-column JTable. 一个两列的JTable。 And the second column uses JComboBox as a cell editor. 第二列使用JComboBox作为单元格编辑器。

  1. Problem 问题

When I edit that JComboBox cell with the origin column order it works fine. 当我用origin列顺序编辑该JComboBox单元时,它可以正常工作。 But When I change the column first, such as switching that 2 column(JComboBox column becomes fist), then I edited the JComboBox cell raising Bug. 但是,当我首先更改列时,例如切换到第二列(JComboBox列变为第一拳),然后我编辑了JComboBox单元格,引发了Bug。 I must click that cell twice to fire the editing event, otherwise, JComboBox isn't activated. 我必须单击两次该单元格以触发编辑事件,否则,不会激活JComboBox。

  1. What I tried 我尝试了什么

I tried to use TableCellListener , but it raises the exception, such as column index is -1; 我尝试使用TableCellListener ,但是它引发了异常,例如列索引为-1;

My SSCCE: 我的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. 我必须单击两次该单元格以触发编辑事件,否则,不会激活JComboBox。

This only happens the first time you try to edit the column after changing column order. 这仅在更改列顺序后第一次尝试编辑列时发生。 It will also happens if you switch the column back to its original position. 如果将列切换回其原始位置,也会发生这种情况。

It is like the table doesn't have focus so the first mouse click is giving the table focus. 就像桌子没有焦点一样,因此第一次单击鼠标即可赋予桌子焦点。

So you could try to add a TableColumnModelListener to the TableColumModel . 因此,您可以尝试将TableColumnModelListener添加到TableColumModel Then in the columnMoved event you can try using table.requestFocusInWindow() 然后,在columnMoved事件中,您可以尝试使用table.requestFocusInWindow()

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

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