JavaFX TabPane,Tab.setContent()不起作用

时间:2016-08-18 16:58:27

标签: java javafx

问题是我想在Tabtab.setContent(item); 之间转移一个项目 所以我首先将项目添加到选项卡。

然后我将相同的项目添加到 GridPane 。它运行良好,直到此处。但是我想将项目添加回选项卡,我正在使用

package pack;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Starter extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setScene(new Scene(new UI()));
        primaryStage.centerOnScreen();
        primaryStage.show();
    }

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

    /**
     * This class contains the graphical interface
     * 
     * @author Alexander
     *
     */
    public class UI extends BorderPane implements Initializable {

        @FXML
        private Tab tab;

        @FXML
        private Button item;

        @FXML
        private GridPane gridPane;

        @FXML
        private Button transferButton;

        boolean onGridPane = false;

        /**
         * Constructor
         */
        public UI() {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("UI.fxml"));
            loader.setController(this);
            loader.setRoot(this);

            try {
                loader.load();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void initialize(URL location, ResourceBundle resources) {
            System.out.println("Main Controller has been initialized....");

            transferButton.setOnAction(a -> {
                if (!onGridPane) {
                    gridPane.add(item, 0, 0);
                    onGridPane = true;
                    transferButton.setText("<-Move to Tab");
                } else {
                    tab.setContent(item);
                    onGridPane = false;
                    transferButton.setText("Move to GridPane->");
                }
            });
        }

    }

}

但它不起作用。从GridPane中删除的项目都没有将项目作为内容添加到选项卡中。为什么会发生这种情况?

  

.java 文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<fx:root prefHeight="322.0" prefWidth="538.0" type="BorderPane" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
   <top>
      <TabPane prefHeight="141.0" prefWidth="538.0" style="-fx-border-color: red;" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
        <tabs>
          <Tab fx:id="tab" text="Tab">
               <content>
                  <Button fx:id="item" mnemonicParsing="false" text="Moving Item" />
               </content>
          </Tab>
        </tabs>
      </TabPane>
   </top>
   <bottom>
      <GridPane fx:id="gridPane" gridLinesVisible="true" style="-fx-border-color: red;" BorderPane.alignment="CENTER">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
      </GridPane>
   </bottom>
   <right>
      <Button fx:id="transferButton" mnemonicParsing="false" text="Move to GridPane-&gt;" BorderPane.alignment="CENTER">
         <font>
            <Font size="16.0" />
         </font></Button>
   </right>
   <padding>
      <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
   </padding>
   <center>
      <Label prefHeight="82.0" prefWidth="340.0" style="-fx-font-weight: bold; -fx-font-size: 15;" text="Above is a [TabPane] and Below is a [GridPane]" BorderPane.alignment="CENTER" />
   </center>
</fx:root>
  

.fxml 文件:

{{1}}

1 个答案:

答案 0 :(得分:1)

节点在场景图中不能出现两次; API已明确禁止将已经是场景图的一部分的节点添加到场景图中,因此,如果您尝试这样做,则行为是未定义的。

您需要先从场景图中删除该按钮,然后再将其添加到其他位置:

transferButton.setOnAction(a -> {
    if (!onGridPane) {
        tab.setContent(null);
        gridPane.add(item, 0, 0);
        onGridPane = true;
        transferButton.setText("<-Move to Tab");
    } else {
        gridPane.getChildren().remove(item);
        tab.setContent(item);
        onGridPane = false;
        transferButton.setText("Move to GridPane->");
    }
});