簡體   English   中英

從控制器到應用程序線程的Javafx調用方法

[英]Javafx call method from controller to application thread

我正在嘗試使用activeMQ創建一個小的消息傳遞程序。 該消息應顯示在右下角的一個小窗口中。 該消息是使用javafx創建的。 現在,我正在努力嘗試更改Windows文本的方法。 問題是,我不知道如何在應用程序線程中而不是在使用者線程中調用該方法。

public class Notification implements Initializable, MessageReciever {

private static Logger log                = LoggerFactory.getLogger( Notification.class );

String                infourl            = "/info.png";
String                warningurl         = "/warning.png";
String                errorurl           = "/error.png";
final String          HeroldStartMessage = "Herold started.";
@FXML
Rectangle             rectangleColor;
@FXML
ImageView             imageIcon;
@FXML
Label                 topicLabel;
@FXML
Label                 messageLabel;
@FXML
Label                 closeLabel;
@FXML
AnchorPane            controlAnchor;

public void messageRecieved( final Message message, final Topic topic ) {


    Notification.this.topicLabel.setText( message.getHeader() );
    Notification.this.messageLabel.setText( message.getText() );

    if ( message.getLevel().contains( "info" ) ) {
        log.info( "message level contains info" );
        Notification.this.rectangleColor.setFill( Paint.valueOf( "#1b2db5" ) );
        Notification.this.imageIcon.setImage( new Image( Notification.this.infourl ) );
        log.info( "Color and Image has been changed." );
    } else if ( message.getLevel().contains( "warning" ) ) {
        log.info( "message level contains warning" );
        Notification.this.rectangleColor.setFill( Paint.valueOf( "#f8790b" ) );
        Notification.this.imageIcon.setImage( new Image( Notification.this.warningurl ) );
        log.info( "Color and Image has been changed." );
    } else if ( message.getLevel().contains( "error" ) ) {
        log.info( "message level contains error" );
        Notification.this.rectangleColor.setFill( Paint.valueOf( "#ff0000" ) );
        Notification.this.imageIcon.setImage( new Image( Notification.this.errorurl ) );
        log.info( "Color and Image has been changed." );
    } else {
        //
    }
    NotificationStage.show();
}

@Override
public void initialize( final URL location, final ResourceBundle resources ) {
    this.closeLabel.setOnMouseClicked( e -> NotificationStage.dismiss() );
    this.rectangleColor.setFill( Paint.valueOf( "#1b2db5" ) );
    this.imageIcon.setImage( new Image( this.infourl ) );
    this.topicLabel.setText( "Herold" );
    this.messageLabel.setText( this.HeroldStartMessage );
    MasterConsumer.getMasterConsumer().addMessageReciever( this );
}

}

線程“ Thread-8”中的異常java.lang.IllegalStateException:在FX應用程序線程上不; currentThread =線程8

消費者的接口調用messagereciever()方法。

使用JavaFx版本進行編輯

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            Notification.this.topicLabel.setText(message.getHeader());
            Notification.this.messageLabel.setText(message.getText());
        }
    });

Platform.runLaterRunnable參數一起使用,該參數包含用於更新UI的代碼。

例:

TextArea textArea = ...

new Thread(() -> {
    String s;
    while ((s = getMessage()) != null) {
        final String newText = "\n" + s;
        Platform.runLater(() -> {
            // run update on application thread
            textArea.appendText(newText);
        });
    }
}).start();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM