我想从gridpane中删除fxml(作为新列添加)。现在,在单击按钮后,方法 addNextToCompare 将一列添加到 mainGridPaneCarCompare ,并在该位置插入来自FXMLLoader的新fxml。
在我的 deleteColumn 方法中,我尝试了 mainGridPaneCarCompare.getChildren()。removeAll(...); 但它返回空指针异常。方法 getChildren.clear()在mainGridPane中留下空白区域。我的计划从一开始就错了吗?我应该创建fxml的 List 并分别删除项目吗?
主要课程:
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
try {
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/resources/mainScene.fxml"));
new GridPane();
GridPane tempGridPane;
tempGridPane = loader.load();
Scene scene = new Scene(tempGridPane);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
}
fxml的控制器:
public class Controller {
@FXML
private GridPane mainGridPaneCarCompare;
@FXML
private AnchorPane addCarAnchorPane;
@FXML
private Button addCarButton;
@FXML
private GridPane nextColumnGridPane;
@FXML
private void initialize() {
}
@FXML
public void addNextToCompare() {
//move anchorPane with button addNextCar one column to right
if (GridPane.getColumnIndex(addCarAnchorPane) < 4) {
GridPane.setColumnIndex(addCarAnchorPane, GridPane.getColumnIndex(addCarAnchorPane) + 1);
} else {
addCarButton.setDisable(true);
}
//load new fxml
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/resources/scene2.fxml"));
GridPane temporaryGridPane = new GridPane();
try {
temporaryGridPane = loader.load();
} catch (Exception e) {
System.out.println(e);
}
//insert nextCarCompareWindow.fxml
mainGridPaneCarCompare.add(temporaryGridPane, GridPane.getColumnIndex(addCarAnchorPane) - 1, 0, 1, 6);
}
@FXML
public void deleteColumn() {
try {
System.out.println("inside nextgridcarcompare: " + nextColumnGridPane.getChildren());
nextColumnGridPane.getChildren().clear();
} catch (Exception e) {
System.out.println(e);
}
}
}
MainScene.fxml:
<GridPane fx:id="mainGridPaneCarCompare" gridLinesVisible="true" hgap="5.0" vgap="5.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints prefWidth="250.0" />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints prefHeight="200.0" />
<RowConstraints prefHeight="50.0" />
<RowConstraints prefHeight="180.0" />
<RowConstraints prefHeight="150.0" />
<RowConstraints prefHeight="150.0" />
<RowConstraints prefHeight="100.0" />
</rowConstraints>
<children>
<AnchorPane prefHeight="200.0" prefWidth="250.0">
<children>
<ImageView fitHeight="140.0" fitWidth="220.0" layoutX="15.0" layoutY="22.0" pickOnBounds="true" preserveRatio="true" />
<HBox layoutX="15.0" layoutY="170.0" spacing="5.0">
<children>
<Button mnemonicParsing="false" text="Change car" />
<Button mnemonicParsing="false" text="Edit" />
</children>
</HBox>
<Label fx:id="selectedCarLabel" layoutX="102.0" layoutY="5.0" text="Title" />
</children>
</AnchorPane>
<AnchorPane fx:id="addCarAnchorPane" GridPane.columnIndex="1">
<children>
<Button fx:id="addCarButton" layoutX="96.0" layoutY="87.0" mnemonicParsing="false" onAction="#addNextToCompare" text="Add car" AnchorPane.bottomAnchor="88.0" AnchorPane.leftAnchor="96.0" AnchorPane.rightAnchor="97.0" AnchorPane.topAnchor="87.0" />
</children>
</AnchorPane>
<AnchorPane prefWidth="250.0" GridPane.rowIndex="1">
<children>
<ListView prefWidth="170.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane GridPane.rowIndex="2">
<children>
<ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane GridPane.rowIndex="3">
<children>
<ListView layoutY="177.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane GridPane.rowIndex="4">
<children>
<ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane GridPane.rowIndex="5">
<children>
<ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</children>
</GridPane>
SecondScene.fxml
<GridPane fx:id="nextColumnGridPane" accessibleRole="NODE" gridLinesVisible="true" hgap="5.0" vgap="5.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints prefWidth="250.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="-Infinity" prefHeight="200.0" />
<RowConstraints prefHeight="50.0" />
<RowConstraints prefHeight="180.0" />
<RowConstraints prefHeight="150.0" />
<RowConstraints prefHeight="150.0" />
<RowConstraints prefHeight="100.0" />
</rowConstraints>
<children>
<AnchorPane>
<children>
<ImageView fitHeight="140.0" fitWidth="220.0" layoutX="15.0" layoutY="22.0" pickOnBounds="true" preserveRatio="true" />
<HBox layoutX="15.0" layoutY="170.0" spacing="5.0">
<children>
<Button fx:id="deleteButton" mnemonicParsing="false" onAction="#deleteColumn" text="Delete" />
<Button mnemonicParsing="false" text="Change car" />
<Button mnemonicParsing="false" text="Edit" />
</children>
</HBox>
<Label layoutX="102.0" layoutY="5.0" text="Title" />
</children>
</AnchorPane>
<AnchorPane GridPane.rowIndex="1">
<children>
<ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane GridPane.rowIndex="2">
<children>
<ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane GridPane.rowIndex="3">
<children>
<ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane GridPane.rowIndex="4">
<children>
<ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane GridPane.rowIndex="5">
<children>
<ListView AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="80.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</children>
</GridPane>
这是我的第一篇文章,我希望,我能清楚表达自己。 感谢您的任何建议:)