Rameez Mahmood
Rameez Mahmood

Reputation: 3

Trying to change image when JButton pressed

I am trying to change the Image on the panel when the any of the JButtons are pressed. I have set up an array of images and need it to change to the next image in the array once it has been pressed. Here is my code:

public class SimpleGui implements ActionListener {
    JButton button = new JButton("Very Happy");
    JButton buttonTwo = new JButton("Happy");
    JButton buttonThree = new JButton("Neutral");
    JButton buttonFour = new JButton("Sad");
    JButton buttonFive = new JButton("Very Sad");
    static int[] ButtonArray = new int[5];
    private static String[] imageList = { "res/snow.jpg", "res/test-gm.jpg" };

    public int i = 0;

    public static void main(String[] args) throws FileNotFoundException {

        SimpleGui gui = new SimpleGui();
        gui.go();

        File file = new File("out.txt");
        FileOutputStream fos = new FileOutputStream(file);
        PrintStream ps = new PrintStream(fos);
        System.setOut(ps);

        ButtonArray[0] = 0;
        ButtonArray[1] = 0;
        ButtonArray[2] = 0;
        ButtonArray[3] = 0;
        ButtonArray[4] = 0;

    }

    public void go() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setBackground(Color.darkGray);

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        button.addActionListener(this);
        buttonTwo.addActionListener(this);
        buttonThree.addActionListener(this);
        buttonFour.addActionListener(this);
        buttonFive.addActionListener(this);
        panel.add(button);
        panel.add(buttonTwo);
        panel.add(buttonThree);
        panel.add(buttonFour);
        panel.add(buttonFive);

        frame.getContentPane().add(BorderLayout.EAST, panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(650, 600);
        frame.setVisible(true);

        ImageIcon image = new ImageIcon(imageList[i]);
        ImageIcon image1 = new ImageIcon(imageList[i + 1]);
        JLabel label = new JLabel("", image, JLabel.CENTER);
        JPanel panel2 = new JPanel(new BorderLayout());
        panel2.add(label, BorderLayout.CENTER);
        panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
        frame.add(panel2, BorderLayout.CENTER);

        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {

        if (event.getSource() == button) {
            ButtonArray[0] = 1;
            ButtonArray[1] = 0;
            ButtonArray[2] = 0;
            ButtonArray[3] = 0;
            ButtonArray[4] = 0;
            System.out.println("Very Happy");

        }
        // buttonTwo = (JButton) event.getSource();
        if (event.getSource() == buttonTwo) {
            ButtonArray[0] = 0;
            ButtonArray[1] = 1;
            ButtonArray[2] = 0;
            ButtonArray[3] = 0;
            ButtonArray[4] = 0;
            System.out.println("Happy");

        }
        // buttonThree = (JButton) event.getSource();
        if (event.getSource() == buttonThree) {
            ButtonArray[0] = 0;
            ButtonArray[1] = 0;
            ButtonArray[2] = 1;
            ButtonArray[3] = 0;
            ButtonArray[4] = 0;
            System.out.println("Neutral");

        }
        // buttonFour = (JButton) event.getSource();
        if (event.getSource() == buttonFour) {
            ButtonArray[0] = 0;
            ButtonArray[1] = 0;
            ButtonArray[2] = 0;
            ButtonArray[3] = 1;
            ButtonArray[4] = 0;
            System.out.println("Sad");

        }

        // buttonFive = (JButton) event.getSource();
        if (event.getSource() == buttonFive) {
            ButtonArray[0] = 0;
            ButtonArray[1] = 0;
            ButtonArray[2] = 0;
            ButtonArray[3] = 0;
            ButtonArray[4] = 1;
            System.out.println("Very Sad");

        }

        // System.out.println(Arrays.toString(ButtonArray));
        // ImageIcon image = (imageList[i]);

    }

}

Upvotes: 0

Views: 633

Answers (1)

tobias_k
tobias_k

Reputation: 82899

I don't really see what most parts of you code are supposed to do. So instead, here's a minimal example that should do what you are asking about: One label, and two buttons setting different images to that label.

ImageIcon[] images = new ImageIcon[] {
        new ImageIcon("foo.gif"),
        new ImageIcon("bar.gif"),
        new ImageIcon("blub.gif")
};

JFrame frame = new JFrame("Test");
frame.getContentPane().setLayout(new FlowLayout());

JLabel label = new JLabel(images[0]);
frame.getContentPane().add(label);

JButton button1 = new JButton("Image 1");
button1.addActionListener(e -> label.setIcon(images[0]));
frame.getContentPane().add(button1);

JButton button2 = new JButton("Image 2");
button2.addActionListener(e -> label.setIcon(images[1]));
frame.getContentPane().add(button2);

frame.pack();
frame.setVisible(true);

Note that this is using Lambda functions (Java 8) but you can do the same with one or more "real" ActionListener classes. The important part is that you call label.setIcon(theImage); this part seems to be missing in your code.


If instead you want to cycle through a list or array of pictures, you can do like this:

AtomicInteger index = new AtomicInteger(0);
JButton buttonCycle = new JButton("Cycle");
buttonCycle.addActionListener(e -> label.setIcon(images[index.getAndIncrement() % images.length]));
frame.getContentPane().add(buttonCycle);

Here, the AtomicInteger is used so I can declare it as a local variable and use it in the lambda. You can just as well use a regular int if you make it a member variable of the surrounding class.

private int c = 0;
...
buttonCycle.addActionListener(e -> label.setIcon(images[c++ % images.length]));

The takeaway is: Create a counter variable, increment it each time the button is called and set the label's icon to the element with that count, module the size of the array.

Upvotes: 1

Related Questions