这段代码有什么问题? [英] What's wrong in this code?

查看:133
本文介绍了这段代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码保存 JTable 内容:

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);
}

,它在Windows 上运行正常。但是,当我运行我的应用程序在Mac OS X 时,出现以下消息:

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?

推荐答案

看起来你正在努力坚持一个编辑之后更改的单元格结束,但更新模型之前。这个 answer 概述了正常的事件顺序。如您所见,实际的编辑器组件可能因平台而异。而不是覆盖 JTable#editingStopped(),覆盖 TableModel#setValueAt(),您知道更新单元格的行,列和类型。可以在此处这里。相关示例说明使用 JComboBox 作为 CellEditor

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.

这篇关于这段代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆