Solved

adding jbutton to instance of a class that extends JPanel

Posted on 2010-11-11
16
937 Views
Last Modified: 2012-05-10
Can not I add jbutton to instance of a class that extends JPanel  ??

0
Comment
Question by:hipal
  • 5
  • 4
  • 4
  • +1
16 Comments
 
LVL 92

Expert Comment

by:objects
ID: 34118025
yes you can do that

panel.add(button);
0
 

Author Comment

by:hipal
ID: 34118032
It is not working  ..
0
 
LVL 92

Expert Comment

by:objects
ID: 34118064
need more information :)
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 16

Accepted Solution

by:
Valeri earned 167 total points
ID: 34118409
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
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 166 total points
ID: 34118436
by default panels will have FlowLayout, post your code! may be you are not adding the panel to a frame/dialog/window
0
 

Author Comment

by:hipal
ID: 34118475
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 34118489
what have you overridden in JPanel( paint/paintComponent...? )
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 34118491
>>I am setting the frame layout to be null

using layouts are the recommended approach!
0
 
LVL 92

Expert Comment

by:objects
ID: 34118526
you need to revalidate() the panel
and nake sure you are overriding paintComponent() and *not* the paint() method
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 34118625
>>you need to revalidate() the panel

unless you add the component at runtime, you don't have to call this!
0
 
LVL 16

Expert Comment

by:Valeri
ID: 34119041
>> do I need to something with panel layout too ?
yes, you have to do this with the panel too
0
 

Author Comment

by:hipal
ID: 34120096
@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
 
LVL 16

Expert Comment

by:Valeri
ID: 34120421
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
 
LVL 92

Expert Comment

by:objects
ID: 34125311
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
 

Author Comment

by:hipal
ID: 34127634
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
 
LVL 92

Assisted Solution

by:objects
objects earned 167 total points
ID: 34128918
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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question