我試圖在用戶編輯並輸入新值時更新JTable
,但我收到了投射錯誤。在運行時更新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)
相同的答案,閱讀Oracle教程,Model_To_View有很好的描述 – mKorbel