-3

I'm saving JTable content with this code:

@override
public void editingStopped(ChangeEvent ce) {

    PreparedStatement pstmt = null;
    try {
        int row = getEditingRow();
        int column = getEditingColumn();
        DefaultStyledDocument doc = (DefaultStyledDocument) getCellEditor().getCellEditorValue();

        doc.setDocumentFilter(null);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject((DefaultStyledDocument) doc);
        oos.flush();

        byte[] data = bos.toByteArray();

        oos.close();
        bos.close();

        String query = "update BOX_ROWS "
                    + "set COLUMN1= ? "
                    + "where BOX_ID=" + ID
                    + " and INDEX=" + row;
        pstmt = ReseachAssistantUI.conn.prepareStatement(query);
        pstmt.setObject(1, data);

        pstmt.executeUpdate();
        doc.setDocumentFilter(new MyDocumentFilter());

    } catch (SQLException ex) {
        Logger.getLogger(MyTable.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        JOptionPane.showMessageDialog(null, "MyTable - " + ex.getMessage());
    } catch (IOException ex) {
        Logger.getLogger(MyTable.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        JOptionPane.showMessageDialog(null, "MyTable - " + ex.getMessage());
    } finally {
        DBUtil.closePreparedStatement(pstmt, MyTable.class.getName());
    }

    super.editingStopped(ce);
}

and it runs fine on Windows. However when I run my app on Mac OS X the following message appears:

MyTable - com.apple.laf.AquaComboBoxUI

The table pop up editor does have 2 comboboxes on a tool bar, but I can't see what they have to do with this exception. Does anyone know why does it throw this exception? Is there something wrong with the way I serialize the object?

Igor
  • 1,532
  • 4
  • 23
  • 44

1 Answers1

4

It looks like you're trying to persist the contents of a changed cell after editing concludes but before the model is updated; this answer outlines the normal sequence of events. As you have found, the actual editor component may vary by platform. Instead of overriding JTable#editingStopped(), override TableModel#setValueAt(), where you know the row, column and type of the updated cell. Examples may be found here and here. This related example illustrates using a JComboBox as a CellEditor.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks, I'll give this a try a set as the answer if it works. – Igor Nov 24 '12 at 15:45
  • I finnally got a chance to test it and unfotunately it doesn't work. Here are the first lines of the stacktrace: `java.io.NotSerializableException: com.apple.laf.AquaComboBoxUI at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)` ... I don't get why the combobox is serialized. I just need the document serialized. – Igor Nov 28 '12 at 09:31
  • Serialize? Why not just write the model's data? – trashgod Nov 28 '12 at 12:12
  • Because I want to save the text styles too. But, how is the combobox serialized here instead of just the textpane document? – Igor Nov 28 '12 at 12:59
  • All concrete `javax.swing.text.Document` classes are `Serializable`. – trashgod Nov 28 '12 at 14:50
  • Why the exception then? Seems to throw it on some versions of _MAC OS X_ only. – Igor Nov 28 '12 at 15:53
  • Edit your question to include an [sscce](http://sscce.org/) that reproduces the problem on your machine, and I'll try it on mine. – trashgod Nov 28 '12 at 16:12
  • I tried to create a SSCCE, but it has at least 500 lines. That doesn't seem like a Short SCCE at all and I'm not sure it OK to post 500 lines here. – Igor Nov 29 '12 at 15:59