如何从java MVC中的arraylist .TXT文件显示JTable中的记录? [英] How to display records in a JTable from an arraylist .TXT file in java MVC?

查看:90
本文介绍了如何从java MVC中的arraylist .TXT文件显示JTable中的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,这是我的主屏幕: () 我有2个文件:"Patient.txt"和"treatment.txt",其中包含多个患者和治疗的记录.

Currently, this is my main screen: () I have 2 files: "patient.txt" and "treatment.txt" which hold records of multiple patients and treatments.

我想做的是,每当我单击显示治疗"或显示患者"时,都在一个漂亮的JTable中显示所有这些记录,如下所示:

What I’m trying to do is to display all of those records in a nice JTable whenever I click "Display Treatments" or "Display Patients", in a screen like so:

我正在为此医院管理系统使用MVC模型(带有HMSGUIModel.java,HMSGUIView.java,HMSGUIController.java,HMSGUIInterface.java文件),并使用以下代码添加记录:

I am using an MVC model for this Hospital Management System (with HMSGUIModel.java, HMSGUIView.java, HMSGUIController.java, HMSGUIInterface.java files), and add records using the following code:

 FileWriter tfw = new FileWriter(file.getAbsoluteFile(), true); 
                BufferedWriter tbw = new BufferedWriter(tfw);
                tbw.write(this.view.gettNumber() + "," + this.view.gettName() + "," +     this.view.gettDoctor() + "," + this.view.gettRoom());
                tbw.newLine();
                tbw.flush();
                JOptionPane.showMessageDialog(null, "Successfully added treatment!"); }

请帮助我如何添加阅读器,以将文本文件中的所有记录显示到表中?

Please help on how I can add a reader as well, to display all the records from the text file to a table?

非常感谢!

推荐答案

与您的MVC保持一致,您可以创建一个TableModel,它知道如何读取给定的患者记录.

Keeping in line with your MVC, you could create a TableModel which knew how to read a give patient record.

不过,就个人而言,我更希望将患者数据的管理与视图分开,因此视图并不在乎数据的来源.

Personally though, I'd prefer to separate the management of the patient data from the view, so the view didn't care about where the data came from.

为此,我将从创建一个Patient对象和一个Treatment对象开始,这些对象会将数据保存在一个自包含的实体中,从而简化了管理工作.

To this end, I would start by creating a Patient object and a Treatment object, these would hold the data in a self contained entity, making the management simpler...

您需要读取这些数据并解析结果...

You would need to read this data in and parse the results...

List<Treatment> treatments = new ArrayList<Treatment>(25);
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String text = null;
    while ((text = br.readline()) != null) {
        String parts[] = text.split(",");
        Treatmeant treament = new Treatment(parts[0],
            parts[1],
            parts[2],
            parts[3]);
        treatments.add(treament);
    }
} // Handle exception as required...

我将其包装到某些实用程序类的readTreatments方法中,以使其更易于使用...

I'd wrap this into a readTreatments method in some utility class to make it easier to use...

在这附近,我会考虑使用独立的数据库甚至XML文档,但这只是我一个.

Around about here, I'd be considering using a stand alone database or even an XML document, but that's just me.

一旦有了这个,就可以设计一个TableModel来支持它...

Once you have this, you can design a TableModel to support it...

public class TreatmentTableModel extends AbstractTableModel {

    protected static final String[] COUMN_NAMES = {
        "Treatment-Number", 
        "Treatment-Name",
        "Doctor-in-charge",
        "Room-No",
    };
    protected static final Class[] COLUMN_CLASSES = new Class[]{
        Integer.class, 
        String.class,
        Doctor.class,
        Integer.class,
    };
    private List<Treatment> treatments;

    public TreatmentTableModel() {
        this.treatments = new ArrayList<>();
    }

    public TreatmentTableModel(List<Treatment> treatments) {
        this.treatments = new ArrayList<>(treatments);
    }

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

    @Override
    public int getColumnCount() {
        return 4;
    }

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

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

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Treatment treatment = treatments.get(rowIndex);
        Object value = null;
        switch (columnIndex) {
            case 0:
                value = treatment.getNumber();
                break;
            case 1:
                value = treatment.getName();
                break;
            case 2:
                value = treatment.getDoctor();
                break;
            case 3:
                value = treatment.getRoomNumber();
                break;
        }
        return value;
    }

}

然后,您只需将其应用于所需的JTable ...

Then you simply apply it to what ever JTable you need...

private JTable treatments;

//...
treatments = new JTable(new TreatmentTableModel());
add(new JScrollPane(treatments));

然后,我们需要加载TreatmentList并将其应用于表...

Then, we you need to, you would load the List of Treatments and apply it to the table...

File file = new File("...");
treatments.setModel(new TreatmentTableModel(TreatmentUtilities.readTreatments(file)));

这篇关于如何从java MVC中的arraylist .TXT文件显示JTable中的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆