File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help adding JButton to a JPanel from another method!

 
Brendon Randall
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic