• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a method from other JPanel  RSS feed

 
Mikasa Jaeger
Greenhorn
Posts: 24
Chrome Google App Engine Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a method in a JPanel called JPanelTicket and I want to call this method from another JPanel called JCatalog.

Can anyone say me how to do this?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

If the method is static you can simply invoke it by JPanelTicket#method()
If it is not, you will need to reference of the panel to invoke it. jPanelTicketInstance#method()

This is more of a beginner question and not a specific Swing problem. I will move this topic over to a more appropriate forum
 
Mikasa Jaeger
Greenhorn
Posts: 24
Chrome Google App Engine Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maneesh,

Thanks for that quickly answer!
But, I'm Doing what you say and still not work.

The method is not static, so I create a instance of JPanelTicket.
Well the problem is:

When i do:



It shows me a NullPointerException

And if I do:



It does nothing

So you know why it's happening this?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't just create another instance of JPanelTicket and call it as it is an entirely different object to the JPanelTicket object you are displaying on the screen. You have to pass the reference of the JPanelTicket object that you are displaying to the JCatalog object that needs to make the call. The JCatalog object then keeps a copy of the reference which it can use to call the JPanelTicket.

Another approach is to have the JCatalog object fire an event to notify any listeners of a change of state etc and have the JPanelTicket object listen for particular events that are of interest to it.
 
Mikasa Jaeger
Greenhorn
Posts: 24
Chrome Google App Engine Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks tony,

I know I'm newbie, but, how can I pass the reference of the JPanelTicket that is displaying?
I do not usually ask in forums unless I've tried everything I know and searched for other solutions.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are the two panels related? If yes, you can try something like

Then inside your typeB you can call typeA#whateverMethod();

I personally prefer exposing a static getInstance() method. That way I do not need to worry about relationship.
 
Mikasa Jaeger
Greenhorn
Posts: 24
Chrome Google App Engine Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ill try getInstance() method!

Thanks!
 
Mikasa Jaeger
Greenhorn
Posts: 24
Chrome Google App Engine Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just realized JPanelTicket is an abstract class.

public abstract class JPanelTicket extends JPanel implements JPanelView

Now I am even more confused. I can't instantiate JPanelTicket.
Still didn't know what to do..
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ouyeahh Manjon wrote:



If JPanelTicket is an abstract class, How did you create an Instance of JPanelTicket like that..???
 
Mikasa Jaeger
Greenhorn
Posts: 24
Chrome Google App Engine Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for help!
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
crossposted
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
Mikasa Jaeger
Greenhorn
Posts: 24
Chrome Google App Engine Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I posted on Stack Overflow only because the question I ask here is too bad.
Just my fault, not that you have answered wrong or do not know the answer. I had changed my question, so, I was almost ashamed to ask again.
How can I ask: " Well, now my question is other because I realize that JPanelTicket was abstract "?

Well, I can resolve mi problem thanks to codeRanck and Stack, so only I can say thanks to both. And sorry for cross-posting
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ouyeahh Manjon wrote:Hi,

I posted on Stack Overflow only because the question I ask here is too bad.
Just my fault, not that you have answered wrong or do not know the answer. I had changed my question, so, I was almost ashamed to ask again.
How can I ask: " Well, now my question is other because I realize that JPanelTicket was abstract "?

Well, I can resolve mi problem thanks to codeRanck and Stack, so only I can say thanks to both. And sorry for cross-posting

Some sites do not allow cross-posting but here at JavaRanch it is allowed as long as you make people aware so they don't waste their time answering a question that has already been answered elsewhere.

But please don't ever be ashamed to ask a question, people here won't get angry at you. We all volunteer our time here because we like to share our knowledge with others.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!