-
March 1st, 2012, 08:55 PM
#1
Adding ArrayList of ImageIcons to JLabels
I have an ArrayList of 52 card ImageIcons. I then use Collections.shuffle to randomize the cards. I want to put this ArrayList into individual JLabels to display in the JPanel.
I cant seem to figure out how to put the ArrayList into JLabels. Is this possible or is there a better method for GUI instead of JFrame like paint() method?
I will post the snippet of code below.
Code:
ImageIcon[] cards = new ImageIcon[53];
ArrayList<ImageIcon> cardArray = new ArrayList<ImageIcon>();
for(int b=0; b<cards.length; b++) {
cards[b]=new ImageIcon("C:/NetBeansProjects/BiddingTool/src/biddingtool/cards/"+b+".gif");
cardArray.add(cards[b]);
}
Collections.shuffle(cardArray);
List northlist = cardArray.subList(0,13);
frame2.add(northcards, BorderLayout.NORTH);
for (int i=0; i<north.length; i++) {
northcards.add(new JLabel());
}
I have tried putting cardArray in the JLabel but that doesnt work. I have scoured this site and others trying to find any answer but to no avail. Any help or ideas would be appreciated.
-
March 2nd, 2012, 06:24 AM
#2
Re: Adding ArrayList of ImageIcons to JLabels
Is this possible or is there a better method for GUI instead of JFrame like paint() method?
Why are you using the paint() method?
JLabels support objects that implement Icon which ImageIcon's do so you can create a new JLabel object for each ImageIcon and add the JLabel to the GUI.
You need to decide how you want the images laid out and choose an appropriate layout manager for the JPanel that will display all the images. Create a JPanel with that layout manager and add the JPanel to the JFrame. Then iterate over the arraylist creating an new JLabel for each ImageIcon and adding each JLabel object to the panel.
-
March 4th, 2012, 07:30 PM
#3
Re: Adding ArrayList of ImageIcons to JLabels
Thanks for the quick reply. I wasnt using paint() method I just threw that out there because most of the examples I found used it.
I figured out I needed to be using cardArray.get[i] inside the loop. Your response got me on the right track of thinking about the problem in a different way.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|