0

I'm relatively new eclipse, and java programing in general. I am having a problem getting my images to work as they should. when I run my program it is supposed to display a drop down menu that has an image. If you want to know more about the goal click the youtube link, for the video tutorial im following(episode.68, 69).

When i run my program instead of dis playing the images it just displays the name of it, this happen with other programs i've written. if you need any more info on the code just ask. Images are just no being displayed, so please help!!!

My code:

import java.util.Scanner;

import javax.swing.JFrame;

public class Initial {
    public static void main(String[] args) {

        GUI go = new GUI();
        go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        go.setSize(300,200);
        go.setVisible(true);

    }   
}



   import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUI extends JFrame{

    private JComboBox box;
    private JLabel picture;

    private static String[] filename = {"test1.png", "test2.png"};
    private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0]))};

    public GUI() {
        super("The title");
        setLayout(new FlowLayout());

        JComboBox<String> box = new JComboBox<>(filename);

        box.addItemListener(
                new ItemListener() {
                    public void itemStateChanged(ItemEvent event) {
                        if(event.getStateChange()==ItemEvent.SELECTED)
                            picture.setIcon(pics[box.getSelectedIndex()]);
                    }
                }
        );

        add(box);
        picture=new JLabel(pics[0]);
    }
}

error when i click on one of the images in the drop down box:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
    at GUI$1.itemStateChanged(GUI.java:23)
    at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1223)
    at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1280)
    at javax.swing.JComboBox.contentsChanged(JComboBox.java:1330)
    at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118)
    at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:93)
    at javax.swing.JComboBox.setSelectedItem(JComboBox.java:576)
    at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:622)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:861)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at com.apple.laf.AquaComboBoxPopup$1.processMouseEvent(AquaComboBoxPopup.java:157)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
nkr
  • 3,026
  • 7
  • 31
  • 39
codey_boi
  • 1
  • 1
  • first of all, there is no link to click. secondly, this is not even closely eclipse related. at last: java.lang.ArrayIndexOutOfBoundsException: 1 is very, very clear. if you do not know how to solve this: please, uninstall the IDE, stop working on GUI's or other frameworks and please, learn the basics first – Stultuske Dec 21 '17 at 14:51
  • You only have one value in your `pics` array and you are trying to access the second value (index 1). – greg-449 Dec 21 '17 at 14:53
  • 1
    *uninstall the IDE* <-- probably a tad unnecessary. – achAmháin Dec 21 '17 at 14:53
  • `JComboBox box = new JComboBox<>(filename);` you're creating the comboBox by passing a String array as argument, that's why you're getting Strings. – Touniouk Dec 21 '17 at 14:57
  • You need to pass the image array, not the String array. If you want more information you can look at the top answer here: https://stackoverflow.com/questions/3958647/displaying-images-in-jcombobox it's pretty much what you need. – Touniouk Dec 21 '17 at 15:00
  • @Touniouk thanks, do you know an option that will pass an array edit: never mind – codey_boi Dec 21 '17 at 15:02
  • Look I’m getting started on java and I told you I don’t have much experience, but instead of telling me to give up I will find out the answer regardless of people like you. So instead of coming here and saying that it’s such an easy problem to fix, YOU help ME fix it. I may be a bit far ahead but I’ve been on it for 2 months and I feel like I’ve worked my way up to this point. It is eclipse related because it’s not accepting my image somewhere. There’s no fatal error in the code just the images are not being displayed. And copy and paste the link. @Stultuske – codey_boi Dec 21 '17 at 15:07
  • @greg-449 I think it would be more accurate to have this question marked as duplicate of the one I linked. As the question wasn't about the arrayOutOfBoundException. – Touniouk Dec 21 '17 at 15:09
  • @codey_boi I'm not telling you to give up. I'm telling you that if you build a building, it's better to start with foundations and walls, not just build a roof on tiny sticks that can't hold the weight. if you don't know/understand the basics, you'll be able to copy paste code and get it to work, but as soon as something has to be changed, you'll be in trouble. – Stultuske Dec 21 '17 at 16:51
  • @Stultuske yea I get where your coming from and I was able to resolve the issue, I used image icon instead – codey_boi Dec 22 '17 at 02:02
  • how is that able to solve an ArrayIndexOutOfBoundsException ? – Stultuske Dec 22 '17 at 11:40

0 Answers0