JTable Inhalte speichern: Unterschied zwischen den Versionen

Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springen
(Die Seite wurde neu angelegt: „Dieses Beispiel zeigt, wie wir den Zustand einer JTable speichern können, so dass sie nach einem Programmneustart wieder geladen werden kann. <code=java> /* * …“)
 
K
 
(6 dazwischenliegende Versionen von 3 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
Dieses Beispiel zeigt, wie wir den Zustand einer JTable speichern können, so dass sie nach einem Programmneustart wieder geladen werden kann.
+
[[Bild:TableSave.jpg]]
  
<code=java>
+
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.
 +
 
 +
<syntaxhighlight lang="java">
 
/*
 
/*
 
  * TableSave.java
 
  * TableSave.java
Zeile 82: Zeile 84:
 
}
 
}
  
</code=java>
+
</syntaxhighlight>
  
<code=java>
+
<syntaxhighlight lang="java">
 
/*
 
/*
 
  * MyTableModel.java
 
  * MyTableModel.java
 
  * is a bean that can be saved/loaded using XMLEncoder/XMLDecoder.
 
  * is a bean that can be saved/loaded using XMLEncoder/XMLDecoder.
 
  * A bean must always have a default constructor (without parameters)
 
  * A bean must always have a default constructor (without parameters)
  * and getters/setters for the attributes.
+
  * and getters/setters for the attributes. It must be a "public" class.
 
  */
 
  */
  
Zeile 97: Zeile 99:
 
public class MyTableModel extends DefaultTableModel {
 
public class MyTableModel extends DefaultTableModel {
  
     //the default constructor is needed (there is no default constructor in DefaultTableModel:
+
     //the default constructor is needed (there is no default constructor in DefaultTableModel):
 
     public MyTableModel() {
 
     public MyTableModel() {
 
         super(0, 4);
 
         super(0, 4);
Zeile 120: Zeile 122:
 
     }
 
     }
 
}
 
}
</code=java>
+
</syntaxhighlight>
  
  
<code=java>
+
<syntaxhighlight lang="java">
 
/*
 
/*
 
  * MyColumnModel.java
 
  * MyColumnModel.java
 
  * is a bean that can be saved/loaded using XMLEncoder/XMLDecoder
 
  * is a bean that can be saved/loaded using XMLEncoder/XMLDecoder
 
  * A bean must always have a default constructor (without parameters)
 
  * A bean must always have a default constructor (without parameters)
  * and getters/setters for the attributes.
+
  * and getters/setters for the attributes. It must be a "public" class.
 
  */
 
  */
  
Zeile 165: Zeile 167:
 
}
 
}
  
</code=java>
+
</syntaxhighlight>
 
 
  
[[Kategorie:Java]]
 
 
[[Kategorie:Tutorials (Java)]]
 
[[Kategorie:Tutorials (Java)]]
 
[[Kategorie:Swing]]
 
[[Kategorie:Swing]]
 
[[Kategorie:Java-Codeschnipsel]]
 
[[Kategorie:Java-Codeschnipsel]]
 +
[[Kategorie:JTable]]

Aktuelle Version vom 28. März 2018, 09:33 Uhr

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