开发者

Changing label icons in eclipse with thread interaction

I'm currently developing an interactive UI which shows you if you turned on a water or lighting component of a greenhouse system.

I am having quite a challenge in making the label icons change when a button is clicked from another window which passes its flag values to a thread class, which, in turn makes the icon change

Controller -> ThreadLogic -> NewSim

here are some sample test codes i used in the UI window i'm still a beginner so I am using the auto-generated codes from eclipse

there seems to be some problem with pasting the code

this is the test code snippet I use to modify the flag values

ThreadLogic

while(!t.interrupted()){

            sim = new NewSim();
            try {
                sim.setLightStatus(1);
                System.out.println("flag is 1");
                t.sleep(5000);

                sim.setLightStatus(0);
                System.out.println("flag is 0");
                t.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


    }

NewSim

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

//VS4E -- DO NOT REMOVE THIS LINE!
public class NewSim extends JFrame {

    private static final long serialVersionUID = 1L;
    protected JLabel water;
    protected JLabel light;
    Icon waterIcon = null;
    Icon lightIcon = null;  
    private int lightFlag = 0;
    private JPanel iconPane;
    private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";

    public NewSim() {
        initComponents();
    }

    private void initComponents() {
        setLayout(new GroupLayout());
        add(getIconPane(), new Constraints(new Leading(4, 313, 10, 10), new Leading(4, 232, 10, 10)));
        setSize(320, 240);
    }

    private JPanel getIconPane() {
        if (iconPane == null) {
            iconPane = new JPanel();
            iconPane.setLayout(new GroupLayout());
            iconPane.add(getWaterLabel(), new Constraints(new Leading(163, 10, 10), new Leading(79, 12, 12)));
            iconPane.add(getLightLabel(), new Constraints(new Leading(115, 12, 12), new Leading(79, 12, 12)));
        }
        return iconPane;
    }

    private JLabel getLightLabel() {
        if (light == null) {
            light = new JLabel();
            System.out.println(this.lightFlag);
            setLightStatus(0); 

        }
        return light;
    }

    private JLabel getWaterLabel() {
        if (water == null) {
            water = new JLabel();
            water.setIcon(new ImageIcon(getClass().getResource("/images/wateroff.png")));
        }
        return water;
    }
    public void setLightStatus(int lightFlag2) {
        // TODO Auto-generated method stub
开发者_高级运维        this.lightFlag = lightFlag2;
        switch(this.lightFlag){
            case 0: System.out.println("case 0");
                    light.setIcon(new ImageIcon(getClass().getResource("/images/lightoff.png")));
                    light.revalidate();
                    break;
            case 1: System.out.println("case 0");
                    light.setIcon(new ImageIcon(getClass().getResource("/images/lighton.png")));
                    light.revalidate();
                    break;
        }
    }
}


In Swing, you can't create or modify a swing component in any othe thread than the Event Dispatch Thread (EDT).

The following lines should thus be executed in the EDT :

sim = new NewSim();
sim.setLightStatus(1);
sim.setLightStatus(0);

Use SwingUtilities.invokeLater() to execute some code in the EDT rather than the current (non-EDT) thread. And read the warning present in the Javadoc of all the swing components : http://download.oracle.com/javase/6/docs/api/javax/swing/package-summary.html#threading

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜