Link to home
Start Free TrialLog in
Avatar of hipal
hipalFlag for United States of America

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  ??

Avatar of Mick Barry
Mick Barry
Flag of Australia image

yes you can do that

panel.add(button);
Avatar of hipal

ASKER

It is not working  ..
need more information :)
ASKER CERTIFIED SOLUTION
Avatar of Valeri
Valeri
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hipal

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 .
what have you overridden in JPanel( paint/paintComponent...? )
>>I am setting the frame layout to be null

using layouts are the recommended approach!
you need to revalidate() the panel
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!
>> do I need to something with panel layout too ?
yes, you have to do this with the panel too
Avatar of hipal

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
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?
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()
Avatar of hipal

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()" ??
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial