• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't see an inner class' method from another method

 
Denise Advincula
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

This is a simple JTable creation using a TableModel. I made an inner class, MyTableModel which extends the AbstractTableModel. I have properly implemented all other required methods. And then I added an additional method called "fireChanges()".

However, when this fireChanges() was invoked from another method, the compiler couldn't see it.

Any ideas why? I'm wondering if this code is correct in the first place:
JTable table = this.makeTable();
TableModel model = table.getModel();


[ August 11, 2008: Message edited by: Denise Saulon ]
 
Steve Ng
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm not sure how your program works. But in order to access a method of an non-static inner class. You would need a instance of the outer class(enclosing class) and an instance of the inner class.

In this case, the myMethod would look similar to:

Inner innerInstance = new MainClass().new InnerClass();
innerInstance.myInnerMethod();

or

Outer outerInstance = new MainClass();
Inner innerInstance = outerInstance.new InnerClass();
innerInstance.myInnerMethod();
 
Denise Advincula
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I managed to find my way.. This is why:

TableModel model = table.getModel();

The TableModel reference has no fireChanges() method. I changed the line to
MyTableModel model = (MyTableModel) table.getModel();

and now it can see the method.

Thank you for taking the time to help me out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic