简体   繁体   中英

JTable changing column order and then editing cell raising bug

I have a strange problem with JTable.

  1. What I have

A two-column JTable. And the second column uses JComboBox as a cell editor.

  1. Problem

When I edit that JComboBox cell with the origin column order it works fine. 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. I must click that cell twice to fire the editing event, otherwise, JComboBox isn't activated.

  1. What I tried

I tried to use TableCellListener , but it raises the exception, such as column index is -1;

My 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.

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 . Then in the columnMoved event you can try using table.requestFocusInWindow()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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