Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

adding a panel to another when i click on a boutton from another class

 
jam dah
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I want to be able to add a panel to another when i click on a button from a ToolBarPanel.
look at what i did:
ToolBarPane : the class where i click on button and so i send the event

ContainerPane: the class where i want to add a panel and where i implemented the interface MouseListener

but nothing happen.
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I'm sure in Swing it is possible to update a panel from another one.

In your code what's a OverPanel? I see you created a ContainerPane on line 33 in ToolBarPane but didn't use it any where. Are any of the methods in ContainerPane overrides of the OverPanel?

You should provide a SSCCE
 
jam dah
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello k.Tsang,
thanks for your reply.
Me too i am sure that in swing it is possible to update a panel frrom other one. I did it before, but i wrote all the code in one class.
I mean handling event in the same class: For example

Now, i want to handle my events from another class.
OvalPane, it is a class which exends JPanel and implements Border interface : it renders a panel with arc corner.
ContainerPane extends OvalPane.
ToolBarPane it is my toolBar: i didn't use JToolBar class but i tried to build it with a panel in order to customise my toolbar.
So from the ToolBarPane i click on a button(openItem) and i want to update the switchPane in the ContainerPane. That's what i want.
Sorry for my poor English.
Thanks
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you have done it before (all in the same class), now the difference is separate classes.

The most straightforward approach is to pass an instance of the JPanel to the action class (class that inplements ActionListener). Then you can use this reference to repaint or update when necessary.

Another approach is make a static method to retrieve the JPanel in your main JFrame class. Then in your action class, call the static method to get JPanel ref and update accordingly.

Hope you understand the logic.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!