Deleting item from JTable & serialize them into file

时间:2016-07-11 20:20:57

标签: java swing jtable

I have a program which have a jtable implemented, i used a array to store header & a bidimentional array of Object to store the data.

My JTable doesn't implement a VECTOR instead i use Simples Arrays.

 String[] headerColum = {"ID", "DESC", "DATE", "FIXEDDATE"};

      Object[][] data = {{"100414774", "harry", new Date(), dateFixed},{"122054", "Paul", new Date(), dateFixed},
      {"265015", "Matwe", new Date(), dateFixed}, {"12345", "unkonw Guy", new Date(), dateFixed}};


      TableModel tableModel = new DefaultTableModel(data, headerColum); 

      JTable table = new JTable(tableModel); //tableModel
      table.setModel(tableModel);

Aditional i had creatd a Jbutton which implement this method to delete a row:

JButton deleteBnt = new JButton("delete row");
      deleteBnt.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent arg0)
        {
            if(table.getSelectedRow() != -1)
            {
                DefaultTableModel defaultableModel = (DefaultTableModel)table.getModel();
                defaultableModel.removeRow(table.getSelectedRow());
                System.out.println("Delete complete");
            }

        }
    });

and it works fine, but when i try to save the Jtable's modelt into a file a error is thrown. Save button:

JButton save = new JButton("save data");
      save.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
              try
                {
                    DefaultTableModel model2 = (DefaultTableModel)table.getModel();
                    ObjectOutputStream fileOut = new ObjectOutputStream(new FileOutputStream("C:/Users/Harry/Desktop/clients.txt"));
                    fileOut.writeObject(model2);
                    fileOut.close();
                    System.out.println("writing Done");
                } catch (IOException ex)
                {
                    ex.printStackTrace();
                }
        }
    });

error:

java.io.NotSerializableException: javax.swing.JTable$CellEditorRemover
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteObject(Unknown Source)
    at javax.swing.JTable.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at javax.swing.event.EventListenerList.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at FilteringJTable_add_deleteRow_save$4.actionPerformed(FilteringJTable_add_deleteRow_save.java:141)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

How can i serialize (Save it into a File without having this error?)?

0 个答案:

没有答案