简体   繁体   English

如何将数据加载到Jtable

[英]How to load data into Jtable

I have been doing a small program to test save and load data by jtable in java. 我一直在做一个小程序,通过Java中的jtable测试保存和加载数据。 there is no problem in saving but can't load the data. 保存没有问题,但无法加载数据。 when I load the data table shows empty, nothing in there. 当我加载数据表时显示为空,那里什么也没有。 May I have any suggestions! 请问我有什么建议! here is the Code: 这是代码:

       public class NewOne extends JFrame {

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

            public NewOne() {
    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);

    JButton Button = new JButton("save");
    Button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
             saveTable();
        }
    });
    Button.setBounds(10, 11, 89, 23);
    contentPane.add(Button);

    JButton Button1 = new JButton("load");
    Button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            loadTable();
        }
    });
    Button1.setBounds(150, 11, 89, 23);
    contentPane.add(Button1);

    table = new JTable();
    table.setModel(new DefaultTableModel(
        new Object[][] {
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
            {null, null, null, null, null},
        },
        new String[] {
            "New column", "New column", "New column", "New column", "New column"
        }
    ));
    table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    table.setBounds(20, 45, 372, 80);
    contentPane.add(table);
}

private JFileChooser myJFileChooser = new JFileChooser(new File("."));

private DefaultTableModel tableModel = new DefaultTableModel();
      private JTable myJTable = new JTable(tableModel);
      private JTable table;

private void saveTable() {
    if (myJFileChooser.showSaveDialog(this) ==
            JFileChooser.APPROVE_OPTION ) {
        saveTable(myJFileChooser.getSelectedFile());
    }
}

private void saveTable(File file) {
    try {
        ObjectOutputStream out = new ObjectOutputStream(
                new FileOutputStream(file));
            out.writeObject(tableModel.getDataVector());
            out.writeObject(getColumnNames());
            out.close();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
}

private Vector<String> getColumnNames() {
    Vector<String> columnNames = new Vector<String>();
    for (int i = 0; i < myJTable.getColumnCount(); i++)
        columnNames.add(myJTable.getColumnName(i) );
        return columnNames;
}

private void loadTable() {
    if (myJFileChooser.showOpenDialog(this) ==
            JFileChooser.APPROVE_OPTION )
        loadTable(myJFileChooser.getSelectedFile());
}

private void loadTable(File file) {
    try {
        ObjectInputStream in = new ObjectInputStream(
        new FileInputStream(file));
        Vector rowData = (Vector)in.readObject();
        Vector columnNames = (Vector)in.readObject();
        tableModel.setDataVector(rowData, columnNames);
        in.close();
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }
}
 }

I guess your problem lies here: 我猜你的问题出在这里:

 private void saveTable(File file) {
        try {
            ObjectOutputStream out = new ObjectOutputStream(
                    new FileOutputStream(file));
                out.writeObject(tableModel.getDataVector());
                out.writeObject(getColumnNames());
                out.close();
            }

Your tableModel is a new DefaultTableModel(); 您的tableModel是一个new DefaultTableModel(); if i haven't missread your code. 如果我没有错过您的代码。 per documentation : " DefaultTableModel() Constructs a default DefaultTableModel which is a table of zero columns and zero rows. " therefore you put an empty file to your OutputSream. 每个文档:“ DefaultTableModel() 构造一个默认的DefaultTableModel,它是零列零行的表。 ”因此,您将一个空文件放入OutputSream。

Maybe you just used he wrong table after all do you want to get the content of your myJTable ? 也许您只是使用了错误的表,毕竟您想获取myJTable的内容吗? If that is true the solution should look like: 如果是这样,则解决方案应如下所示:

private void saveTable(File file) {
            try {
                ObjectOutputStream out = new ObjectOutputStream(
                        new FileOutputStream(file));
                    out.writeObject(myJTable.getDataVector());
                    out.writeObject(getColumnNames());
                    out.close();
                }

Correct me if I am wrong. 如果我错了,请纠正我。 Its difficult to look at foreign code :P 很难看外码:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM