0

I'm trying to add multiple images to JLabel. The problem is that it is impossible to add more than one image, and I don't know how many images I will need in advance. How can I overcome the problem? mabye not JLabel..

Thanks

codeCompiler77
  • 508
  • 7
  • 22

1 Answers1

1

1) Show us what you've done so far :)

2) Short answer is that you can't. JLabels and JButtons can only have one image associated. So you can either have multiple dynamically created JLabels or JPanels.

3) I don't understand why you want the image in a JLabel.

SOLUTION

Create a JPanel called container and give it a grid layout. Dynamically create more JPanels (pnl1, pnl2, etc) and add them to container. Each image should be added to pnl1, pnl2, etc.

Create a class image panel that extends jpanel as your container for each image.

If you don't know during compile time, I assume you know during runtime? Which means you just keep creating new panels until you run out of images.

In your Image Panel class you'll also want to assign an id for each image panel in the event that you want to be able to utilize or change the content/image later. So you'll find panels by id and not by variable name because those will all be dynamic and you won't have them.

Community
  • 1
  • 1
codeCompiler77
  • 508
  • 7
  • 22
  • It doesn't have to be Jlabel, if you have a better idea. And how can I add dynamically Jlabels? Thank you very much – Meir Julian Mar 09 '16 at 19:31