gpt4 book ai didi

java - JTable/DefaultTableModel 中的 TableModelListener 和 addRow 干扰

转载 作者:行者123 更新时间:2023-12-01 18:02:01 27 4
gpt4 key购买 nike

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

此代码可以编译,但当我使用“添加行”按钮时会引发错误。错误是:(堆栈跟踪要长得多,但我已将其减少为有关我的类而不是 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);
}
}

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

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

最佳答案

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

关于java - JTable/DefaultTableModel 中的 TableModelListener 和 addRow 干扰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40192911/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com