Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Java GUI in elclipse (Lightbullb)

Hi I was wondering if someone could show me the proper way to implement my changelistener and actionlisteners methods. The images are displayed but I can't change them with button or slider. Some code may be redundant.

package playingWithGuis;

import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.SwingConstants; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener;

public class lightbulb extends JFrame implements ActionListener, ChangeListener{

private ImageIcon icons[] = new ImageIcon[4];
private boolean switchIsOn = false;
private JLabel label;
private JButton button;
private JSlider slider;
private static final int Watts20 = 20,Watts40 = 40,Watts60 = 60;

public lightbulb(){

    super("Lightbulb");

    Container c = getContentPane();

    c.setLayout(new FlowLayout());




    icons[0] =new ImageIcon(getClass().getResource("download.jpg"));
    label = new JLabel(icons[0]);
    label.setIcon(icons[0]);

    icons[1] =new ImageIcon(getClass().getResource("th.jpg"));
    //label = new JLabel(icons[1]);

    icons[2] =new ImageIcon(getClass().getResource("th (1).jpg"));
    icons[3] =new ImageIcon(getClass().getResource("41divnkkBVL._SL500_AC_SS350_.jpg"));


    slider = new JSlider(SwingConstants.HORIZONTAL, 0,  60, 0);
    slider.setMajorTickSpacing(20); 
    slider.setPaintTicks(true);
    slider.setPaintLabels(true);


    //slider.setLabelTable();


    slider.addChangeListener(this);


    //label.setHorizontalAlignment(JLabel.CENTER);

    button = new JButton("On/Off");
    button.addActionListener(this);

    c.add(label, BorderLayout.CENTER);
    c.add(button, BorderLayout.NORTH);
    c.add(slider,BorderLayout.SOUTH);


    //label.setIcon(icons);

    //switchIsOn = false;


    /*add(label, BorderLayout.CENTER);

    add(button);

    add(slider,BorderLayout.SOUTH);*/




}


@Override
public void stateChanged(ChangeEvent e) {
    if(switchIsOn) {

        if(slider.getValue() == 0) {


            label = new JLabel(icons[0]);
                            label.setIcon(icons[0]);

        }
        else if(slider.getValue() > 0 && slider.getValue() <= Watts20) {
            label = new JLabel(icons[1]);
            label.setIcon(icons[1]);


        }
        else if(slider.getValue() > Watts20 && slider.getValue() <= Watts40) {
            label = new JLabel(icons[2]);
            label.setIcon(icons[2]);


        }
        else if(slider.getValue() > Watts40 && slider.getValue() <= Watts60) {
            label = new JLabel(icons[3]);
            label.setIcon(icons[3]);


        }

    }

}

@Override
public void actionPerformed(ActionEvent e) {
    if(!switchIsOn) {
        switchIsOn = true;

        //label = new JLabel(icons[1]);

    }
    else {

        switchIsOn = false;
        label = new JLabel(icons[0]);
        label.setIcon(icons[0]);

    }

}

public static void main(String[] args) {

    lightbulb lb = new lightbulb();
    lb.setSize(400, 400);
    lb.setVisible(true);
    lb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

}