Adding Control to JFrame

Hi,

I'd want to add a second button to my JFrame when the first button is pressed,

so in the eventhander of button 1 I put:

JButton btn = new JButton("button2");

this.getContentPane().add(btn);

where this is a class extending javax.swing.JFrame

nothing happens when i press the first button... I'm puzzled, this should be easy...

[374 byte] By [CodingFisha] at [2007-11-27 3:50:39]
«« DRM?
»» New Guy
# 1
I find it easier to first declare and add the 2 buttons and put button2 visible(false) and when button 1 is pushed button2 visible(true)Satanduvel
Satanduvela at 2007-7-12 8:54:35 > top of Java-index,Desktop,Core GUI APIs...
# 2
You usually need to make the JFrame repaint itself in order for new elements to become visible. Usually a pack() or invalidate() does the trick. Packing may resize the JFrame, however... Greets, Mike
MikePa at 2007-7-12 8:54:35 > top of Java-index,Desktop,Core GUI APIs...
# 3
I've tried adding pack(), validate(), invalidate() but the button still doesn't show :s
CodingFisha at 2007-7-12 8:54:35 > top of Java-index,Desktop,Core GUI APIs...
# 4
i noticed that it works if I choose a border, or gridlayout.. etc but the problem is that I need my buttons to be positioned absolutely :s
CodingFisha at 2007-7-12 8:54:35 > top of Java-index,Desktop,Core GUI APIs...
# 5

> I've tried adding pack(), validate(), invalidate() but the button still doesn't show :s

Then you are still doing something wrong.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 8:54:35 > top of Java-index,Desktop,Core GUI APIs...