I have a GUI in which has a container for a JTable called 'listTable'. I want to populate the JTable with data stored in a linked list, using the AbstractTableModel.
Basically, I have a LinkedList stored in Processing.java. It contains all the data retrieved from a text file. I created an AbstractTableModel to tell the JTable how to be populated. What I want to do now is to populate the JTable in GUI_g with "the contents of the linkedlist".
Anyone can tell me how to do this?
Here is the code:
GUI_g:
import java.awt.*;
import javax.swing.*;
import javax.swing.JTable;
import java.util.List.*;
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();
// ProgramTableModel model = new ProgramTableModel(List<Program>);
JTable listTable = new JTable();
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
{
private List<Program> schedule;
public ProgramTableModel(List<Program> result)
{
this.schedule= result;
}
public int getColumnCount()
{
return 2;
}
public int getRowCount()
{
return schedule.size();
}
public Object getValueAt(int rowIndex, int columnIndex)
{
Program sData = schedule.get(rowIndex);
switch (columnIndex)
{
case 0: return sData.getTitle();
case 1: return sData.getDuration();
case 2: return sData.getCategory();
default: return "Not Available.";
}
}
}
Processing:
import java.io.*;
import java.util.*;
public class Processing
{
public static List<Program> readAllData() {
List<Program> schedule = new LinkedList<Program>();
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());
}
return schedule;
}
}
Program:
public abstract class Program {
String Title;
int Duration;
String Category;
public Program(String title, int duration, String category)
{
this.Title = title;
this.Duration = duration;
this.Category = category;
}
public String getTitle() {
return Title;
}
public int getDuration() {
return Duration;
}
public String getCategory() {
return Category;
}
}
Basically what I want is to put the contents of List<Program> into a JTable in the GUI.
Can anyone help me ?