Solved

adding jbutton to instance of a class that extends JPanel

Posted on 2010-11-11
16
939 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

821 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