-1

我有一个 GUI,其中有一个名为“listTable”的 JTable 容器。我想使用 AbstractTableModel 使用存储在链接列表中的数据填充 JTable。

基本上,我有一个 LinkedList 存储在 Processing.java 中。它包含从文本文件中检索到的所有数据。我创建了一个 AbstractTableModel 来告诉 JTable 如何填充。我现在要做的是用“链接列表的内容”填充 GUI_g 中的 JTable。

谁能告诉我该怎么做?

这是代码:

GUI_g:

import java.awt.*; 
import javax.swing.*;
import javax.swing.JTable;


public class GUI_g extends JFrame {

    public void buildGui() {

        JFrame frame = new JFrame("Hotel TV Scheduler");

                JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout(0,0));

                JPanel chPanel = new JPanel();
                chPanel.setLayout(new GridLayout(3,1));

                JPanel listPanel = new JPanel();
                listPanel.setLayout(new GridLayout(3,2,1,0));

                JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new GridLayout(0,3, 1, 0));


                JPanel addPanel = new JPanel();
        addPanel.setLayout(new GridLayout(0,3));

                JPanel chlblPanel = new JPanel();
                chlblPanel.setLayout(new GridLayout(1,2));

                JPanel tablePanel = new JPanel();
                tablePanel.setLayout(new GridLayout(1,2));

                JPanel rmvbtnPanel = new JPanel();
                rmvbtnPanel.setLayout(new GridLayout(1,2));

                JPanel centrePanel = new JPanel();
                centrePanel.setLayout(new GridLayout(0,3));


             //   mainPanel.add(chPanel, BorderLayout.WEST);
            //    mainPanel.add(listPanel, BorderLayout.EAST);
                mainPanel.add(centrePanel, BorderLayout.CENTER);


                JTable chOneTable = new JTable();
                JTable chTwoTable = new JTable();

                JTable listTable = new JTable(new ProgramTableMode());

                JLabel ch1Label = new JLabel("Channel 1");
                JLabel ch2Label = new JLabel("Channel 2");
                JLabel listLabel = new JLabel("List");

                JButton rmvChOneButton = new JButton("Remove Channel");
                JButton rmvChTwoButton = new JButton("Remove Channel");

                chlblPanel.add(ch1Label);
                chlblPanel.add(ch2Label);

                tablePanel.add(chOneTable);
                tablePanel.add(chTwoTable);

                rmvbtnPanel.add(rmvChOneButton);
                rmvbtnPanel.add(rmvChTwoButton);            

                chPanel.add(chlblPanel);
                chPanel.add(tablePanel);
                chPanel.add(rmvbtnPanel);

                listPanel.add(listLabel);
                listPanel.add(listTable);                

                JLabel titleLabel = new JLabel("Title");
                JLabel genreLabel = new JLabel("Genre");
                JLabel durationLabel = new JLabel("Duration");
                JLabel actorLabel = new JLabel("Actor");
                JLabel directorLabel = new JLabel("Director");
                JLabel rentableLabel = new JLabel("Rentable");
                JLabel synLabel = new JLabel("Synopsis");

                JTextField txtTitle = new JTextField();          
                JTextField txtGenre = new JTextField();
                JTextField txtDuration = new JTextField();
                JTextField txtActor = new JTextField();
                JTextField txtDirector = new JTextField();
                JTextField txtSynopsis = new JTextField();

                JCheckBox rentCB = new JCheckBox();

                JButton btnAddProg = new JButton("Add Program");

                JList channelList = new JList();
                JList timeList = new JList();

                infoPanel.add(titleLabel);
                infoPanel.add(txtTitle);
                infoPanel.add(new JLabel(" "));
                infoPanel.add(genreLabel);
                infoPanel.add(txtGenre);
                infoPanel.add(new JLabel(" "));
                infoPanel.add(durationLabel);
                infoPanel.add(txtDuration);
                infoPanel.add(new JLabel(" "));
                infoPanel.add(actorLabel);
                infoPanel.add(txtActor);
                infoPanel.add(new JLabel(" "));
                infoPanel.add(directorLabel);
                infoPanel.add(txtDirector);
                infoPanel.add(new JLabel(" "));
                infoPanel.add(rentableLabel);
                infoPanel.add(rentCB);
                infoPanel.add(new JLabel(" "));
                infoPanel.add(synLabel);
                infoPanel.add(txtSynopsis);
                infoPanel.add(new JLabel(" "));
                infoPanel.add(btnAddProg);
                infoPanel.add(channelList);
                infoPanel.add(timeList);

                centrePanel.add(chPanel);
                centrePanel.add(infoPanel);
                centrePanel.add(listPanel);

                frame.add(mainPanel);
                frame.setVisible(true);


    }


}

程序表模型:

import java.util.List;

import javax.swing.table.AbstractTableModel;

public class ProgramTableModel extends AbstractTableModel{

    protected String[] columnNames = {"Type","Title","Length"};

    private List<Program> data;


    public ProgramTableModel (List<Program> data){
        this.data = data;
    }

    public int getColumnCount() {
        return 3;
    }

    public int getRowCount() {
        return data.size();
    }

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

    public Object getValueAt(int row, int column) {

            switch (column){
            case 0: return data.get(row).getCategory();
            case 1: return data.get(row).getTitle();
            case 2: return data.get(row).getDuration();
            default: return "N/A";
            }

    }
}

加工:

import java.io.*;
import java.util.*;


public class Processing
{
    List<Program> schedule = new LinkedList<Program>();

    public void readAllData() {

        try{
            FileInputStream fstream = new FileInputStream("file.txt");

            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String tempString = null;





            while ((tempString = br.readLine()) != null)   {


                String[] row = tempString.split("-");

                if(row[0].compareTo("COMEDY") == 0) {

                    Comedy comedy = new Comedy(row[0], row[1], Integer.parseInt(row[2]), row[3], row[4], Boolean.parseBoolean(row[5]));
                    schedule.add(comedy);

                } 

                else if(row[0].compareTo("DRAMA") == 0) {

                    Drama drama = new Drama(row[0], row[1], Integer.parseInt(row[2]), row[3], row[4], Boolean.parseBoolean(row[5]));
                    schedule.add(drama);

                }

                else if(row[0].compareTo("MUSIC") == 0) {

                    MusicVideo music = new MusicVideo(row[0], row[1], Integer.parseInt(row[2]));
                    schedule.add(music);

                }

                else if(row[0].compareTo("HOTEL") == 0) {

                    HotelInfo hotel = new HotelInfo(row[0], row[1], Integer.parseInt(row[2]));
                    schedule.add(hotel);

                }

            }
            in.close();

        }

        catch (Exception e){
            System.err.println("Error: " + e.getMessage());
        }
    }

    public void getAll()
    {
        for (Program p: schedule)
        {
            System.out.println(p.getTitle() + p.getDuration() + p.getCategory() + p);
        }
    }

}
4

1 回答 1

0

您可以添加一个重置方法来ProgramTableModel重置内部列表:

public reset (List<Program> data){ 
    this.data = data; 
} 

然后只需调用reset()与关联的模型对象listTable。这意味着,您需要从 GUI 存储和公开该对象

注意:您可以按照自己喜欢的方式操作模型列表:添加、删除、替换元素,只需添加能够实现所需行为的适当方法

于 2012-04-12T12:00:05.590 回答