• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Cannot add an image to JPanel. Nothing works:(

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic