gpt4 book ai didi

java - 在运行时更新 Jtable?

转载 作者:太空宇宙 更新时间:2023-11-04 07:41:01 24 4
gpt4 key购买 nike

当用户编辑并输入新值时,我尝试更新 JTable,但出现转换错误。

到目前为止的编码:

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

public class Stackq extends AbstractTableModel {
Vector data;
Vector columns;

public Stackq() {
String line;
data = new Vector();
columns = new Vector();
int count = 0;


try {
FileInputStream fis = new FileInputStream("D:/joy/text/registration.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
StringTokenizer st1 = new StringTokenizer(br.readLine(), "\t");
while (st1.hasMoreTokens()) {
columns.addElement(st1.nextToken());
count++;
}
while ((line = br.readLine()) != null) {
StringTokenizer st2 = new StringTokenizer(line, "\t");
for (int i = 0; i < count; i++) {
if (st2.hasMoreTokens())
data.addElement(st2.nextToken());
else
data.addElement("");
}
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}



public void setValueAt(Object value, int row, int col){
((Vector) data.get(row)).setElementAt(value, col);
fireTableCellUpdated(row,col);
}

public boolean isCellEditable(int row, int col){
//if (4 == col){
return true;
}
//else {
// return false;
// }
//}

public void insertData(Object[] values){
data.add(new Vector());
for(int i =0; i<values.length; i++){
((Vector) data.get(data.size()-1)).add(values[i]);
}
fireTableDataChanged();
}

public void removeRow(int row){
data.removeElementAt(row);
fireTableDataChanged();
}


public int getRowCount() {
return data.size() / getColumnCount();
}

public int getColumnCount() {
return columns.size();
}

public Object getValueAt(int rowIndex, int columnIndex) {
return (String) data.elementAt((rowIndex * getColumnCount())
+ columnIndex);
}

public String getColumnName(int i){
return (String)columns.get(i);
}


public static void main(String s[]) {
Stackq model = new Stackq();
JTable table = new JTable();
table.setModel(model);



JScrollPane scrollpane = new JScrollPane(table);
JPanel panel = new JPanel();
panel.add(scrollpane);
JFrame frame = new JFrame();
frame.add(panel, "Center");
frame.pack();
frame.setVisible(true);


}
}

错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Vector
at Stackq.setValueAt(Stackq.java:45)
at javax.swing.JTable.setValueAt(JTable.java:2709)
at javax.swing.JTable.editingStopped(JTable.java:4711)
at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:125)
at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(DefaultCellEditor.java:350)
at javax.swing.DefaultCellEditor.stopCellEditing(DefaultCellEditor.java:215)
at javax.swing.JTable$GenericEditor.stopCellEditing(JTable.java:5465)
at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:980)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:263)
at java.awt.Component.processMouseEvent(Component.java:6260)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)

最佳答案

您的data实例是 Vector 。在这条线上,

((Vector) data.get(row)).setElementAt(value, col);

您可以使用 data.get(row) 从 vector 中获取单个元素,在你的例子中是一个字符串。现在您尝试将该字符串转换为 Vector(Vector)这显然行不通。

您想将 vector 安全地保存在 vector 中以生成表格吗?也许定义通用 vector 的类型将帮助您找到问题,例如Vector<Vector<String>> data

关于java - 在运行时更新 Jtable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16056222/

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