Reputation: 637
I'm Implementing a type of table. To do this, I have used an AbstractTableModel
, but I don't know what my code is doing (that's why I get a snippet that did his work, but obviously I want to understand). You can see the code
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
}
}
}
Well, what does it mean table = new JTable(new MyTableModel());
?
I thought that table
would be a MyTableModel
instance, but that is not true. In fact, if I pass table to another external class, this class won't see the method myMethod
defined in the body of MyTableModel
.
Upvotes: 1
Views: 97
Reputation: 205805
A MyTableModel
is a TableModel
, but a TableModel
is not necessarily a MyTableModel
.
As you know your table's model is an instance of MyTableModel
, which has the desired myMethod()
, you can use a cast:
JTable table = new JTable(new MyTableModel());
MyTableModel model = (MyTableModel) table.getModel();
It may be easier to create and use a reference to your model:
MyTableModel model = new MyTableModel();
JTable table = new JTable(model);
Don't neglect to include the implementation of setValueAt()
, shown here.
Upvotes: 1
Reputation: 827
its expected behaviour that other classes cant see the function myMethod because they "think" that your MyTableModel is just a AbstractTableModel (which in fact it isnt) The code and everything else you put into MyTableModel is callen correctly but you just cant see the function myMethod Thats because youre using a OOP language
So no youre wrong. Youre creating a instance of MyTableModel so everything you do inside will be executed if you call
you should read this here which will explain more about the OOP part: http://www.dummies.com/how-to/content/understanding-javas-objectoriented-programming-oop.html
just to say it quick
If you need to access your myMethod function just cast it like so: // Create the MyTableInstance (this is possible because AbstractTableModel is the parent and MyTableModel is the child) AbstractTableModel atmTestVariable = new MyTableModel();
// Now to be able to call this:
// atmTestVariable.myMethod();
// we need to do this:
if(atmTestVariable isntanceof MyTableModel) //make sure that atmTestVariable is REALY a MyTableModel
{
MyTableModel mtmTemp = (MyTableModel)atmTestVariable; //cast to MyTableModel
mtmTemp.myMethod(); //Call the function
// and now the short variant:
((MyTableModel)atmTestVariable).myMethod(); //cast to MyTableModel AND call the function
}
Hopefully i was able to help
Ohhh and please move the define of your MyTabelModel class into a seperate file : /
Upvotes: 0
Reputation: 4671
a JTable constructor accepts any Object of a class that implements the TableModel
interface
this will help you learn more.
Upvotes: 0