如何保存将AbstractTableModel扩展到文件中的类,只要它发生变化?

时间:2016-01-14 19:21:44

标签: java swing reference jtable

我有两节课。那里的第一个类mainWindow extends JFrame我有一个JTable table1。 在我的第二堂课中,我需要得到这张桌子。当我在mainWindow中生成一个getter时,我需要mainWindow win = new mainWindow();才能跟随win.getTable1();。问题是mainWindow win = new mainWindow();事情打开窗口,所以我最终得到了两次。如何在第二堂课中获得对table1的引用?

mainWindow.java

package windows;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import helpers.*;


public class mainWindow extends JFrame {

    private final String fileName = "Studenten";
    public studentTableModel model;
    private JPanel rootPanel;
    private JComboBox comboBox1;
    private JTable table1;
    private JButton generierenButton;
    private calculations calc = new calculations();

    public mainWindow() throws IOException {
        super("Zufallsgenerator fürs Repetieren");
        pack();

        setContentPane(rootPanel);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1000, 700);

        List<student> studentList = calc.readFromFile("Studenten");
        model = new studentTableModel(studentList);
        table1.setModel(model);
        setVisible(true);
    }
}

studentTableModel.java

package helpers;

import windows.mainWindow;

import javax.swing.table.AbstractTableModel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class studentTableModel extends AbstractTableModel {
    private calculations calc = new calculations();

    private final List<student> students;

    private final String[] columnNames = new String[] {
            "Id", "Name", "Bereits x-Mal repetiert", "Wahrscheinlichkeit", "im Pot"
    };

    private final Class[] columnClass = new Class[] {
            String.class, String.class, int.class, double.class, Boolean.class
    };

    public studentTableModel(List<student> students){
        this.students = students;
    }

    @Override
    public String getColumnName(int column){
        return columnNames[column];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex)
    {
        return columnClass[columnIndex];
    }

    @Override
    public int getColumnCount()
    {
        return columnNames.length;
    }

    @Override
    public int getRowCount()
    {
        return students.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex)
    {
        student row = students.get(rowIndex);
        if(0 == columnIndex) {
            return row.getId();
        }
        else if(1 == columnIndex) {
            return row.getName();
        }
        else if(2 == columnIndex) {
            return row.getAnzahlRepetitonen();
        }
        else if(3 == columnIndex) {
            return row.getWahrscheinlichkeit();
        }
        else if(4 == columnIndex) {
            return row.isInPot();
        }
        return null;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex)
    {
        return true;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex)
    {
        student row = students.get(rowIndex);
        if(0 == columnIndex) {
            row.setId((String) aValue);
        }
        else if(1 == columnIndex) {
            row.setName((String) aValue);
        }
        else if(2 == columnIndex) {
            row.setAnzahlRepetitonen((Integer) aValue);
        }
        else if(3 == columnIndex) {
            row.setWahrscheinlichkeit((Double) aValue);
        }
        else if(4 == columnIndex) {
            row.setInPot((Boolean) aValue);
        }
        List<student> students1 = new ArrayList<student>();

        //here I need to get the table data to save it to a file using the method in the next class

        calc.saveToFile("newStudents", students1);

    }
}

calculations.java

package helpers;

import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class calculations {

    public void saveToFile(String file,List<student> studentList){
        int length = studentList.size();

        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(file);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        for (helpers.student student : studentList){
            String id = student.getId();
            String name = student.getName();
            int rep = student.getAnzahlRepetitonen();
            double wahrscheinlichkeit = student.getWahrscheinlichkeit();
            boolean inPot = student.isInPot();

            try {
                fileWriter.write(id + ";" + name + ";" + Integer.toString(rep) + ";" + Double.toString(wahrscheinlichkeit) + ";" + Boolean.toString(inPot) + "\n");
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
        }

        try {
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public List<student>readFromFile(String file){
        List<student> studentList = new ArrayList<student>();
        String line = null;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }

        try {
            while ((line = br.readLine()) != null){
                student st = null;
                String[] cols = line.split(";");
                String id = cols[0];
                String name = cols[1];
                int rep = Integer.parseInt(cols[2]);
                double wahrscheinlichkeit = Double.parseDouble(cols[3]);
                boolean isPot = Boolean.parseBoolean(cols[4]);
                st = new student(id, name,rep, wahrscheinlichkeit, isPot);
                studentList.add(st);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return studentList;
    }

    public List<student> getTableData(JTable table, List<student> studentList){
        int rows = table.getModel().getRowCount();
        for (int i = 0; rows >= i;){
            String id = (String) table.getModel().getValueAt(i, 0);
            String name = (String) table.getModel().getValueAt(i, 1);
            int anzahl = (int) table.getModel().getValueAt(i, 2);
            double prob = (Double) table.getModel().getValueAt(i, 3);
            boolean inPot = (Boolean) table.getModel().getValueAt(i, 4);
            studentList.add(new student(id, name, anzahl, prob, inPot));
            System.out.println("Read row " + i);
        }
        return studentList;
    }

}

的ChangeListener

table1.getModel().addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                System.out.println("Change detected");
                List<student> newStudentList = new ArrayList<student>();
                calc.getTableData(table1, newStudentList);
                System.out.println("Now saving...");
                calc.saveToFile("newStudents", newStudentList);
            }
        });

1 个答案:

答案 0 :(得分:2)

  

一旦表格发生变化,程序就应该打印出“检测到更改”,但没有任何反应。

addTableModelListener ......

确实如此
  

[...]在每次发生数据模型更改时通知的列表中添加一个侦听器。

但是通过扩展抽象类AbstractTableModel,您有责任使用提供的方法(例如fireTableDataChanged())进行此类通知。当你这样做取决于你,但方法setValueAt看起来像是一个通知听众的好地方,因为它是改变表数据的那个。