これは私のコントローラーです (モデルからデータを読み取り、これらのデータを JTable オブジェクトに割り当て、クラス ビューで MedalTableModel オブジェクトを割り当てます)。
public class TableController {
private TableModel table_model;
private TableView table_view;
public TableController (TableModel table_model, TableView table_view) {
this.table_model = table_model;
this.table_view = table_view;
}
public void getMedalData () {
try {
table_model.getCsvData();
} catch (Exception e) {
e.printStackTrace();
}
}
public void setFilePath (String file_path) {
table_model.setFilePath(file_path);
}
/*
public void addLineData (ArrayList<String> data_list) {
Object[] line_data = new Object[data_list.size()];
for (int index = 0; index < data_list.size(); index ++) {
line_data[index] = data_list.get(index);
}
table_view.getTableModel().addRow(line_data);
}
*/
public void addTableData (String file_path) {
Object[] col_names = {"Country name", "Official medal ranking",
"GDP rank", "Pop rank", "Team size"};
setFilePath(file_path);
getMedalData();
int row_size = table_model.getRowSize();
int col_size = table_model.getColumnSize();
Object[][] table_data = new Object[row_size][col_size];
ArrayList<String> data_list = new ArrayList<String>();
for (int row_index = 0; row_index < row_size; row_index ++) {
data_list = table_model.getLineData(row_index);
for (int col_index = 0; col_index < col_size; col_index ++) {
table_data[row_index][col_index] = data_list.get(col_index);
}
}
//table_view.setTableData(table_data);
JTable temp_table = new JTable (table_data, col_names);
table_view.setTable(temp_table);
MedalTableModel temp_model = new MedalTableModel(table_data, col_names);
table_view.setTableModel(temp_model);
}
}
これが私の見解です:
public class TableView extends JFrame{
//private DefaultTableModel def_model;
private MedalTableModel mtable_model;
private JTable medal_table;
private JButton close_button;
private TableRowSorter sorter;
private Object[][] table_data;
/*
public DefaultTableModel getTableModel () {
return def_model;
}
// maybe do not need this method
public void setTableModel (DefaultTableModel table_model) {
this.def_model = table_model;
}
*/
public TableView(String frame_title) {
super(frame_title);
medal_table = new JTable();
JScrollPane scroll = new JScrollPane(medal_table);
JPanel button_panel = new JPanel();
close_button = new JButton("Close");
button_panel.add(close_button);
getContentPane().add(scroll, BorderLayout.CENTER);
getContentPane().add(button_panel, BorderLayout.SOUTH);
pack();
}
public void setTableData (Object[][] table_data) {
this.table_data = table_data;
}
public Object[][] getTabledata () {
return table_data;
}
public MedalTableModel getTableModel () {
return mtable_model;
}
// maybe do not need this method
public void setTableModel (MedalTableModel table_model) {
this.mtable_model = table_model;
}
public void setTable (JTable table) {
this.medal_table = table;
}
public void setTableModel () {
//add data here
medal_table.setModel(mtable_model);
}
// set sort feature should be added after setModel
public void setSortMtd () {
//sorter = new TableRowSorter<DefaultTableModel>(def_model);
medal_table.setAutoCreateRowSorter(false);
TableRowSorter trs = new TableRowSorter(mtable_model);
class IntComparator implements Comparator {
public int compare(Object o1, Object o2) {
Integer int1 = (Integer)o1;
Integer int2 = (Integer)o2;
return int1.compareTo(int2);
}
public boolean equals(Object o2) {
return this.equals(o2);
}
}
trs.setComparator(1, new IntComparator());
trs.setComparator(2, new IntComparator());
medal_table.setRowSorter(trs);
medal_table.setModel(mtable_model);
}
}
これが私の MedalTableModel extends DefaultTableModel; です。
public class MedalTableModel extends DefaultTableModel {
public MedalTableModel (Object[][] table_data, Object[] col_names) {
super(table_data, col_names);
}
public MedalTableModel() {
// TODO Auto-generated constructor stub
}
Class[] types = { String.class, Integer.class, Integer.class,
Integer.class, Integer.class };
//String[] ColumnName = {"Country name", "Official medal ranking",
// "GDP rank", "Pop rank", "Team size"};
@Override
public Class getColumnClass (int columnIndex) {
return this.types[columnIndex];
}
/*
@Override
public String getColumnName(int index) {
return ColumnName[index];
}
*/
/*
@Override
public Class getColumnClass(int col) {
//zero column accepts only String values
if (col > 0) {
return Integer.class;
}
else {
return String.class;
}
}
*/
@Override
public boolean isCellEditable(int row, int col) {
return false;
}
}
そして、これが主な方法です(別のファイルにあります):(初期モデル、コントローラー、およびビュー)
public static void main( String[] args ) {
SwingUtilities.invokeLater(new Runnable() {
public void run () {
String file_path = "London2012OlympicAlternativeMedalRankingsALL.csv";
TableModel table_model = new TableModel(file_path);
TableView table_view = new TableView("Medal Table");
TableController table_contrl = new TableController(table_model,
table_view);
table_contrl.addTableData(file_path);
table_view.setSortMtd();
table_view.setVisible(true);
table_view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
//System.out.println( "Hello World!" );
}
問題は、このプログラムを実行しようとすると、JFrame に何も表示されないことです。私のコードの問題は何ですか? どうもありがとう。