2

I have a FXML file with a gridpane in it, and want to add another item in it with JavaFX.

If I dont use the FXML file, I can add items in the gridpane.

How to add items in the fxml gridpane? (thought something with getChildren.add, but cant find it)

For this I'll use a simple label, but in my program i have 100+ lines of code.

Main.java:

package fotos;

import java.io.IOException;
import java.io.InputStream;
import javafx.scene.control.Label;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 *
 * @author Geert
 */
public class Fotos extends Application
{

    private static Stage primaryStage;

    @Override
    public void start(Stage stage)
    {
        primaryStage = stage;
        openWindow(this.getClass(), "/fotos/SelectPicture.fxml");
    }

    public static void openWindow(Class<?> c, String fxml)
    {
        Scene scene = null;
        try
        {
            Parent root = FXMLLoader.load(c.getResource(fxml));
            scene = new Scene(root);
        }
        catch (IOException ex)
        {
            System.out.println(ex.getMessage());

            return;
        }

        primaryStage.setResizable(false);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

SelectPicture.fxml:

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
  <children>
    <GridPane fx:id="gridPanePhoto" layoutX="14.0" layoutY="24.0" prefHeight="362.0" prefWidth="572.0">
      <children>
        <TreeView fx:id="mapTree" prefHeight="372.0" prefWidth="162.0" GridPane.columnIndex="0" GridPane.rowIndex="1" />
        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Maps" GridPane.columnIndex="0" GridPane.rowIndex="0" />
        <Text scaleX="1.0" scaleY="0.9999999999607461" strokeType="OUTSIDE" strokeWidth="0.0" text="Picture's" GridPane.columnIndex="1" GridPane.rowIndex="0" />
      </children>
      <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="284.0" minWidth="10.0" prefWidth="188.0" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="440.0" minWidth="10.0" prefWidth="384.0" />
      </columnConstraints>
      <rowConstraints>
        <RowConstraints maxHeight="186.0" minHeight="10.0" prefHeight="32.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="344.0" minHeight="10.0" prefHeight="340.0" vgrow="SOMETIMES" />
      </rowConstraints>
    </GridPane>
    <MenuBar layoutX="0.0" layoutY="0.0" prefWidth="600.0">
      <menus>
        <Menu mnemonicParsing="false" text="File">
          <items>
            <MenuItem mnemonicParsing="false" text="Close" />
          </items>
        </Menu>
        <Menu mnemonicParsing="false" text="Edit">
          <items>
            <MenuItem mnemonicParsing="false" text="Delete" />
          </items>
        </Menu>
        <Menu mnemonicParsing="false" text="Help">
          <items>
            <MenuItem mnemonicParsing="false" text="About" />
          </items>
        </Menu>
      </menus>
    </MenuBar>
  </children>
</AnchorPane>

SelectPictureController.java:

package fotos;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TreeView;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javafx.scene.control.Label;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;

/**
 * FXML Controller class
 *
 * @author Geert
 */
public class SelectPictureController implements Initializable
{

    @FXML
    private TreeView<?> mapTree;
    @FXML
    private ImageView dutchFlag;
    @FXML
    private ImageView englishFlag;
    @FXML
    private GridPane gridPanePhoto;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb)
    {

        Label test = new Label("Test");
        gridPanePhoto.add(test,0,1);
    }

    @FXML
    private void dutchClicked(MouseEvent event)
    {
        //TODO
    }

    @FXML
    private void englishClicked(MouseEvent event)
    {
        //TODO
    }
}
Geert Berkers
  • 653
  • 7
  • 19
  • Aren't you just missing the `` attribute in the root element of the FXML? – James_D Mar 25 '15 at 18:06
  • I need to check this, but i'm using SceneBuilder 1.1. – Geert Berkers Mar 25 '15 at 18:13
  • If you use Scenebuilder, why not use that to add those items? Btw, use Scenebuilder v2.0 http://www.oracle.com/technetwork/java/javase/downloads/javafxscenebuilder-1x-archive-2199384.html – WonderWorld Mar 25 '15 at 18:20
  • We had problems with Scenebuilder 2.0. But I need to add images to a gridpane. But ill make a new gridpane with all the images inside it. We programmatically check how many pictures and then add them. – Geert Berkers Mar 25 '15 at 18:22
  • @James_D thank you! Ill deleted everything, and made a new file, there was something wrong with the fx:controller! – Geert Berkers Mar 25 '15 at 18:28

0 Answers0