0

tabPane を作成しました。各タブの下に、() 実際のセッション ユーザーを表示する fxml を次のコードと共に含めました。

home-tab.fxml には次の行があります。

<fx:include fx:id="topTab" source="../top-tab.fxml"/>

top-tab.fxml :

<AnchorPane maxHeight="20.0"  prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="wuendo.client.TopTabController">
   <children>
      <HBox id="hbox_top" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
         <Label id="label_session" prefHeight="20.0" text="SESSION : " />
         <Label fx:id="sessionLabel" prefHeight="20.0" text="" />  
      </HBox>
   </children>
</AnchorPane>

TopTabController.java :

@FXML public Label sessionLabel;

HomeTabController.java :

@FXML private TopTabController topTabController;


@Override
public void initialize(URL url, ResourceBundle rb) {

  URL location = getClass().getResource("../top-tab.fxml");
  FXMLLoader fxmlLoader = new FXMLLoader(location);
  AnchorPane root = null;
  try {
     root = (AnchorPane) fxmlLoader.load();
  }  catch (IOException ex) {
     Logger.getLogger(HomeTabController.class.getName()).log(Level.SEVERE, null, ex);
  }
  topTabController = (TopTabController) fxmlLoader.getController();

  Label txt = (Label) root.lookup("#sessionLabel");
  txt.setText("blabla");

  System.out.println("sessionLabel= " + topTabController.sessionLabel.getText());
}

これを実行すると、コンソールに「blabla」が出力されますが、ラベルはプログラムで変更されません (gui)

値が更新されたことを確認するにはどうすればよいですか?

皆さん、ありがとうございました

4

1 に答える 1

2

The FXMLLoader is already created the TopTabController while the home-tab.fxml is being loaded. And it is the rendered one in the scene. However you are creating/loading another instance of TopTabController which is not added to the any scene. And you are changing the text of the label in the second one. The correct approach is to modify the already loaded 1st instance and not to load other one:

@Override
public void initialize(URL url, ResourceBundle rb) {
    topTabController.sessionLabel.setText("Real blabla");
    System.out.println("sessionLabel= " + topTabController.sessionLabel.getText());
}

Side note, The link you provided in the comment was useful.

于 2012-08-24T12:47:52.783 に答える