如何输出编辑过的Jtable数据?

时间:2016-11-25 17:13:25

标签: java xml swing pdf jtable

我在eclipse中编写程序。我需要为用户创建一个GUI来编辑XML文件,并通过新的xml文件和PDF输出编辑过的数据。

我已将数据保存在数组中并将其放在jTable中,但无法通过FileOutputStream保存编辑的数据(Jbutton"输出")。我尝试使用FileOutputStream(Jbutton"输出")成功输出数据,但在编辑后它不起作用。

它会显示如下错误:

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 testing.Saving$3.actionPerformed(Saving.java:90)
    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)

如何输出编辑过的数据?

GUI:

package testing;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;

public class Saving extends JFrame {

    private JPanel contentPane;
    private JTable table;
    private JTextField textField;
    private JButton btnOutput;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Saving frame = new Saving();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();

                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Saving() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
//      String[] array = {"text","apple","orange","tree"};
        table = new JTable();
        table.setModel(new DefaultTableModel(
            new Object[][] {
                {"Name","text","apple","orange","tree"},
            },
            new String[] {
                "New column", "New column", "New column"
            }
        ));
        table.setBounds(46, 41, 324, 69);
        contentPane.add(table);

        JButton btnSave = new JButton("Save");
        btnSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                DefaultTableModel model = (DefaultTableModel) table.getModel();
                model.setValueAt(textField.getText(),  table.getSelectedRow(),1);
                table.invalidate();
            }
        });
        btnSave.setBounds(46, 161, 87, 23);
        contentPane.add(btnSave);

        textField = new JTextField();
        textField.setBounds(46, 130, 96, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        btnOutput = new JButton("Output");
        btnOutput.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    ObjectOutputStream oos = new ObjectOutputStream(new
                    FileOutputStream("tableVM"));
                    oos.writeObject(table);
                    oos.close();
                    } catch (FileNotFoundException a) {
                    // TODO Auto-generated catch block
                    a.printStackTrace();
                    } catch (IOException a) {
                    // TODO Auto-generated catch block
                    a.printStackTrace();
                    }
            }
        });
        btnOutput.setBounds(143, 161, 87, 23);
        contentPane.add(btnOutput);
    }
}

输出到pdf:

package testing;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

import javax.swing.JTable;
import javax.swing.table.TableModel;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;

import javax.swing.JTable;
import javax.swing.table.TableModel;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfWriter;
public class testing {
public static void main(String[] args) throws DocumentException {
    Document document = new Document();
try {
ObjectInputStream ois = new ObjectInputStream(new
FileInputStream("tableVM"));
JTable table = (JTable) ois.readObject();
ois.close();
TableModel tm = table.getModel();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("Selected.pdf"));
document.open();
document.addCreationDate();
document.add(new Paragraph("Brand: "));
for (int i = 0; i < tm.getRowCount(); i++) {
for (int j = 0; j < tm.getColumnCount(); j++) {
     document.add(new Phrase(tm.getValueAt(i, j) + "\t"));
}
System.out.println();
}
document.close();
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

[&#34; java.io.NotSerializableException的属性&#34;]] [1

0 个答案:

没有答案