我正在实现一个 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
类中,我定义了要在 CreaOrario
类中使用的方法 getLesson()
。
但是,如果在MyTableModel
中我编写table.getLesson()
,则会显示一条消息错误,指出该方法 getLesson() 对于该类型未定义JTable
。
为什么? table
实际上是一个 TabellaOrario
对象,并以 MyTableModel
作为模型。 getLesson()
是为 MyTableModel
定义的,因此我希望 table.getLesson()
可以工作。
最佳答案
这是因为 table
被声明为 JTable
并且 getTable()
返回一个 JTable
。您只需要进行类型转换,因为 JTable
中不存在 getLesson()
。
您需要像这样调用 getLesson():
String[] lessons = ((TabellaOrario.MyTableModel)table.getModel()).getLesson();
关于java - 无法在同一类的对象上使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18812032/