import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.TableModel;
public class TimeTable extends JFrame implements TableModelListener {
public TimeTable(){
super("TimeTable");
Object[][] data = {
{"Monday", "",
"", "TCP/IP LECTURE", "TCP/IP LECTURE","TCP/IP Tutorial","Break","OOP","OOP","OOP"},//1
{"Tuesday", "","", "Network Management Lecture","Network Management Lecture","Break","OOP Lecture","OOP Lecture","WNet Lecture","WNet Lecture"},//2
{"Wednesday", "TCP/IP Practical",
"TCP/IP Practical", "Wnet Tutorial", "","","","","",""},//3
{"Thursday", "",
"", "Network Management Prac", "Network Management Prac","Break","WNet Prac","WNet Prac","Network Management Tutorial",""},//4
{"Friday", "FYP",
"FYP", "FYP", "FYP","","","","",""},//5
{"Saturday", "Wake Up",
"Prepare Bag and Things for outing", "", "Meet at Jurong East","","","","",""},//6
{"Sunday", "Lih",
"Teaching high school", "", "","","","","",""},//7
};
String[] columnNames = {"Day",
"8am-9am",
"9am-10am",
"10am-11am",
"11am-12pm",
"12pm-1pm",
"1pm-2pm",
"2pm-3pm",
"3pm-4pm",
"4pm-5pm",
};
final JTable table = new JTable(data, columnNames);
table.getModel().addTableModelListener(this);
table.setPreferredScrollableViewportSize(new Dimension(900, 500));
//Set Cell Heigh and Width//
table.setRowHeight(0,90);
table.getColumnModel().getColumn(0).setWidth(90); //1
table.setRowHeight(1,90);
table.getColumnModel().getColumn(1).setWidth(90); //2
table.setRowHeight(2,90);
table.getColumnModel().getColumn(2).setWidth(90); //3
table.setRowHeight(3,90);
table.getColumnModel().getColumn(3).setWidth(90); //4
table.setRowHeight(4,90);
table.getColumnModel().getColumn(4).setWidth(90); //5
table.setRowHeight(5,90);
table.getColumnModel().getColumn(5).setWidth(90); //6
table.setRowHeight(6,90);
table.getColumnModel().getColumn(6).setWidth(90); //7
table.setRowHeight(7,90);
table.getColumnModel().getColumn(7).setWidth(90); //8
table.setRowHeight(8,90);
table.getColumnModel().getColumn(8).setWidth(90); //9
table.setRowHeight(10,90);
table.getColumnModel().getColumn(9).setWidth(90); //10
/////////////////////////////////////
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this window.
getContentPane().add(scrollPane, BorderLayout.CENTER);
}
public static void main(String[] args) {
TimeTable frame = new TimeTable();
frame.pack();
frame.setVisible(true);
}
@Override
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel)e.getSource();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column);
}
}