hipal
asked on
adding jbutton to instance of a class that extends JPanel
Can not I add jbutton to instance of a class that extends JPanel ??
ASKER
It is not working ..
need more information :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am setting the frame layout to be null
do I need to something with panel layout too ? I just setting its size and position .. I think that should do .. as I am doing likewise in other programs so far.. and it works .
do I need to something with panel layout too ? I just setting its size and position .. I think that should do .. as I am doing likewise in other programs so far.. and it works .
what have you overridden in JPanel( paint/paintComponent...? )
>>I am setting the frame layout to be null
using layouts are the recommended approach!
using layouts are the recommended approach!
you need to revalidate() the panel
and nake sure you are overriding paintComponent() and *not* the paint() method
and nake sure you are overriding paintComponent() and *not* the paint() method
>>you need to revalidate() the panel
unless you add the component at runtime, you don't have to call this!
unless you add the component at runtime, you don't have to call this!
>> do I need to something with panel layout too ?
yes, you have to do this with the panel too
yes, you have to do this with the panel too
ASKER
@objects .. sorry but I did not get you
@KSiv thanks I did not knew that
@Valeri can you plz plz give example what do you mean by that
@KSiv thanks I did not knew that
@Valeri can you plz plz give example what do you mean by that
1. have you added the panel to your jframe?
2. are you sure that button is created? I mean is it not null?
3. could you please print getBounds() of the button and to see where it's placed?
2. are you sure that button is created? I mean is it not null?
3. could you please print getBounds() of the button and to see where it's placed?
whenever you change the layout of a visible container you need to let the container know that it needs to redo its layout
eg.
panel.add(button);
panel.revalidate();
panel.repaint();
And if panel is a subclass then you need to make sure you have not overridden paint()
eg.
panel.add(button);
panel.revalidate();
panel.repaint();
And if panel is a subclass then you need to make sure you have not overridden paint()
ASKER
Hi everyone .. thanks for such good responses
@Valeri .. yes I will try your advice now.
@Objects .. can you please elaborate on why " if panel is a subclass then you need to make sure you have not overridden paint()" ??
@Valeri .. yes I will try your advice now.
@Objects .. can you please elaborate on why " if panel is a subclass then you need to make sure you have not overridden paint()" ??
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
panel.add(button);