我正在实现一个JFrame
必须查看表格并对其进行处理的类。该表有一个AbstractTableModel
,我已经定义了一个我想在JFrame
类中使用的方法。但我无法做到这一点。这里的代码:
必须查看 JTable 的 JFrame 类
public class CreaOrario extends JFrame {
public CreaOrario(int num) {
initialize(num);
}
/**
* Initialize the contents of the frame.
*/
private void initialize(final int semestre) {
this.setSize(1150,650);
this.setTitle("Creazione dell'Orario");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TabellaOrario orarioEditabile = new TabellaOrario(semestre);
this.getContentPane().add(orarioEditabile, BorderLayout.CENTER);
final JTable table = orarioEditabile.getTable();
//now I'd like to do: table.getLesson;
}
}
必须在 JFrame 中添加并包含表的 JPanel 类:
public class TabellaOrario extends JPanel
{
private JTable table;
public TabellaOrario (int semestre)
{
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
table = new JTable(new MyTableModel());
table.setFillsViewportHeight(true);
table.setPreferredScrollableViewportSize(new Dimension(510, 350));
JScrollPane jps = new JScrollPane(table);
add(jps);
add(new JScrollPane(table));
table.setCellSelectionEnabled(true);
table.setRowHeight(40);
}
public JTable getTable() {
return table;
}
public class MyTableModel extends AbstractTableModel
{
private String[] columns = {"","Lunedì","Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato"};
private Object [][] 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","","","","","",""}};
@Override
public int getColumnCount() {
return columns.length;
}
@Override
public int getRowCount() {
return data.length;
}
@Override
public String getColumnName(int col) {
return columns[col];
}
@Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
@Override
public void setValueAt(Object aValue, int row, int col) {
data[row][col] = aValue;
fireTableCellUpdated(row, col);
}
}
public String[] getLesson (int row, int column)
{
String value = (String) table.getValueAt(row, column);
int start = value.indexOf("<b>");
int end = value.indexOf("</b>");
String insegnamento = value.substring(start + "<b>".length(), end);
start = value.lastIndexOf("<b>");
end = value.lastIndexOf("</b>");
String aula = value.substring(start + "<b>".length(), end);
return new String[] {insegnamento,aula};
}
}
}
在MyTableModel
课堂上,我已经定义了getLesson()
我想在CreaOrario
课堂上使用的方法。
但是如果在MyTableModel
我写table.getLesson()
的时候,它会显示一个消息错误,上面写着The method getLesson() is undefined for the type JTable
。为什么?table
事实上是一个TabellaOrario
对象,并MyTableModel
作为模型。getLesson()
被定义为MyTableModel
,所以我希望它table.getLesson()
有效。