'listTable'というJTableのコンテナを持つGUIがあります。AbstractTableModelを使用して、リンクリストに格納されているデータをJTableに入力したいと思います。
基本的に、Processing.javaにLinkedListが格納されています。テキストファイルから取得したすべてのデータが含まれています。JTableにデータを入力する方法を指示するAbstractTableModelを作成しました。ここで実行したいのは、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);
}
}
ProgramTableModel:
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);
}
}
}