问题是我想在GridPaneTab之间转移项目
所以我首先将项目添加到选项卡。

然后我将相同的项目添加到GridPane.It正常工作到这里。但是然后我想将该项目添加回选项卡中,我正在使用

tab.setContent(item);


但这没有用。既没有从GridPane中删除该项目,也没有将该项目作为内容添加到选项卡。为什么会这样?


.java文件:


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->");
                }
            });
        }

    }

}



和.fxml文件:


<?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>

最佳答案

节点不能在场景图中出现两次; 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->");
    }
});

关于java - JavaFX TabPane,Tab.setContent()不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39023718/

10-13 04:23