简体   繁体   English

JavaFX 标签不更新

[英]JavaFX Label doesn't update

I have a .fxml file with a <Label> tag and a <ImageView> in it.我有一个带有<Label>标记和<ImageView>的 .fxml 文件。 In my controller class there an event function that is called, the ImageView is updated but my label stays the same.在我的控制器类中有一个被调用的事件函数,ImageView 被更新但我的标签保持不变。 All help is definetly appreciated.非常感谢所有帮助。

My controller:我的控制器:

public class LayoutController implements BrowserPlaybackEvent {
    @FXML
    public ImageView currentTrack;

    @FXML
    public Label trackTitle;

    SpotySync spotySync;
    public LayoutController(){
        this.spotySync = Main.spotySync;
        spotySync.addBrowserPlaybackEventListerners(this);
    }



    @Override
    public void browserPlaybackEvent(JSONObject playBackState) {
        System.out.println("Layout controller handling browser event");
        Track track = new Track((JSONObject) ((JSONObject) playBackState.get("track_window")).get("current_track"));
        trackTitle.setText(track.name);
        currentTrack.setImage(track.tAlbum.image);

    }
}

.jfxml file: .jfxml 文件:

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="729.0" prefWidth="861.0" styleClass="HBox" stylesheets="@stylesheet.css" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nl.nothic.spotysync.ui.LayoutController">

   <children>
      <Pane prefHeight="200.0" prefWidth="617.0" />
      <Pane prefHeight="200.0" prefWidth="200.0" />
      <Pane id="currentlyPlaying" maxHeight="300.0" minHeight="300.0" prefHeight="300.0" prefWidth="200.0">
         <Label>
            <graphic>

               <ImageView fx:id="currentTrack">
                  <image>
                     <Image url="https://i.scdn.co/image/ab67616d00001e02eb9888357d422ff3e6bf0321" />
                  </image>
               </ImageView>

            </graphic>
         </Label>

         <Label fx:id="trackTitle" alignment="CENTER" layoutX="300.0" layoutY="14.0" prefHeight="45.0" prefWidth="550.0" text="Song title" textAlignment="CENTER" textFill="WHITE" wrapText="true">
            <font>
               <Font size="30.0" />
            </font>
         </Label>

      </Pane>
   </children>
</VBox>

Application class:应用类:

public class Gui extends Application{
    SpotySync spotySync = null;

    @Override
    public void start(Stage primaryStage) throws IOException {

        Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getClassLoader().getResource("nl/nothic/spotysync/ui/Layout.fxml")));


        Scene scene = new Scene(root,600,600);
        scene.getStylesheets().add(getClass().getClassLoader().getResource("nl/nothic/spotysync/ui/stylesheet.css").toExternalForm());

        primaryStage.setTitle("SpotySync");
        primaryStage.setScene(scene);
        primaryStage.show();


    }

    public static void launch(){
        Application.launch();
    }

}

The problem I am getting is in the controller.我遇到的问题是在控制器中。 When the browserPlaybackEvent is called a new track is played and the application updated.当调用 browserPlaybackEvent 时,将播放新曲目并更新应用程序。 The Imageview (currentTrack) is updated, also visually in the application itself. Imageview (currentTrack) 更新,也在应用程序本身中可视化。 I have debugged the code and track.name is valid text and the object is also updated with new text.我已经调试了代码,track.name 是有效文本,并且对象也更新为新文本。 However in the application only the image gets updated.但是在应用程序中,只有图像会更新。

I have spent about an hour trying to pinpoint the issue without results.我花了大约一个小时试图查明问题,但没有结果。

Thanks in advance提前致谢

I am not sure why but I think the problem has to do that the event is called on a different Thread (I am no expert on threading so I might be wrong).我不知道为什么,但我认为问题在于该事件是在不同的线程上调用的(我不是线程方面的专家,所以我可能是错的)。 The reason I didn't suspect this was because the ImageView element did update.我没有怀疑这是因为 ImageView 元素确实更新了。

For the solution.对于解决方案。 To get the correct thread to update the text I used:要获得正确的线程来更新我使用的文本:

Platform.runLater(() -> {
    trackTitle.setText(track.name);
});

This (I think) moves the update to the correct Thread and updates the <Label> .这(我认为)将更新移动到正确的 Thread 并更新<Label>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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