Results 1 to 3 of 3
  1. #1
    Cymro is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default ArrayList problem with images

    I have this code:
    Java Code:
        public static ArrayList<ImageIcon> imRay = new ArrayList<ImageIcon>();
        ImageIcon one = new ImageIcon("0.png");
        ImageIcon two = new ImageIcon("1.png");
    
        public Test() {
            initComponents();
            this.setLocationRelativeTo(null);
            imRay.add(one);
            imRay.add(two);
        }
    Then, later on, under a button:

    Java Code:
    int i = 0;
    i = jComboBox1.getSelectedIndex();
    jLabel3.setIcon(imRay.get(i));
    However, whilst it doesn't throw an error (unless I select a value in the jComboBox that is higher than currently allowed), it doesn't change the icon in jlabel3.

    Where am I going wrong?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    This works okay.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.util.*;
    import java.util.List;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class ImageSwap implements ActionListener {
        JLabel label;
        public List<ImageIcon> imRay = new ArrayList<ImageIcon>();
    
        public void actionPerformed(ActionEvent e) {
            int index = ((JComboBox)e.getSource()).getSelectedIndex();
            label.setIcon(imRay.get(index));
        }
    
        private JLabel getLabel(BufferedImage[] images) {
            for(int j = 0; j < images.length; j++)
                imRay.add(new ImageIcon(images[j]));
            label = new JLabel(imRay.get(0));
            label.setHorizontalAlignment(JLabel.CENTER);
            return label;
        }
    
        private JPanel getComboPanel() {
            JComboBox combo = new JComboBox();
            for(int j = 0; j < imRay.size(); j++)
                combo.addItem(Integer.valueOf(j));
            combo.addActionListener(this);
            JPanel panel = new JPanel();
            panel.add(combo);
            return panel;
        }
    
        public static void main(String[] args) throws IOException {
            String[] ids = { "--g--", "---h-" };
            BufferedImage[] images = new BufferedImage[ids.length];
            for(int j = 0; j < images.length; j++) {
                String path = "images/geek/geek" + ids[j] + ".gif";
                images[j] = ImageIO.read(new File(path));
            }
            ImageSwap test = new ImageSwap();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getLabel(images));
            f.add(test.getComboPanel(), "North");
            f.setSize(400,400);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }

  3. #3
    Cymro is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default

    Thanks, but I don't know which parts I would need for my already existing class. Is there any chance that you could add some comments to the vital parts, so I can have some idea of what to do with it?

    EDIT: I have found a rather unelegant possible solution which involves invisible labels containing the images necessary, then, instead of loading the images on command, it simply gets the label to use the other label's icon. It's quite slow, but I'll have to try and incorporate some elements of your code into it to speed it up a bit.
    Last edited by Cymro; 02-05-2008 at 08:45 PM.

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  2. ArrayList problem
    By khamuruddeen in forum New To Java
    Replies: 7
    Last Post: 12-22-2007, 05:46 AM
  3. ArrayList problem (finding largest no)
    By bugger in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 12:47 PM
  4. Help with images...
    By toby in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 05:25 AM
  5. Images in JSP
    By Daniel in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-05-2007, 06:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •