File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes placing image on jpanel and then add other components Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "placing image on jpanel and then add other components " Watch "placing image on jpanel and then add other components " New topic

placing image on jpanel and then add other components

Kay Tracid
Ranch Hand

Joined: Mar 06, 2002
Posts: 148
I have an image in a JPanel where I call the paint methode to draw the image.I want to add some buttons on top or under the image(-panel). But all I tried looks terible:
1.Add a button to the same panel: nothing happens (because i overwrite "paint")
2.I add the imagePanel and a buttonPanel to a third panel, but the size of the imagePanel is wrong. It is to small, I dont see the whole picture.
Please help, its urgent...
Thanks a lot!
Here my imagePanel:
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Kay,
First, since you are using Swing you should not be overriding paint instead you should be overriding paintComponent. See the quote from Sun below.

Note: Don't override or invoke the method that calls the paintXxx methods: the paint method. Although overriding paint was legitimate in pre-Swing components, it's generally not a good thing to do in components that descend from JComponent. Unless you're careful, overriding paint would likely confuse the painting system, which relies on the JComponent implementation of the paint method for correct painting, performance enhancements, and features such as double buffering.

Second, you will need to call super.paintComponent in order for things to be drawn correctly (i.e., other components appearing above your picture).
I agree. Here's the link:
subject: placing image on jpanel and then add other components
Similar Threads
problem relating to jpanel background image
ImagePanel with components
Images sometimes don't display
How to add image inside jPanel ??