gpt4 book ai didi

java - TableModelListener 和多列验证

转载 作者:行者123 更新时间:2023-11-30 07:36:31 25 4
gpt4 key购买 nike

第一次在这里发帖,如有错误请见谅

我正在处理一个 JTable,其中的列数据必须验证一些参数,例如:

第 3 列值 > 30
第 4 列值 > 10
第 5 列值 > 4

前 2 列也是“自动”填充的,其余列为 0。

如果该数据正确,我将在第 5 列中显示勾号图像,否则,我将显示警告图像。

为了验证这一点,我使用以下代码

    ImageIcon accept = new javax.swing.ImageIcon(getClass().getResource("/resources/accept.png"));
ImageIcon deny = new javax.swing.ImageIcon(getClass().getResource("/resources/exclamation.png"));

public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
double d1 = Double.valueOf(jTable.getValueAt(row, 2).toString());
double d2 = Double.valueOf(jT.getValueAt(row, 3).toString());
double d3 = Double.valueOf(jT.getValueAt(row, 4).toString());

if(d1>MAX_A||d2>MAX_B||d3>MAX_C){
jTable.setValueAt(deny, row, 5);
}
else{
jTable.setValueAt(accept, row, 5);
}
}

这段代码的问题是返回 Stack Overflow,我不知道如何处理。

是否有任何其他方法可以在暗示多个单元格的表上实现一些 validator ?

提前致谢。

最佳答案

The problem of this code is thatreturns a Stack Overflow, and I don'tknow how to handle this.

问题是您的代码在模型监听器中设置了一个值,因此生成了另一个 tableChanged 事件。你的代码应该是这样的:

if (e.getColumn() != 5)
// do your code

我没有发现使用 TableModelListener 根据另一列中的数据动态设置列值的问题。这是一个简单的例子:

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

public class TableProcessing extends JPanel implements TableModelListener
{
public TableProcessing()
{
String[] columnNames = {"Item", "Quantity", "Price", "Cost"};
Object[][] data =
{
{"Bread", new Integer(1), new Double(1.11), new Double(1.11)},
{"Milk", new Integer(1), new Double(2.22), new Double(2.22)},
{"Tea", new Integer(1), new Double(3.33), new Double(3.33)},
{"Cofee", new Integer(1), new Double(4.44), new Double(4.44)}
};

DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
// Returning the Class of each column will allow different
// renderers to be used based on Class
@Override
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}

// The Cost is not editable
@Override
public boolean isCellEditable(int row, int column)
{
return (column == 3) ? false : true;
}
};
model.addTableModelListener( this );

JTable table = new JTable( model );
table.setPreferredScrollableViewportSize(table.getPreferredSize());

JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );

String[] items = { "Bread", "Milk", "Tea", "Coffee" };
JComboBox<String> editor = new JComboBox<String>( items );

DefaultCellEditor dce = new DefaultCellEditor( editor );
table.getColumnModel().getColumn(0).setCellEditor(dce);
}

/*
* The cost is recalculated whenever the quantity or price is changed
*/
public void tableChanged(TableModelEvent e)
{
if (e.getType() == TableModelEvent.UPDATE)
{
int row = e.getFirstRow();
int column = e.getColumn();

if (column == 1 || column == 2)
{
TableModel model = (TableModel)e.getSource();
int quantity = ((Integer)model.getValueAt(row, 1)).intValue();
double price = ((Double)model.getValueAt(row, 2)).doubleValue();
Double value = new Double(quantity * price);
model.setValueAt(value, row, 3);
}
}
}

private static void createAndShowGUI()
{
JFrame frame = new JFrame("Table Model Listener");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TableProcessing());
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}

public static void main(String[] args) throws Exception
{
EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}

关于java - TableModelListener 和多列验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3540661/

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