Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dynamically adding a JPanel in JFrame - not reflecting

 
Arvind Sampath
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

We are developing a Standalone Java application using Swing. I am facing problems while trying to dynamically add a JPanel in a JFrame.

The JFrame gets loaded successfully. When the action event for the button occurs, I am trying to add a JPanel to this frame at runtime. But the Panel elements are not getting reflected in the screen. This is the code that is present in actionPerformed()



I tried calling repaint() on getContentPane() and on the frame object as well.

Any suggestions/pointers are greatly appreciated.

Cheers,
Arvind
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try revalidate instead.
 
Arvind Sampath
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried calling revalidate() on the JPanel object which was added to the JFrame. Still didnt work.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about the frame itself?
 
Arvind Sampath
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is no revalidate() method in JFrame as it is not in the Component Hierarchy
 
Brian Cole
Author
Ranch Hand
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arvind Sampath:
I tried calling revalidate() on the JPanel object which was added to the JFrame. Still didnt work.


You would want to call revalidate() on the container to which something was added, not on the thing that was added itself. In this case that would be the frame's content pane.
 
Arvind Sampath
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian,

As far as I can see, there is no revalidate() method on the container.

I tried calling validate() on the JFrame. It didnt work in the NetBeans created code. But it did work, when I coded the UI myself using GridBagLayout. I found that when I used NetBeans for UI design, by default, it is addding the components to a Layout and then adding the layout to the frame. Now, when I try to add components dynamically to the Frame instead of the layout, the changes were not reflecting
 
Brian Cole
Author
Ranch Hand
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arvind Sampath:
As far as I can see, there is no revalidate() method on the container.


There is, but you have to cast it:

getContentPane().add(new HomePanel());
((JComponent)getContentPane()).revalidate();

That should work unless you have set the content pane to be a non-Swing container (unlikely).
[ October 31, 2007: Message edited by: Brian Cole ]
 
ivin jacob
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also facing the same problem .

I tried the above said technique , still its not working .

Its something related to the automatically generated code by netbeans , since i am able to view the new panel , in a seperate project where in which i did not use the layout the netbens created . instead i used flow layout
 
aml lopez
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must set the layout for the panel in netbeans. For example, gridlayout or something.

Good luck
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has been my experience that revalidate() needs to be followed up by repaint()
 
aml lopez
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need whatsoever to call revalidate or repaint. As long as you have defined a suitable layout for your panel, a simple call to add will do the job.

Regards.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the original stated problem
"I am trying to add a JPanel to this frame at runtime."

if you wouldn't mind posting a small sample program to prove your statement
"There is no need whatsoever to call revalidate or repaint."

because I have 'always' needed to revalidate/repaint when adding to a visible container
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aml, we really appreciate your input. However, did you really think it was necessary to reply to a thread that was over one year old?
 
aml lopez
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example:

VisiblePanel.add(new Jbutton("test"));

This works for me in Netbeans if VisiblePanel has a layout manager set. Otherwise it doesn't. And as I said there is no need to call revalidate or repaint.

As for why I answer to a question which is one year old. I stumbled across this post through Google, when I was looking for a solution to the same problem that the asker was having. When I found the solution by myself I thought somebody may benefit it from it in the future, if they are searching through google for a similar problem. I actually just registered on this site to be able to post the solution in case someone may need it in the future.

Got it?

Regards.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aml lopez wrote:Example:

VisiblePanel.add(new Jbutton("test"));

This works for me in Netbeans if VisiblePanel has a layout manager set. Otherwise it doesn't. And as I said there is no need to call revalidate or repaint.

I think that your solution has little to do with adding things at runtime as was specified in the original post. My guess (and guess is all I can do since you didn't post a program but rather a snippet) is that your button was added before the GUI was packed and displayed which is quite a different issue.

On a side note, I highly recommend that you not use NetBeans to generate your Swing code until you've learned how to do it by hand. Going through the Sun Swing tutorials and learning to code Swing will save you worlds of trouble later when you want to tweak your GUI into doing things that it didn't do before. I think that NetBeans code-generation is fine for quick and dirty apps, but using it too much actually stunts your Swing education.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Got it?

the only thing I 'got' is that your claims are absolute rubbish.
and that is being nice (javaranch policy), you shouldn't go around stating falsehoods.

as you won't post a working example of your claim, I'll do it for mine.
run the program, click the button on the left (anything added?), then click the button on the right.


 
aml lopez
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you have your code. The asker was asking about NetBeans and why it doesn't work in NetBeans. The reason is he hadn't set up a layout manager for the panel. In netbeans the default layout manager is free and not flow. If you set the layout to flow the dynamic components will add perfectly without any need to call revalidate or repaint. However, if you don't set the layout manager in NetBeans the dynamic components won't show even if you call repaint or revalidate.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, but you might as well try running this example in NetBeans (if you even know how to use it) and you'll see that it works perfectly without calling revalidate or repaint.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx , so please do run this code, and save us all some time.



 
aml lopez
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete stein wrote:
aml lopez wrote:Example:

VisiblePanel.add(new Jbutton("test"));

This works for me in Netbeans if VisiblePanel has a layout manager set. Otherwise it doesn't. And as I said there is no need to call revalidate or repaint.

I think that your solution has little to do with adding things at runtime as was specified in the original post. My guess (and guess is all I can do since you didn't post a program but rather a snippet) is that your button was added before the GUI was packed and displayed which is quite a different issue.

On a side note, I highly recommend that you not use NetBeans to generate your Swing code until you've learned how to do it by hand. Going through the Sun Swing tutorials and learning to code Swing will save you worlds of trouble later when you want to tweak your GUI into doing things that it didn't do before. I think that NetBeans code-generation is fine for quick and dirty apps, but using it too much actually stunts your Swing education.


I am sorry but you are right. After checking my code (the one I posted) I think that actually the button is added before the panel is packed.

Therefore, I apologise.

One thing is true if you don't set the layout manager in netbeans, revalidate doesn't work and the new button won't show. So what the asker should do is set the layout manager to flow or whatever he needs and then call revalidate.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aml Lopez,
As much as we like your contributions, I would like to remind you of the number one rule here at the ranch, "Be Nice"
There have been instances in the past, where different people have various approaches regarding solution for a specific problem. However, the "ranch" way to do it is a friendly discussion.
The way you have put forth your opinions might be taken in an offensive way.
Please remember, whatever we write here, is taken at word value, as we are on the web and do not know each other.
I think you did not intend to be intentionally offensive, but please take care in the future.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> One thing is true if you don't set the layout manager in netbeans, revalidate doesn't work and the new button won't show.

once again, that is just plain wrong.

revalidate will work - if the button doesn't show it is because it has no bounds in a ("if you don't set the layout manager") null layout,
and null layouts require the bounds to be set or the default is accepted (0,0,0,0)

disclaimer: revalidate, on its own, sometimes doesn't work, but in conjunction with repaint, always works
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic