JTable Inhalte speichern

Aus Byte-Welt Wiki
Wechseln zu: Navigation, Suche

TableSave.jpg

Dieses Beispiel zeigt, wie wir den Zustand einer JTable speichern können (Daten, Spaltenbreite und -position), so dass sie nach einem Programmneustart wieder geladen werden kann.

/*
 * TableSave.java
 */
 
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import javax.swing.*;
 
public class TableSave extends JFrame {
 
    private JTable table;
    private MyTableModel tableModel;
    private MyColumnModel columnModel;
    private String filename = "TableSaveColumns.xml";
 
    public TableSave() {
        super("TableSaveColumns");
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);
        table = new JTable();
        getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
        //TableModel:
        tableModel = new MyTableModel();
        table.setModel(tableModel);
        //ColumnModel:
        columnModel = new MyColumnModel();
        table.setColumnModel(columnModel);
        //initialize:
        tableModel.addRow(new Object[]{new Integer(101), "2a", "3a", "4a"});
        tableModel.addRow(new Object[]{new Integer(102), "2b", "3b", "4b"});
        loadTable();
        //listener:
        addWindowListener(new WindowAdapter() {
 
            @Override
            public void windowClosing(final WindowEvent evt) {
                saveTable();
                dispose();
            }
        });
    }
 
    private void saveTable() {
        try {
            XMLEncoder o = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(filename)));
            o.writeObject(tableModel);
            o.writeObject(columnModel);
            o.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    private boolean loadTable() {
        try {
            XMLDecoder d = new XMLDecoder(new BufferedInputStream(new FileInputStream(filename)));
            tableModel = (MyTableModel) d.readObject();
            table.setModel(tableModel);
            columnModel = (MyColumnModel) d.readObject();
            table.setColumnModel(columnModel);
            d.close();
        } catch (FileNotFoundException ex) {
            return false;
        }
        return true;
    }
 
    public static void main(final String args[]) {
        EventQueue.invokeLater(new Runnable() {
 
            public void run() {
                new TableSave().setVisible(true);
            }
        });
    }
}
/*
 * MyTableModel.java
 * is a bean that can be saved/loaded using XMLEncoder/XMLDecoder.
 * A bean must always have a default constructor (without parameters)
 * and getters/setters for the attributes. It must be a "public" class.
 */
 
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
 
public class MyTableModel extends DefaultTableModel {
 
    //the default constructor is needed (there is no default constructor in DefaultTableModel):
    public MyTableModel() {
        super(0, 4);
    }
 
    //this setter is needed because it is missing in DefaultTableModel:
    public void setDataVector(Vector vector) {
        dataVector = vector;
    }
 
    //this getter is needed because it is missing in DefaultTableModel:
    public Vector getColumnIdentifiers() {
        return columnIdentifiers;
    }
 
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 0) {
            return Integer.class;
        }
        return String.class;
    }
}


/*
 * MyColumnModel.java
 * is a bean that can be saved/loaded using XMLEncoder/XMLDecoder
 * A bean must always have a default constructor (without parameters)
 * and getters/setters for the attributes. It must be a "public" class.
 */
 
import java.util.*;
import javax.swing.table.*;
 
public class MyColumnModel extends DefaultTableColumnModel {
 
    private Object[] columnData = new Object[]{
        "Title 1", 100, //column 1: header value, width
        "Title 2", 50, //column 2: header value, width
        "Title 3", 50, //column 3: header value, width
        "Title 4", 50 //column 4: header value, width
    };
 
    public MyColumnModel() {
        super();
        for (int i = 0; i < columnData.length; i += 2) {
            String title = (String) columnData[i];
            Integer width = (Integer) columnData[i + 1];
            TableColumn tableColumn = new TableColumn(i / 2, width);
            tableColumn.setHeaderValue(title);
            addColumn(tableColumn);
        }
    }
 
    //this getter is needed because it is missing in DefaultTableColumnModel:
    public Vector<TableColumn> getTableColumns() {
        return tableColumns;
    }
 
    //this setter is needed because it is missing in DefaultTableColumnModel:
    public void setTableColumns(Vector<TableColumn> tableColumns) {
        this.tableColumns = tableColumns;
    }
}