• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 978
  • Last Modified:

adding jbutton to instance of a class that extends JPanel

Can not I add jbutton to instance of a class that extends JPanel  ??

0
hipal
Asked:
hipal
  • 5
  • 4
  • 4
  • +1
3 Solutions
 
objectsCommented:
yes you can do that

panel.add(button);
0
 
hipalAuthor Commented:
It is not working  ..
0
 
objectsCommented:
need more information :)
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ValeriCommented:
probbaly this is because you are not using layout manager. after adding the buton you have to possition it by setBounds :
yourPanel.add(button);
button.setBounds(left, top, width, height)
0
 
ksivananthCommented:
by default panels will have FlowLayout, post your code! may be you are not adding the panel to a frame/dialog/window
0
 
hipalAuthor Commented:
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 .
0
 
ksivananthCommented:
what have you overridden in JPanel( paint/paintComponent...? )
0
 
ksivananthCommented:
>>I am setting the frame layout to be null

using layouts are the recommended approach!
0
 
objectsCommented:
you need to revalidate() the panel
and nake sure you are overriding paintComponent() and *not* the paint() method
0
 
ksivananthCommented:
>>you need to revalidate() the panel

unless you add the component at runtime, you don't have to call this!
0
 
ValeriCommented:
>> do I need to something with panel layout too ?
yes, you have to do this with the panel too
0
 
hipalAuthor Commented:
@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
0
 
ValeriCommented:
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?
0
 
objectsCommented:
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()
0
 
hipalAuthor Commented:
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()" ??
0
 
objectsCommented:
because paint() is responsible for all aspects of the painting, not just what you need to override when subclassing. When subclassing you only typically need to override paintComponent(). Overriding paint() can break some of the standard painting behaviour

http://java.sun.com/products/jfc/tsc/articles/painting/
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now