Rameez Mahmood February 2016

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(imageLi        

Answers


tobias_k February 2016

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 co

Post Status

Asked in February 2016
Viewed 2,765 times
Voted 8
Answered 1 times

Search




Leave an answer