File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Help adding JButton to a JPanel from another method! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help adding JButton to a JPanel from another method!" Watch "Help adding JButton to a JPanel from another method!" New topic
Author

Help adding JButton to a JPanel from another method!

Brendon Randall
Greenhorn

Joined: Mar 13, 2009
Posts: 10
Hi all,

I have a bit of a problem, I am trying to add some JButtons into a JPanel from a method call that is triggered by a buttons actionlistener, the problem i have got is when it comes to adding the button it does not show on the JPanel but if i get the component count of the JPanel it shows it as being there. I am using Netbeans 6.5 and using a FlowLayout for my JPanel, this is what i have

in my first JPanel that i have i add buttons to it when the JPanel is created and this works fine



As you can see i created my own actionlistener for these buttons which i attach to the buttons and this is what it looks like



In my actionlistener i then call a few methods that are in the PosTeller class as this is where my JPanel sits, here are the methods



The problem is when it gets to the addToCatPane() and i create the buttons to add to the productsPane absolutly nothing happens, nothing gets displayed!! but the component count shows that there are components there, i have called rePaint() reValidate() inValidate() etc etc and nothing seems to make them display....

Please can someone help me
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
normally when adding or removing components from a visible swing container you need both
container.revalidate();
container.repaint();

not always, but most times, both are required
Brendon Randall
Greenhorn

Joined: Mar 13, 2009
Posts: 10
Thanks for the reply, i have just tried that, this is what i did



But still no luck the buttons do not show
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you would need to post a compilable program, otherwise it's just a guessing game.

if productsPane's layoutmanger is null, your problem is there
if cat.size() < 1 your problem is there
etc

in posting a compilable program, get rid of the unnecessary stuff.
you're trying to get a JButton to appear, so the button's font, actionCommand,
actionListener etc are not part of the problem

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help adding JButton to a JPanel from another method!
 
Similar Threads
TicTacToe! Stop user from changing input
help help
font size and style
How do make the applet taller then it is wide?
Query on ProgressBar(Help plz)