This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Help adding JButton to a JPanel from another method!
Brendon Randall
Greenhorn
Joined: Mar 13, 2009
Posts: 10
posted
0
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
posted
0
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
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Help adding JButton to a JPanel from another method!