简体   繁体   中英

How to save a JTable after changes were made

I want to save changes that are made in the table and then when I want to load it Ill be able to every time I relaunch the program. Here is my code so far (Note that I have looked for an answer to this but I am not the best at java):

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;


public class Table2 extends JFrame implements ActionListener{


//String[] colomn = {"Name","Age","Gender"};
Object[][] data = {{"John","20","Male"}};
JTable table = new JTable(new DefaultTableModel(data, new Object[]{"Name", "Age","Gender"})){

    public Component prepareRenderer(TableCellRenderer r, int data, int col){ //gives a pattern to the table cells.
        Component c = super.prepareRenderer(r, data, col);

        if(data % 2 == 0){
            c.setBackground(Color.PINK);
        }
        else{
            c.setBackground(Color.LIGHT_GRAY);
        }
        return c;
    }

};


//JTable table = new JTable(data,colomn);

public Table2(){

    table.setPreferredScrollableViewportSize(new Dimension(300,200));
    table.setFillsViewportHeight(true);

    JScrollPane scrollpane = new JScrollPane(table);
    add(scrollpane);
}

public static void main(String[] args) {

    Table2 table = new Table2();
    table.setLayout(new FlowLayout());
    table.setVisible(true);
    table.setSize(400,300);
    table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    table.setResizable(false);
    table.setTitle("Table Prictice on JFrame");
    //table.pack();

    JButton button = new JButton("Add");
    table.add(button);
    button.addActionListener(table);
}

public void actionPerformed(ActionEvent e) {

    DefaultTableModel model = (DefaultTableModel) table.getModel();
    model.addRow(new Object[]{"", "", ""});
    System.out.println("Action Performed!");
}

}

You can save your data array with ObjectOutputStream to a binary file and you can read it from there. To save you can use something like this:

ObjectOutputStream outstream = new ObjectOutputStream(new FileOutputStream("filename"));
        outstream.writeObject(data);
        outstream.close();

To read you can use:

ObjectInputStream instream = new ObjectInputStream(newFileInputStream("filename"));
        Object[][] data = (Object[][]) instream.readObject();
        instream.close();

To get the changed data take a look at How to Retrieve JTable Data as an Array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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