Solved

adding jbutton to instance of a class that extends JPanel

Posted on 2010-11-11
16
924 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now