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
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
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.