问题
I am trying to update a JTable
when a users edit and enters new values into it, but I am getting casting error.
Coding So Far:
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);
}
}
Error Message:
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)
回答1:
Your data
instance is a Vector
. On this line,
((Vector) data.get(row)).setElementAt(value, col);
you fetch a single element from the vector with data.get(row)
, which in your case is a String. Now you are trying to convert that String into a Vector
with (Vector)
which obviously doesn't work.
Do you want to safe a vector in a vector to produce a table? Maybe defining the type of the generic vector will help you find your problem, e.g. Vector<Vector<String>> data
.
回答2:
It's easy to explain, on the one hand you fill your data vector with individual strings from your file. Later on in the setValueAt, you treat the values of your vector as another vector (which they are not since you put strings in there).
I'd suggest you define your Vector properly (depending on what you actually want in there). I'm guessing you want either this
Vector<String> data
or
Vector<Vector<String>> data
回答3:
You should see warnings "Vector is a raw type..."
First thing:
Vector data;
should be
Vector<String> data;
Always look at the warnings, they are there for a reason.
Second thing:
Data is already of type Vector holding Strings. You cast the content of data (which is a String) to Vector, which obviously doesn't work.
((Vector) data.get(row)).setElementAt(value, col);
should be
data.setElementAt(value, col);
Form your code I would assume that this is wrong either, because it looks like you want to have a matrix strcuture. So you must declare a vector holding vectors of type string.
Vector<Vector<String>>data = null;
then you can access it like this:
Vector<String>row = data.get(row);
String value = row.get(column);
And try to get rid of casts. A cast is often an indicator that there might be something wrong with your design and prevents the parser from catching errors at compile time.
来源:https://stackoverflow.com/questions/16056222/update-the-jtable-at-runtime