This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes adding controls to jpanel at runtime problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "adding controls to jpanel at runtime problem" Watch "adding controls to jpanel at runtime problem" New topic
Author

adding controls to jpanel at runtime problem

nesma abd el mawla
Greenhorn

Joined: Nov 29, 2012
Posts: 4
I am trying to add jlabel to a jpanel at runtime and Here's My code but it doesn't work :



Thanks in advance
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
are you using netbeans deault GroupLayout
as the layout manager?
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

Hi nesma,
Welcome to the Ranch!

You need to give us more information about the problem. What is the LayoutManager you are using for the panel?
If you haven't already, I would suggest reading Using Layout Managers


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
Why are you trying to add a label when the button is pushed? That would create a very peculiar appearance. If the button is pushed many times, you would create many labels.
nesma abd el mawla
Greenhorn

Joined: Nov 29, 2012
Posts: 4
Michael Dunn , Ranganathan Kaliyur Mannar :
First thanks for replys then,
currently I don't use a layout manager but I tried to use grouplayout or borderlayout as :

jPanel1.add(b,BorderLayout.PAGE_START);

and it fails too !

Campbell Ritchie :
the aim of application is simple ladder diagram tracer ,, when i press a button a jlabel (with set icon property using .png images) will appear and draw a switch for example.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

Before testing this on the button click, I would suggest adding this directly before you call setVisible(true) and see if it works. That should give you a clue.
nesma abd el mawla
Greenhorn

Joined: Nov 29, 2012
Posts: 4
Ranganathan Kaliyur Mannar wrote:Before testing this on the button click, I would suggest adding this directly before you call setVisible(true) and see if it works. That should give you a clue.


Thanks for reply Ranganathan ,
I tried setting visible property to true but it doesn't solve anything ,,
the problem was Creating controls and adding it on netbeans main panel at run time needs Only "Grid or Flow Layout managers" other layout managers don't work properly and I was using the default group layout.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

no no, what I was trying to say was:
normally, you will build all the controls and then call setSize() or pack() on the frame and then call setVisible(true) on the frame. So, whatever code you have in the ActionListener, just call it before you show the frame. This would help you to actually find out if that code works.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: adding controls to jpanel at runtime problem
 
Similar Threads
JLabel , image
Swing application using netBeans IDE
Problems with ImageIcon when focus is lost
JFrame
problem with actionevent