JTable/DefaultTableModel 中的 TableModelListener 和 addRow 干扰

TableModelListener and addRow interference in JTable/DefaultTableModel

TableModelListener(第 87 行/HERE-1)是否干扰我的 JTable 的 addRow 方法(第 139 行,HERE-2)?如果是,我该如何解决?

此代码可以编译,但当我使用 "add row" 按钮时会抛出错误。 错误是: (堆栈跟踪要长很多,但我已将其缩减为关于我的 class 而不是 java API 的行)

线程异常 "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException:

在 HPLC.addRow(HPLC.java:142)

在 HPLC.lambda$new$1(HPLC.java:109)

自从我添加了 TableModelListener 之后,这一直是个问题。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

class HPLC extends JFrame
{
    public static void main (String [] args)
    {
        new HPLC();
    }
    HPLC()
    {
        //create window
        JFrame frame = new JFrame();
        frame.setTitle("HPLC Calculator");
        frame.setSize(500,750);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        //row 0
        JLabel title = new JLabel("HPLC Gradient Calculator");
        gridBagAdd(panel, title, 0,0,4,1, GridBagConstraints.CENTER);

        //row 1
        JLabel flowLabel = new JLabel("Flow rate");
        gridBagAdd(panel, flowLabel, 0,1,1,1, GridBagConstraints.CENTER);

        JTextField flow = new JTextField(5);
        gridBagAdd(panel, flow, 1,1,1,1, GridBagConstraints.CENTER);

        JLabel mlMinLabel = new JLabel("ml/min");
        gridBagAdd(panel, mlMinLabel, 2,1,1,1, GridBagConstraints.CENTER);

        //row 2
        JLabel numInjLabel = new JLabel("Number of injections");
        gridBagAdd(panel, numInjLabel, 0,2,1,1, GridBagConstraints.CENTER);

        JTextField numInj = new JTextField(5);
        gridBagAdd(panel, numInj, 1,2,1,1, GridBagConstraints.CENTER);

        //row 3
        JLabel volALabel = new JLabel("Volume of A");
        gridBagAdd(panel, volALabel, 0,3,1,1, GridBagConstraints.CENTER);

        JTextField volA = new JTextField(5);
        volA.setEditable(false);
        gridBagAdd(panel, volA, 1,3,1,1, GridBagConstraints.CENTER);

        JLabel volBLabel = new JLabel("Volume of B");
        gridBagAdd(panel, volBLabel, 2,3,1,1, GridBagConstraints.CENTER);

        JTextField volB = new JTextField(5);
        volB.setEditable(false);
        gridBagAdd(panel, volB, 3,3,1,1, GridBagConstraints.CENTER);

        //row 4
        JButton calculate = new JButton("calculate");
        gridBagAdd(panel, calculate, 0,4,1,1, GridBagConstraints.CENTER);

        JButton addRow = new JButton("add row");
        gridBagAdd(panel, addRow, 1,4,1,1, GridBagConstraints.CENTER);

        JButton removeRow = new JButton("remove row");
        gridBagAdd(panel, removeRow, 2,4,1,1, GridBagConstraints.CENTER);

        //the gradient table
        String[] columns = {"Time", "%A", "%B"};
        DefaultTableModel model = new DefaultTableModel(columns, 2)
        {
            public boolean isCellEditable(int row, int column)
            //this overrides the default method to suit these conditions to make the 3rd column read only
            {
                if (column == 2)
                    return false;
                return true;
            }
        };
        JTable table = new JTable(model);
        table.setFillsViewportHeight(true);
        JScrollPane tablePane = new JScrollPane(table);
        gridBagAdd(panel, tablePane, 0,5,4,4, GridBagConstraints.CENTER);
        table.getModel().addTableModelListener(new TableModelListener()
   //HERE-1 {
                public void tableChanged(TableModelEvent e)
                //overriding the TableModelListener method
                {
                    //the basics in order to get the table cell
                    int row = e.getFirstRow();
                    int column = e.getColumn();
                    TableModel model = (TableModel)e.getSource();
                    String ColumnName = model.getColumnName(column);
                    Object data = model.getValueAt(row, column);

                    //what i do with that cell
                    volB.setText(data.toString());
                }   
            });

        //button methods
        calculate.addActionListener(e -> calculate(model, volA));
        addRow.addActionListener(e -> addRow(model));
        removeRow.addActionListener(e -> removeRow(model));

        //make visible
        frame.add(panel);
        frame.setVisible(true);
    }
    //method for placement of components
    private void gridBagAdd(JPanel p, JComponent c, int x, int y, int width, int height, int align)
    {
        GridBagConstraints gc = new GridBagConstraints();
        gc.gridx = x;
        gc.gridy = y;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.weightx = 100;
        gc.weighty = 100;
        gc.insets = new Insets(5,5,5,5);
        gc.anchor = align;
        gc.fill = GridBagConstraints.NONE;
        p.add(c, gc);
    }
    //button method execution
    private void calculate(DefaultTableModel model, JTextField volA)
    {
            int rowCount = model.getRowCount();
            String msg = rowCount + " rows";
            volA.setText(msg);
    }
    private void addRow(DefaultTableModel model)
    {
        int colCount = model.getColumnCount();
//HERE-2 Object [] newRow = new Object[colCount];
        model.addRow(newRow);
    }
    private void removeRow(DefaultTableModel model)
    {
        int rowCount = model.getRowCount();
        if (rowCount > 2)
            model.removeRow(rowCount -1);
    }
}

我想让 table 做的是根据 table 的内容创建一个数组,以便执行计算。已添加 TableModelListener,以便 %B 列将自动完成以给出总数 100%。

我是自学的,这是第一次有人看到我的代码。如果您有任何关于如何改进我的写作方法的建议,我将很乐意听到。

您要添加一个新行,因此 TableModelListener 将 return 事件中的 ALL_COLUMNS 常量用于列 属性,值为 -1 reference

int column = e.getColumn(); // here

然后尝试在此处使用该 -1 值:

Object data = model.getValueAt(row, column);

这显然行不通。

解决方案:在使用前检查监听器中的列值。也许您会希望为您的列值使用 int 文字。

The TableModelListener has been added so that the %B column will auto complete to give a total of 100%

该事件会告诉您它产生的原因。在我看来,您只想在更改单元格中的值时执行代码。

所以代码应该是这样的:

public void tableChanged(TableModelEvent e)
{
    if (e.getType() == TableModelEvent.UPDATE)
    {
        // add processing here
    }
}

有关工作示例,请查看:JTable -> TableModeListener