Adding ArrayList of ImageIcons to JLabels
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Adding ArrayList of ImageIcons to JLabels

Hybrid View

  1. #1
    Join Date
    Mar 2012
    Posts
    2

    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.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Mar 2012
    Posts
    2

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center