Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate the Jtable at Runtime?
    primarykey
    data
    text
    <p>I am trying to update a <code>JTable</code> when a users edit and enters new values into it, but I am getting casting error.</p> <p>Coding So Far:</p> <pre><code>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 &lt; 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&lt;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); } } </code></pre> <p>Error Message:</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload