Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

unable to see dynamically added component in swing  RSS feed

 
rajiv modh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I have created a java swing application using netbeans 6.5
I have added almost all elements using drag n drop method.
Now I want to add one component dynamically in the panel.
But i can not see that component.
I have tried debugging the ode and it traverse through the component but it's not displaying on the screen.

Code snippet I tried is as below.
JLabel jLabel=new JLabel();
jLabel.setText("labelllllll");
jLabel.setVisible(true);
pnlImage.add(jLabel);
pnlImage.validate();
pnlImage.repaint();

Can anyone please suggect wht's wrong or wht I am missing here?

Thanks in advance.
 
Marky Vasconcellos
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think iy should work:



Remember always post code in the tags [ code ] and [/ code ]
 
rajiv modh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marky,
I tried that, It didn't work out.

I am extending Jframe.
I have added Jpanel to the frame using drag n drop.

Now I want to add Jlabel to the panel dynamically.
 
Marky Vasconcellos
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try revalidate the frame.. i think it should call all revalidate methods in the Components of this frame.
 
Bartek Myszkowski
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rajiv - Marky is right, revalidate works
check this example which I've written in 5 min. - it dynamically adds JLabels to JPanel after pressing JButton and I've written it just like Marky said -> revalidate:If it doesn't work for You, maybe there's a problem with layout.. what layout do You use? Paste more code (whole class) or isolate the problem in simple example...
 
rajiv modh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks but you created frame and panel by code.
I am using netbeans drag n drop option to create my frame and panel.

But now I want to add jlabel by coding.
Is it possible that way?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dump and DnD kind of IDEs like Netbeans and start hand coding.
Best way to learn. Also you never face these kind of problems if you hand code.
 
Bartek Myszkowski
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, and what about layout You use? for ex. if You set layout to null You have to set boundes of components.. if You use GroupLayout You have to add components in a special way.. so which one do You use? read here about layouts: http://java.sun.com/docs/books/tutorial/uiswing/layout/group.html

It would be great if could give whole class sourcecode (or whole project or classes with isolated problem).
 
rajiv modh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for suggestion but I would appreciate more if someone can show some solution.
Project is about to finish and now can't go back and create all again.
 
Bartek Myszkowski
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With code You've provided, Marky, in second post, have shown You a solution (which I've checked and it works).

If You don't post more code it's impossible to give You solution... but it's only my opinion.
 
Marky Vasconcellos
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i do not have so many time last days..
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Thanks for suggestion but I would appreciate more if someone can show some solution.

the solutions already provided do work.

it's now up to you to post the code proving they don't work,
otherwise you're just wasting everyone's time.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:
it's now up to you to post the code proving they don't work,
otherwise you're just wasting everyone's time.

I'm surprised he hasn't come up with a solution what with his cross-posting this same question on so many fora.
 
rajiv modh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

thnaks all
I figured out the middle way.
though it's not optimum solution but it worked for me anyway.


I used scrollpane as container to add my label and image.
Due to some reasons it's not adding any java component to my panel.
Now it's with gud GUI.

Thanks everyone.
 
Marky Vasconcellos
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyway.. i know i need to post more code.. but i just changed the example he gives.. and i normally post in that way in other forums.. sorry if the people here want more.. I'll back and be more efficient when i have time.

cya
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!