简体   繁体   中英

How to create an updating value to Label in JavaFX?

im new to Java and JavaFX and I want to create a GUI with a Label which shows the current system Time. The Problem is: the Time Value doesn't update. So I wanted to create a Thread, that is updating my Time-Value and also the Label every 1 second with the command:

label_time_show.setText(curTime);

In the inizialize method (see code below) the "label_show_time" can be inizialized with any value, but when I try to setText in an other method I get the following Error:

Exception in thread "Thread-4" java.lang.NullPointerException

I commented the line in the Code where the Label is null and where the Label is NOT null.

Can someone help me with this Problem?

public class FXMLDocumentController implements Initializable, Runnable
{
    public String curTime;  // <--- main value of time (String)


    @FXML
    private Label label_time_show;
    @FXML
    private Label label_time;

    // initializer
    @Override
    public void initialize(URL url, ResourceBundle rb) 
    {    
        java.util.Date now = new java.util.Date();
        Long sysTime = now.getTime();
        String sysTimeString = sysTime.toString();    
        Integer h = now.getHours();
        Integer m = now.getMinutes();
        Integer s = now.getSeconds();
        String hh = h.toString();
        String mm = m.toString();
        String ss = s.toString();
        curTime = hh + ":" + mm + ":" + ss;


        label_show_time.setText(curTime); // <---- label_show_time is NOT null            
    }    

    // run method
    @Override
    public void run()
    {
        System.out.println("THREAD FXMLController running....");  
        while(true)
        {        
            time();            
        }        
    }

    public void time()
    {
        java.util.Date now = new java.util.Date();
        Long sysTime = now.getTime();
        String sysTimeString = sysTime.toString();    
        Integer h = now.getHours();
        Integer m = now.getMinutes();
        Integer s = now.getSeconds();
        String hh = h.toString();
        String mm = m.toString();
        String ss = s.toString();
        curTime = hh + ":" + mm + ":" + ss;

        label_show_time.setText(curTime);   // <---- label_show_time is null !!! throws ERROR
    }       
}

So, given the limited information, I'll take a blind stab at this. At the end of your initialize method your label is not null, so start your thread then.

public void initialize(URL url, ResourceBundle rb) 
{    

    //java.util.Date now = new java.util.Date();
    //Long sysTime = now.getTime();
    //String sysTimeString = sysTime.toString();    
    //Integer h = now.getHours();
    //Integer m = now.getMinutes();
    //Integer s = now.getSeconds();
    //String hh = h.toString();
    //String mm = m.toString();
    //String ss = s.toString();
    //curTime = hh + ":" + mm + ":" + ss;
    //label_show_time.setText(curTime); // <---- label_show_time is NOT null 
    //Since the label not null here, start your thread here.
    new Thread(this).start();           
}    

Then your time method would be similar except you would post things to the Platform thread.

public void time()
{
    java.util.Date now = new java.util.Date();
    Long sysTime = now.getTime();
    String sysTimeString = sysTime.toString();    
    Integer h = now.getHours();
    Integer m = now.getMinutes();
    Integer s = now.getSeconds();
    String hh = h.toString();
    String mm = m.toString();
    String ss = s.toString();
    curTime = hh + ":" + mm + ":" + ss;
    Platform.runLater(()->{
        label_show_time.setText(curTime);
    });
}       

You might want to put a sleep in your run method since you only need to update every second.

public void run(){
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    while (true){   
        Date date = new Date();
        label_show_time.setText(dateFormat.format(date));
        try{
            TimeUnit.SECONDS.sleep(1);
        }catch(InterruptedException e){
        }
    }
}

mb it helps you? and init your label.

update:

 public void initialize(URL url, ResourceBundle rb) {
        ...
 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    Date date = new Date();
                    System.out.println(dateFormat.format(date));
                    try{
                        TimeUnit.SECONDS.sleep(1);
                    }catch(InterruptedException e){
                    }
                }
            }
        }).start();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM