我怎样才能有多个SplitPanes,包括带有javaFX的孩子?

时间:2015-04-14 09:31:46

标签: javafx duplicates containers nodes children

我的结构:

AnchorPane
 | 
 ScrollPane
  |
  FlowPane
   |
   SplitPane
    |
    StackedBarChart
    TableView

如何将SplitPane及其子级复制到未知数字的重复项中?

如何为每个副本设置新的变量名称?

在Scene Builder中,我右键单击SplitPane并选择Duplicate。但是,问题是在编写程序之前重复数量不可用,直到运行时才知道。

2 个答案:

答案 0 :(得分:2)

最佳方法是为SplitPane创建单独的FXML,根据需要多次加载并将其添加到FlowPane

让我们将新的fxml称为split.fxml。 fxml的控制器称为SplitController

然后,我们将根据需要多次加载此fxml,获取根并将其添加到FlowPane,现在它位于单独的fxml中。

让我们将主要的fxml称为container.fxml。 fxml的控制器称为ContainerController

回答你的一些问题:

  

我怎样才能将我的SplitPane与其子项一起复制到未知数量的重复项?

只需加载fxml多次。

  

如何为每个副本设置新的变量名称?

你有控制器,你可以用变量识别它。您也可以在分割窗格中设置classid

完整的代码......

<强> split.fxml

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

<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.chart.StackedBarChart?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>

<SplitPane fx:id="splitPane" dividerPositions="0.25" prefHeight="363.0" prefWidth="773.0" fx:controller="SplitController"  xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <items>
        <TableView fx:id="tableView" prefHeight="458.0" prefWidth="251.0">
            <columns>
                <TableColumn prefWidth="75.0" text="C1" />
                <TableColumn prefWidth="75.0" text="C2" />
            </columns>
        </TableView>
        <StackedBarChart fx:id="stackedBarChart" prefHeight="210.0" prefWidth="107.0">
            <xAxis>
                <CategoryAxis fx:id="categoryAxis" side="BOTTOM" label="Year" />
            </xAxis>
            <yAxis>
                <NumberAxis fx:id="numberAxis" side="LEFT" label="Value"/>
            </yAxis>
        </StackedBarChart>
    </items>
</SplitPane>

<强> SplitController.java

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TableView;

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

public class SplitController implements Initializable{

    @FXML
    private SplitPane splitPane;

    @FXML
    private StackedBarChart<String, Number> stackedBarChart;

    @FXML
    private TableView<String> tableView;

    @FXML
    private CategoryAxis categoryAxis;

    @FXML
    private NumberAxis numberAxis;

    private ObservableList<String> tableList;

    private ObservableList<Integer> chartValueList = FXCollections.observableArrayList();

    public void setTableList(ObservableList<String> tableList) {
        this.tableList = FXCollections.observableList(tableList);
    }

    public void setChartValueList(ObservableList<Integer> chartValueList) {
        this.chartValueList = FXCollections.observableList(chartValueList);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    public void setChart(){
        XYChart.Series<String,Number> series1 = new XYChart.Series();

        series1.getData().add(new XYChart.Data("January", chartValueList.get(0)));
        series1.getData().add(new XYChart.Data("February", chartValueList.get(1)));
        series1.getData().add(new XYChart.Data("March", chartValueList.get(2)));
        series1.getData().add(new XYChart.Data("April", chartValueList.get(3)));

        XYChart.Series<String,Number> series2 = new XYChart.Series();

        series2.getData().add(new XYChart.Data("January", chartValueList.get(0)));
        series2.getData().add(new XYChart.Data("February", chartValueList.get(1)));
        series2.getData().add(new XYChart.Data("March", chartValueList.get(2)));
        series2.getData().add(new XYChart.Data("April", chartValueList.get(3)));

        stackedBarChart.getData().addAll(series1, series2);
    }
}

<强> container.fxml

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

<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.FlowPane?>

<BorderPane xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ContainerController">
   <center>
        <ScrollPane fx:id="scrollPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" BorderPane.alignment="CENTER">
            <content>
                <FlowPane fx:id="flowPane" maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="353.0" prefWidth="745.0">

                </FlowPane>
            </content>
        </ScrollPane>
   </center>
</BorderPane>

<强> ContainerController.java

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.FlowPane;

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

public class ContainerController implements Initializable{

    @FXML
    private AnchorPane anchorPane;

    @FXML
    private ScrollPane scrollPane;

    @FXML
    private FlowPane flowPane;

    private final int NUMBER_OF_COPIES = 5;
    private final int NUMBER_OF_ROWS = 10;
    private ObservableList<String> tableList = FXCollections.observableArrayList();
    private ObservableList<Integer> chartValueList = FXCollections.observableArrayList();


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        try {
            for(int i=0; i<NUMBER_OF_COPIES; i++) {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("/split.fxml"));
                SplitPane pane = loader.load();
                SplitController controller = loader.getController();
                fillChartValueList();
                controller.setChartValueList(chartValueList);
                controller.setChart();
                flowPane.getChildren().add(pane);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void fillChartValueList(){

        // clear previous values
        chartValueList.clear();

        Random rand = new Random();
        int min = 1000;
        int max = 8000;


        for(int i=0; i<NUMBER_OF_ROWS; i++) {
            int randomNum = rand.nextInt((max - min) + 1) + min;
            chartValueList.add(randomNum);
        }
    }
}

Main.java - 加载容器,将其设置为舞台并显示

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {

    public Main() {
        super();
    }

    @Override
    public void start(Stage primaryStage) throws IOException{

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/container.fxml"));
        Parent parent = loader.load();
        ContainerController controller = loader.getController();

        Scene scene = new Scene(parent, 600, 600);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.show();

    }

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

<强>截图

enter image description here

注意 - TableView是空的,因为根据问题我没有考虑填写它很重要。

答案 1 :(得分:0)

有效:

    SplitPane sp=new SplitPane(splitPane);
    sp.setId(machine);
    flowPane.getChildren().add(new SplitPane(sp));