Search code examples
javaimageswinguser-interfacepanel

Java swing gui change background of jlabel and make it reset


I have a gui that has: a label at the top a JFrame at the bottom with 2 Buttons called left and right a panel in center that is gridlayout with 2 JLabel to either display an image or change the back ground color. (currently the background color is set to black for both jLabels).

*what I would like to happen.

When you click on button "left" the image appears on lblPicture1 and lblPicture2 has a black background and no image. and vise versa for the right button. and when you click on the left again, it repeats this cycle.

I accomplish that however, when i click the left and right button I just have two images and neither one has a black background.

I belive this is due to the image not resetting.

Can you direct me to the right place on how I can get this to work?

Thank you

package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ExampleGUI extends JFrame {

    private JPanel contentPane;
    private JLabel lblPicture1;
    private JLabel lblPicture2;
    private int change;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ExampleGUI frame = new ExampleGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ExampleGUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JLabel lblExampleGui = new JLabel("Example GUI");
        lblExampleGui.setBorder(new EmptyBorder(8, 0, 8, 0));
        lblExampleGui.setFont(new Font("Lucida Grande", Font.PLAIN, 24));
        lblExampleGui.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(lblExampleGui, BorderLayout.NORTH);

        JPanel panelButton = createPanelButton();
        contentPane.add(panelButton, BorderLayout.SOUTH);

        JButton btnLeft = createBtnLeft();
        panelButton.add(btnLeft);

        JButton btnRight = createBtnRight();
        panelButton.add(btnRight);

        JPanel panelCenter = createPanelCenter();
        contentPane.add(panelCenter, BorderLayout.CENTER);

        JLabel lblPicture1 = createLblPicture1();
        panelCenter.add(lblPicture1);

        JLabel lblPicture2 = createPicture2();
        panelCenter.add(lblPicture2);
    }

    public JLabel createPicture2() {
        lblPicture2 = new JLabel();
        lblPicture2.setOpaque(true);
        lblPicture2.setBackground(Color.BLACK);
        return lblPicture2;
    }

    public JLabel createLblPicture1() {
        lblPicture1 = new JLabel();
        lblPicture1.setOpaque(true);
        lblPicture1.setBackground(Color.BLACK);
        //lblPicture1.setIcon(new ImageIcon(ExampleGUI.class.getResource("/gui/schlange.gif")));
        return lblPicture1;
    }

    public JPanel createPanelCenter() {
        JPanel panelCenter = new JPanel();
        panelCenter.setLayout(new GridLayout(0, 2, 8, 0));
        return panelCenter;
    }

    public JButton createBtnRight() {
        JButton btnRight = new JButton("right");
        btnRight.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //TODO
                lblPicture1.setBackground(Color.BLACK);
                lblPicture2.setIcon(new ImageIcon(ExampleGUI.class.getResource("/gui/schlange.gif")));
            }
        });
        btnRight.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
        return btnRight;
    }

    public JButton createBtnLeft() {
        JButton btnLeft = new JButton("left");
        btnLeft.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //TODO
                lblPicture2.setBackground(Color.BLACK);
                lblPicture1.setIcon(new ImageIcon(ExampleGUI.class.getResource("/gui/schlange.gif")));

            }
        });
        btnLeft.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
        return btnLeft;
    }

    public JPanel createPanelButton() {
        JPanel panelButton = new JPanel();
        return panelButton;
    }

}

Solution

  • The background is painted beneath the icon, so if the icon is not reset, then it will continue to be displayed.

    You can simply set the icon property by passing it null, for example

    public JButton createBtnLeft() {
        JButton btnLeft = new JButton("left");
        btnLeft.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //TODO
                lblPicture2.setIcon(null);
                lblPicture2.setBackground(Color.BLACK);
                lblPicture1.setIcon(new ImageIcon(ExampleGUI.class.getResource("/gui/schlange.gif")));
    
            }
        });
        btnLeft.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
        return btnLeft;
    }