Solved

adding jbutton to instance of a class that extends JPanel

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

0
Comment
Question by:hipal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

717 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