• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to do an image gallery

 
Jose Lince
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I'm new here and I expect you can solve my doubts .

I'm trying to do an image gallery and I'm adding labels dinamically to a JPanel when an user select an image from JFileChooser.

My problem is that I don't know how to add actions dinamically to labels because I would like, for example, print a message when an user clicks in the labels.

Thanks all.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you select an image using the file chooser you must have code something like:



So all you need to add is something like:

 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The information you have provided in insufficient, there are number of ways to do this, I am mentioning the smallest possibility according to my knowledge.

OK. First you need to have few labels, for an example 5 labels ( Creating much labels as you need is also possible through for loop). Then, when the user selects images, you have to add those into an"ImageIcon". Image Icon declaration is like below

Icon icon = new ImageIcon("image path");

then you have to set these into the labels, as below


label1 = new JLabel(icon);





Anyway, you can you can't prompt the JFileChooser to add images in each and every label because that could be really annoying for the user, so, when the user selects multiple images, you can insert them into an array (String array, then you can place array values as the image path for the ImageIcon).



To the rest of the problem, which is about printing messages etc, just either implement MouseLister interface or MouseMotionListener interface or else create a sub class or something extending MouseAdapter or MouseMotionAdapter classes as you can see in the post of the other member.
 
Jose Lince
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob & Yohan, you two have given me good ideas.

If anyone have more advices for me, i'll receive them gratefully.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!