我正在实现一种表。为此,我使用了AbstractTableModel
,但我不知道我的代码在做什么(这就是为什么我得到一个完成他的工作的片段,但显然我想理解)。你可以看到代码
public class TablePanel extends JPanel
{
private JTable table;
public TablePanel()
{
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
table = new JTable(new MyTableModel());
table.setFillsViewportHeight(true);
table.setPreferredScrollableViewportSize(new Dimension(500, 100));
JScrollPane jps = new JScrollPane(table);
add(jps);
add(new JScrollPane(table));
table.setCellSelectionEnabled(true);
}
private class MyTableModel extends AbstractTableModel {
private String[] columns = {"","Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
private String[][] data = {{"8:30 - 9:30","","","","","",""},
{"9:30 - 10:30","","","","","",""},
{"10:30 - 11:30","","","","","",""},
{"11:30 - 12:30","","","","","",""},
{"12:30 - 13:30","","","","","",""},
{"13:30 - 14:30","","","","","",""},
{"14:30 - 15:30","","","","","",""},
{"15:30 - 16:30","","","","","",""},
{"16:30 - 17:30","","","","","",""}};
public int getColumnCount() {
return columns.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columns[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Object myMethod (){
//do something
}
}
}
嗯,这是什么意思table = new JTable(new MyTableModel());
?
我认为这table
将是一个MyTableModel
例子,但事实并非如此。事实上,如果我将 table 传递给另一个外部类,这个类将看myMethod
不到MyTableModel
.