私はテーブルのタイプを実装しています。これを行うために、私は を使用しました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
。