Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot add an image to JPanel. Nothing works:(

 
Liana Norpunova
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Liana Norpunova
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic