I use NetBeans IDE to create an application using java. I want to include a JButton
over a JLabel
with image. Actually when I add JButton
over the JLabel
it would inserted but it will transparent and the name and the button didn't display. It seems the button to be added under the JLabel
. How can I overcome this problem?
Asked
Active
Viewed 1,057 times
0

ROMANIA_engineer
- 54,432
- 29
- 203
- 199

user3029009
- 1
- 1
-
1So, what are you really trying to accomplish here? Layering components isn't that easy in Swing. For more information, see [How to Use Layered Panes](http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html). – mre Nov 24 '13 at 19:21
-
2**first**, show us what you have tried, **second** Make an image depicting your requirement, upload it to some image hosting site and link it in your question. – Sage Nov 24 '13 at 19:36
-
Im using JLayeredPane if you wanna keep it simple :) – Tomas Bisciak Nov 24 '13 at 20:09
-
You can create a JButton with an image, so what is the JLabel doing for you? – Gilbert Le Blanc Nov 25 '13 at 19:51
-
An example is shown **[here](http://stackoverflow.com/questions/19198298/show-button-on-jlabel-in-swing/19198871#19198871)**. You should choose a proper layout manager based on your needs. – dic19 Nov 25 '13 at 22:27
2 Answers
4
there are two ways
put image to
JPanel
by overridepaintComponent
(standard way)JLabel
haven't anyLayoutManager
in API, you have to setLayoutManager
, thenJLabel
will be container

mKorbel
- 109,525
- 20
- 134
- 319
-
+1 for suggestions. I think the problem OP is facing is about missing `JLabel`'s layout manager. – dic19 Nov 25 '13 at 22:25
0
ImageIcon img = null;
class Panel extends JPanel{
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img == null){
img = new ImageIcon("d:\\picture.jpg");
g.drawImage(img.getImage(), 0, 0, null);
}
}
}
//in the constructor
JButton button = new JButton( "OK");
Panel panel = new Panel();
panel.add(button);
add(panel);

FTibor
- 1
- 1