aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Cannot add an image to JPanel. Nothing works:( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Cannot add an image to JPanel. Nothing works:(" Watch "Cannot add an image to JPanel. Nothing works:(" New topic
Author

Cannot add an image to JPanel. Nothing works:(

Liana Norpunova
Greenhorn

Joined: Aug 24, 2010
Posts: 23
Hi!

I would like to add an Image to JPanel after the Mouse Click. I've read dozens of information about this topic, however have a problem...I've tried three different variants of code to add an image, however each time got just a blank screen - no picture. Pliz help.



I've also tried the following code:



...and this one:


NOTHING WORKS

P.S.: I'm using NetBeans IDE 6.9.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

What does icon.getImageWidth() print? If that's -1 then the image path isn't correct.

Also:
1) don't add the panel to the content pane again. It's already there (well, I assume).
2) call revalidate() on the JPanel after you add a control to it while it's already visible.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Liana Norpunova
Greenhorn

Joined: Aug 24, 2010
Posts: 23
Thanks! Everything is ok now This is the workable code:


Maybe someone could also advise me how to move the created JLabel: label with the mouse on the screen? I'd like to allow the user to position the images on the screen. I've used label.setLocation(MouseInfo.getPointerInfo().getLocation().x-jPanel1.getX(),MouseInfo.getPointerInfo().getLocation().y-jPanel1.getY()); inside the private void jLabel1MousePressed(java.awt.event.MouseEvent evt). However, it doesn't work.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

To position components manually you must set its layout manager to null. However, this also means that you must set the preferred size manually. After that you can use setLocation or setBounds to place the component. Note that without a layout manager the added component will initially have a size of 0x0. By calling you can give it its desired size, then use setLocation to move it around.

If you want to drag your label around you should try searching first. There are quite some examples for that.
Liana Norpunova
Greenhorn

Joined: Aug 24, 2010
Posts: 23
Thanks a lot! I've updated the code by setting null layout manager and positioning JLabel: label by hand. Everything works correctly!

However, if I want JLabel: label to respond on my mouse click and get a position of the mouse cursor, then it doesn't work. What makes me confused is that MouseEvent is set to JLabel: jLabel1 and not to JLabel: label...Could someone tell me how to set the event handling to JLabel: label? Maybe some web links...? Now I have the following code (it doesn't work in the way I want):

Liana Norpunova
Greenhorn

Joined: Aug 24, 2010
Posts: 23
This is how I try to solve my problem (still doesn't work correctly):



In addition, I've created the Java class:



Any ideas how to make it workable (drag and drop behavior of JLabel: label)?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot add an image to JPanel. Nothing works:(