[英]JavaFX Label doesn't update
我有一個帶有<Label>
標記和<ImageView>
的 .fxml 文件。 在我的控制器類中有一個被調用的事件函數,ImageView 被更新但我的標簽保持不變。 非常感謝所有幫助。
我的控制器:
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 文件:
<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>
應用類:
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();
}
}
我遇到的問題是在控制器中。 當調用 browserPlaybackEvent 時,將播放新曲目並更新應用程序。 Imageview (currentTrack) 更新,也在應用程序本身中可視化。 我已經調試了代碼,track.name 是有效文本,並且對象也更新為新文本。 但是在應用程序中,只有圖像會更新。
我花了大約一個小時試圖查明問題,但沒有結果。
提前致謝
我不知道為什么,但我認為問題在於該事件是在不同的線程上調用的(我不是線程方面的專家,所以我可能是錯的)。 我沒有懷疑這是因為 ImageView 元素確實更新了。
對於解決方案。 要獲得正確的線程來更新我使用的文本:
Platform.runLater(() -> {
trackTitle.setText(track.name);
});
這(我認為)將更新移動到正確的 Thread 並更新<Label>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.