使用Java从相同的FXML构建单独的选项卡

时间:2014-10-03 10:18:08

标签: java javafx fxml

如何将相同的FXML层次结构加载到两个不同的根中? 我试过这个:

protected void openPrivateSession(String remoteNick, int port, boolean isHost) {
    FXMLLoader loader =
        new FXMLLoader(getClass().getResource("PrivateTab.fxml"));
    try {
        PrivateChat pcc = new PrivateChatController(remoteNick, port, isHost);
        loader.setController(pcc);
    if (isHost) {
        connectRemoteUser(remoteNick, pcc.getPort());
    }
    Tab privateChat = new Tab(remoteNick);
    loader.setRoot(privateChat);
    loader.load();
    clientController.addPrivateChatTab(privateChat);
} catch (IOException e) {
    e.printStackTrace();
}

在客户端程序中。这是客户端控制器:

public class ClientController implements Initializable {
    private Client associatedClient;
    private String hostname;
    private int port;

    private Stage primaryStage;
    private String title;

    @FXML
    private VBox nickList;

    @FXML
    private TextArea chatArea;

    @FXML
    private TextField msgBar;

    @FXML
    private Button sendButton;

    @FXML
    private MenuItem connectDisconnect;

    @FXML
    private TabPane privateChatPane;

    @FXML
    private MenuItem settingsButton;

    @FXML
    private Stage settings;

    .
    .
    .

}

但是,它将相同的privateChat选项卡加载到同一个客户端(而不是一个本地和一个远程 - 这意味着privateChatPane和可能所有其他FXML对象具有相同的引用。)

所以我想我的最后一个问题是......你怎么能将相同的FXML注入2个不同的对象并保持它们的独特性?

好的MCVE:

class classA extends Application{

 public static void main(String[] args){ launch(args); }

 private ClassAController cac;

 public void start(Stage s){
  FXMLLoader loader = new FXMLLoader(getClass().getResource("ClassA.fxml");
  loader.setController(cac = new ClassAController());
  s.setScene(new Scene(loader.load()));
  s.show();
 }

 public void onSomeEvent(String s){
  FXMLLoader loader = new FXMLLoader(getClass().getResource("PrivateChat.fxml");
  loader.setController(new PrivateChatController());
  Tab tab = new Tab(s);
  loader.setRoot(tab);
  cac.addPrivateChatTab(tab);
 }
}

class ClassAController implements Initializeable{
@FXML TabPane privateChatPane;

 public void initialize(URL url, ResourceBundle resource){}

 //Using this method causes the add of the same Tab twice to the same app instead once for each app
 public void addPrivateChatTab(Tab privateChat){
 privateChatPane.getTabs().add(privateChat);
}

我正在运行2个ClassA应用程序实例。当我使用addPrivateChatTab()时,我在同一个应用程序实例中两次获得相同的选项卡。

1 个答案:

答案 0 :(得分:0)

好的,所以我解决了这个问题,this帖子很有帮助。 我的代码改变了一点:

protected void addPrivateChatTab(String remoteNick, int port, boolean isHost) {
 FXMLLoader loader =
  new FXMLLoader(getClass().getResource("PrivateTab.fxml"));
 try {
  loader.setController(new PrivateChat(remoteNick, port, isHost));
  if (isHost) {
   associatedClient.connectRemoteUser(remoteNick, PrivateChat.getPort());
  }
  Tab privateChat = new Tab(remoteNick);
  SplitPane sp = loader.load();
  privateChat.setContent(sp);
  addPrivateChatTab(privateChat);
  } catch (IOException e) {
   e.printStackTrace();
}

更具体针对googlers:

  1. 创建包装对象(Tab privateChat = new Tab(remoteNick))。
  2. 加载您要添加的动态对象的内容(SplitPane sp = loader.load())。
  3. 将包装对象的内容设置为您加载的对象(privateChat.setContent(sp)
  4. 最后将完成的对象添加到舞台/父节点(addPrivateChatTab(privateChat)