Java:一个线程内的SetText

时间:2014-07-29 14:05:15

标签: java multithreading javafx settext

任何人都可以帮助我:

我有一些TextFields我想用作计时器(或时钟),所以我将文本设置在我在控制器类中调用的线程中。

package application;

import java.net.URL;
import java.util.ResourceBundle;

import org.joda.time.DateTime;

import javafx.beans.property.LongProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import jdk.nashorn.internal.runtime.FindProperty;

public class MainWindowController extends Thread implements Initializable{

@FXML
private TextField dayText;

@FXML
private TextField monthText;

@FXML
private TextField yearText;

@FXML
private TextField hoursText;

@FXML
private TextField minutesText;

@FXML
private TextField secondsText;

@FXML
private TextField julianDayText;

@Override
public void initialize(URL arg0, ResourceBundle arg1) {

    this.start();

}

@Override
public void run() {

        while(true){

            DateTime d = new DateTime(System.currentTimeMillis());

            dayText.setText(String.valueOf(d.getDayOfMonth()));
            monthText.setText(String.valueOf(d.getMonthOfYear()));
            yearText.setText(String.valueOf(d.getYear()));

            hoursText.setText(String.valueOf(d.getHourOfDay()));
            minutesText.setText(String.valueOf(d.getMinuteOfHour()));
            secondsText.setText(String.valueOf(d.getSecondOfMinute()));
        }
    }   
}

我不知道为什么在运行我的代码之后我得到一个NullPointerException(它有点工作然后崩溃):

Exception in thread "Thread-4" java.lang.NullPointerException
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:339)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.scene.control.TextInputControl$TextProperty.fireValueChangedEvent(TextInputControl.java:1116)
at javafx.scene.control.TextInputControl$TextProperty.markInvalid(TextInputControl.java:1120)
at javafx.scene.control.TextInputControl$TextProperty.set(TextInputControl.java:1056)
at javafx.scene.control.TextInputControl.setText(TextInputControl.java:279)
at application.MainWindowController.run(MainWindowController.java:208)

请提前帮助并表示感谢

2 个答案:

答案 0 :(得分:4)

您可以使用TimeLine和KeyFrames。只需使用您自己的代码替换showTime()即可。 并且不要扩展线程。

public class FXMLTimeController implements Initializable {

    @FXML
    private TextField txtTime;

    //timeline
    private Timeline timeline;

    private void showTime() {
        txtTime.setText((new Date()).toString());
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        timeline = new Timeline();
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.setAutoReverse(false);

        timeline.getKeyFrames().add(
                new KeyFrame(Duration.seconds(1),
                        new EventHandler<ActionEvent>() {
                            @Override public void handle(ActionEvent event) {
                                showTime();
                            }
                        }));
        timeline.play();
    }

}

答案 1 :(得分:0)

我建议在传入JavaFX的Platform.runLater(Runnable runnable)字段时调用initialize()中的TextField,而不是让控制器扩展Thread,因为JavaFX会将控制器用于自身。

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        Platform.runLater(
            new someThread(dayText, monthText, yearText,
            hoursText, minutesText, secondsText));
    }

    public class someThread implements Runnable {

        public someThread(TextField... textFields) {
            // Create local variables
        }

        @Override
        public void run() {
            // while (true) loop goes here
        }

    }

您还可以使用this作为someThread的参数传入您的控制器,并使您的TextField公开,以便someThread可以访问它们。