0

I've created a JFrame window with a JComboBox. I can select things but they don't do anything. I thought that event was a String, but it isn't. What is the solution for this?

import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Gui extends JFrame {
    private JComboBox box;
    private JLabel picture;

    private static String[] filename = {"", "b.png", "x.png"};

    public Gui() {
        super("the title");
        setLayout(new FlowLayout());
        box = new JComboBox(filename);
        box.addItemListener(
                new ItemListener() {
                    public void itemStateChanged(ItemEvent event) {
                        if(event.getStateChange()==ItemEvent.SELECTED){
                            System.out.println("test");
                            if(event=="b.png") {
                                System.out.println("test2");
                            }
                        }
                    }
                });
        add(box);
    }
}
user1803551
  • 12,965
  • 5
  • 47
  • 74
  • Compare `String`s with `equals`, not with `==`. – user1803551 Jan 09 '16 at 18:42
  • You mean: if(event.equals("b.png")) { –  Jan 09 '16 at 18:44
  • Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Jan 09 '16 at 18:48
  • I'm really getting depressive that everyone is always downvoting me. I almost don't dare to ask a question anymore. –  Jan 09 '16 at 18:52
  • 1
    @HovercraftFullOfEels I'm not sure if this is a complete duplicate because the OP is missing a Swing-related step of `event.getItem()`. After all, they are stating they have a problem with `event` not being a `String`. – user1803551 Jan 09 '16 at 18:55
  • 1
    Wout, I know, I'm pretty experienced with this site network and I am also afraid when asking questions because of odd downvotes. I suggest you ignore the score and focus on solving your problem. This site is far from perfect on its Q&A side. – user1803551 Jan 09 '16 at 18:57
  • Thanks, that really made me feel better. :) –  Jan 09 '16 at 19:02
  • 1
    @WoutHuygens, don't take downvotes personally. Nobody here knows you personally. The votes apply to questions and answers only. By familiarizing yourself with the question guidelines, you will minimize the downvotes (but of course, some people are just rude and unfair, so don't let that depress you. Just a part of life). Here are some links that may help you: [**asking questions 1**](http://stackoverflow.com/help/how-to-ask) and [**asking questions 2**](http://stackoverflow.com/help/mcve). Good luck! – Michael Benjamin Jan 09 '16 at 19:54

1 Answers1

2

You need to get the selected item (which is a String in your case) and compare it to your string with equals:

if ("b.png".equals(event.getItem()))

Note that this is preferable to

event.getItem().equals("b.png")

since this can theoretically throw a NPE.

Also, use a generic type: JComboBox<String>, not the raw type.

user1803551
  • 12,965
  • 5
  • 47
  • 74