Dynamically added view wont fit Pane - java

I want to make a application with a menu on the left and the view on the right. I have different fxml file. 1 main, and multiple for the view.
Right now I load different view but the view on right doesn't his parent.
Main view
<GridPane
xmlns="http://javafx.com/javafx/8.0.112"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controller.Controller">
<columnConstraints>
<ColumnConstraints percentWidth="30.0" />
<ColumnConstraints percentWidth="70.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<GridPane maxWidth="Infinity" GridPane.columnIndex="0" GridPane.rowIndex="0">
<columnConstraints>
<ColumnConstraints percentWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
</rowConstraints>
<children>
<Button fx:id="devices" maxWidth="Infinity" mnemonicParsing="false" text="Devices" GridPane.columnIndex="0" GridPane.rowIndex="0" />
<Button fx:id="inventory" maxWidth="Infinity" mnemonicParsing="false" text="Inventory" GridPane.columnIndex="0" GridPane.rowIndex="1" />
</children>
</GridPane>
<Pane
fx:id="container"
minWidth="Infinity"
maxWidth="Infinity"
GridPane.hgrow="ALWAYS"
GridPane.vgrow="ALWAYS"
GridPane.columnIndex="1"
GridPane.columnSpan="1"
GridPane.rowIndex="0">
</Pane>
</children>
</GridPane>
Custom view load in container
<GridPane
xmlns="http://javafx.com/javafx/8.0.112"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controller.DevicesController"
GridPane.hgrow="ALWAYS"
GridPane.vgrow="ALWAYS">
<children>
<TableView
GridPane.columnIndex="0"
GridPane.columnSpan="1"
GridPane.hgrow="ALWAYS"
GridPane.vgrow="ALWAYS"
GridPane.rowIndex="0">
<columns>
<TableColumn text="Devices" />
<TableColumn text="C2" />
</columns>
</TableView>
</children>
</GridPane>
Current form
Controller
public class Controller implements Initializable {
#FXML private Pane container;
#FXML private Button devices;
#FXML private Button inventory;
private HashMap<Views, Node> views;
private enum Views {
DEVICES,
INVENTORY
}
#Override
public void initialize(URL location, ResourceBundle resources) {
try {
views = new HashMap<>();
views.put(Views.DEVICES, FXMLLoader.load(getClass().getResource("../view/" + "view-devices.fxml")));
views.put(Views.INVENTORY, FXMLLoader.load(getClass().getResource("../view/" + "view-inventory.fxml")));
devices.setOnMouseClicked(v -> load(Views.DEVICES));
inventory.setOnMouseClicked(v -> load(Views.INVENTORY));
} catch (IOException e) {
e.printStackTrace();
}
}
public void load(Views view){
if (views.containsKey(view)) {
Node newView = views.get(view);
if (!container.getChildren().contains(newView)) {
container.getChildren().add(newView);
} else {
newView.resize(container.getMaxWidth(), container.getMaxHeight());
newView.toFront();
}
} else {
System.out.println("Error: key not found in map");
}
}
}
I want to the right view to fit the container.

The Pane you are using for the container for the right side of the display performs no layout, so it cannot make the content fill the available space (no matter what settings you use).
Use a BorderPane for the container instead:
<BorderPane
fx:id="container"
GridPane.hgrow="ALWAYS"
GridPane.vgrow="ALWAYS"
GridPane.columnIndex="1"
GridPane.columnSpan="1"
GridPane.rowIndex="0">
</BorderPane>
and then in the controller you can simply set the new content as the center each time:
public class Controller implements Initializable {
#FXML private BorderPane container;
// ...
public void load(Views view){
if (views.containsKey(view)) {
Node newView = views.get(view);
container.setCenter(newView);
} else {
System.out.println("Error: key not found in map");
}
}
}

Related

Adding item to TableView from other stage

I'm newbie in Java FXML. I want to add an item to the TableView in first stage(tableview.fxml) from form I've created in second stage(dodawanie.fxml). Can someone tell me how to do this?
Calling the method AddPersonToTable from DodawanieController causes that ObservableList data is updated, but the TableView not.
TableViewController.java
public class TableViewController {
#FXML public TableView<Person> tableView;
#FXML private TextField firstNameField;
#FXML private TextField lastNameField;
#FXML private TextField emailField;
#FXML
protected void addPerson(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Dodawanie.fxml"));
Stage stage = new Stage();
stage.setTitle("Dodaj ksiazke");
stage.setScene(new Scene(root));
stage.show();
}
public void updateTable(Person person){
ObservableList<Person> data = tableView.getItems();
data.add(person);
tableView.setItems(data);
}
}
DodawanieController.java
public class DodawanieController {
#FXML
private TextField firstNameField;
#FXML
private TextField lastNameField;
#FXML
private TextField emailField;
#FXML
public void AddPersonToTable(ActionEvent actionEvent) throws IOException {
Person person = new Person(firstNameField.getText(), lastNameField.getText(), emailField.getText());
System.out.println(person);
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("tableview.fxml"));
loader.load();
TableViewController tableViewController = loader.getController();
tableViewController.updateTable(person);
}
}
tableview.fxml
<GridPane alignment="CENTER" hgap="10.0" vgap="10.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.TableViewController">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<Label style="-fx-font: NORMAL 20 Tahoma;" text="Address Book" GridPane.columnIndex="0" GridPane.rowIndex="0">
</Label>
<TableView fx:id="tableView" GridPane.columnIndex="0" GridPane.rowIndex="1">
<columns>
<TableColumn text="First Name">
<cellValueFactory><PropertyValueFactory property="firstName" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Last Name">
<cellValueFactory><PropertyValueFactory property="lastName" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Email Address">
<cellValueFactory><PropertyValueFactory property="email" />
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<Person email="jacob.smith#example.com" firstName="Jacob" lastName="Smith" />
<Person email="isabella.johnson#example.com" firstName="Isabella" lastName="Johnson" />
<Person email="ethan.williams#example.com" firstName="Ethan" lastName="Williams" />
<Person email="emma.jones#example.com" firstName="Emma" lastName="Jones" />
<Person email="michael.brown#example.com" firstName="Michael" lastName="Brown" />
</FXCollections>
</items>
</TableView>
<Button alignment="CENTER_RIGHT" onAction="#addPerson" text="Add" GridPane.rowIndex="2" />
<HBox prefHeight="100.0" prefWidth="200.0" GridPane.rowIndex="3">
<children>
<TextField fx:id="firstNameField" />
<TextField fx:id="lastNameField" />
<TextField fx:id="emailField" />
</children>
</HBox>
<Button onAction="#addPerson2" mnemonicParsing="false" text="Dodaj" GridPane.rowIndex="4" />
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
</rowConstraints>
</GridPane>
Dodawanie.fxml
<GridPane alignment="CENTER" hgap="10.0" prefHeight="400" prefWidth="600" vgap="10.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.DodawanieController">
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<VBox prefHeight="200.0" prefWidth="100.0" spacing="10">
<children>
<Label text="First Name" />
<Label text="Last Name" />
<Label text="Email" />
</children>
</VBox>
<VBox prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1" spacing="10">
<children>
<TextField fx:id="firstNameField" />
<TextField fx:id="lastNameField" />
<TextField fx:id="emailField" />
<Button mnemonicParsing="false" text="Button" onAction="#AddPersonToTable"/>
</children>
</VBox>
</children>
</GridPane>

Why doesn't JavaFX TextField listener work?

I want to add a listener to a textField in a window that appears when I press a button on a previous window. The problem is that the listener doesn't work at all. It doesn't detect any change.
public class WindowTwoController {
private Stage stage;
#FXML
private TextField imieTF = new TextField();
public void show() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("winTwo.fxml"));
Parent root = loader.load();
stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("Sell Art");
stage.setScene(new Scene(root, 500, 500));
imieTF.textProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("textfield changed from " + oldValue + " to " + newValue);
});
stage.showAndWait();
}
I'm changing the value of the textField, but nothing is printed out to the console. The show method is called when a button is pressed on the previous window. Please help me. This is my winTwo.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<GridPane alignment="center" hgap="10" prefHeight="441.0" prefWidth="500.0" vgap="10" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="windowTwo.WindowTwoController">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<VBox prefHeight="354.0" prefWidth="455.0">
<children>
<Label alignment="CENTER" prefHeight="45.0" prefWidth="457.0" text="Sprzedaż dzieła">
<font>
<Font size="30.0" />
</font>
</Label>
<Separator prefWidth="200.0" />
<GridPane prefHeight="139.0" prefWidth="455.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label alignment="CENTER" prefHeight="40.0" prefWidth="220.0" text="Imię">
<font>
<Font size="28.0" />
</font>
</Label>
<Label alignment="CENTER" prefHeight="40.0" prefWidth="220.0" text="Nazwisko" GridPane.rowIndex="1">
<font>
<Font size="28.0" />
</font>
</Label>
<TextField fx:id="imieTF" GridPane.columnIndex="1">
<GridPane.margin>
<Insets right="20.0" />
</GridPane.margin></TextField>
<TextField fx:id="nazwiskoTF" GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets right="20.0" />
</GridPane.margin>
</TextField>
</children>
</GridPane>
<Separator prefWidth="200.0" />
<Label alignment="CENTER" prefHeight="17.0" prefWidth="450.0" text="Klient powracający">
<font>
<Font size="22.0" />
</font>
</Label>
<Separator prefWidth="200.0" />
<GridPane prefHeight="126.0" prefWidth="455.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label alignment="CENTER" prefHeight="40.0" prefWidth="220.0" text="Poziom Zaprzyjaźnienia" textFill="#00000080">
<font>
<Font size="19.0" />
</font>
</Label>
<ComboBox fx:id="cb" opacity="0.5" prefHeight="25.0" prefWidth="207.0" GridPane.columnIndex="1" />
<Button mnemonicParsing="false" prefHeight="25.0" prefWidth="175.0" text="Akceptuj" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="25.0" />
</GridPane.margin>
</Button>
<Button mnemonicParsing="false" prefHeight="25.0" prefWidth="175.0" text="Anuluj" GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="27.0" />
</GridPane.margin>
</Button>
</children>
</GridPane>
</children>
</VBox>
</children>
</GridPane>
You should add an anotation to the private member imieTF like so
public class WindowTwoController {
#FXML
private TextField imieTF;
#FXML
private void initialize() {
imieTF.textProperty().addListener((observable, oldValue, newValue) ->
{
System.out.println("textfield changed from " + oldValue + " to " + newValue);
});
}
public static void show() {
FXMLLoader loader = new FXMLLoader(getClass().getResource("winTwo.fxml"));
Parent root = loader.load();
stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("Sell Art");
stage.setScene(new Scene(root, 500, 500));
stage.showAndWait();
}
}
This should bind the TextField instance to the controller. But from what I can find out the FXML will create a new instance of WindowTwoController when the window belonging to the fxml file gets created.
Also see https://www.callicoder.com/javafx-fxml-form-gui-tutorial/ for a basic example on how this works.
Note that all manipulations of the textfield should be part of the JavaFX flow and cannot be done in a manual instance of WindowTwoController. Keep in mind that JavaFX will create it's own instance of WindowTwoController during the loader.load(); operation.

JavaFX MVC what should be handled by the controller and what by the main application?

I've recently made a java swing application to play oxo and I'm trying to make it again in javaFX using the MVC design pattern. I'm strugling as to what should be handled in which part of my application.
How it looked in swing (see picture link)
The design in javaFX using fxml (see picture link)
The folder structure is (see picture link)
To make this abstract question more concrete: I handle the click in my controller but what do I do with it then? Do I handle my game logic there? Or should I just determine the source and send it up to my main app to do the rest?
http://imgur.com/a/z3dGL
Main:
package be.vincent_nagy.oxo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("oxo.fxml"));
Parent root = loader.load();
Scene mainScene = new Scene(root,600,600);
primaryStage.setTitle("OXO Spel");
primaryStage.setScene(mainScene);
primaryStage.show();
//Hier geef ik de controller toegang tot de main app.
OxoController controller = loader.getController();
controller.setMain(this);
}
public static void main(String[] args) {
launch(args);
}
}
Controller:
package be.vincent_nagy.oxo;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
public class OxoController {
#FXML
private GridPane mainGrid;
#FXML
private Button button5;
#FXML
private Button button3;
#FXML
private Button button2;
#FXML
private Button button4;
#FXML
private Button button6;
#FXML
private Button button7;
#FXML
private Button button8;
#FXML
private Button button9;
#FXML
private Button button1;
//Referentie naar de main applicatie
private Main main;
//Constructor wordt opgeroepen voor de initialize() methode
public OxoController(){
}
// tot de #FXML properties van deze controller
#FXML
private void initialize(){
}
//Referentie naar de main applicatie. Aanroeping vanuit main
public void setMain(Main main){
this.main = main;
}
#FXML
void handleButtonAction(ActionEvent event) {
if(event.getSource() instanceof Button){
Button src = (Button) event.getSource();
//send the src up to main or handle the game code here?
}
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane fx:id="mainGrid" alignment="center" gridLinesVisible="true" minHeight="600.0" minWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="be.vincent_nagy.oxo.OxoController">
<rowConstraints>
<RowConstraints maxHeight="200.0" minHeight="200.0" prefHeight="200.0" />
<RowConstraints maxHeight="200.0" minHeight="200.0" prefHeight="200.0" />
<RowConstraints maxHeight="200.0" minHeight="200.0" prefHeight="200.0" />
</rowConstraints>
<columnConstraints>
<ColumnConstraints maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
<ColumnConstraints maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
<ColumnConstraints maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
</columnConstraints>
<children>
<Button fx:id="button5" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Button fx:id="button3" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" />
<Button fx:id="button2" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" />
<Button fx:id="button4" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1" />
<Button fx:id="button6" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<Button fx:id="button7" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="2" />
<Button fx:id="button8" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Button fx:id="button9" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="2" />
<Button fx:id="button1" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" />
</children>
</GridPane>
https://pastebin.com/dgfQJiEK

Is it possible to create an array of Controllers in JavaFX?

I've had a good search and either I'm not using the right terminology or this question hasn't appeared yet. I've been coding in Java for a few years and I've just started playing with Java FX over the last few weeks.
Context:
I'm trying to create an app that is effectively a quiz, but instead of having a new Pane/Tab for each question, I'd like to have them appear side by side.
I have created a questionController for a single question, but I'm unsure of the best way of creating many questionControllers that are all uniquely identifiable by the fx:id's.
I'd like my GUI to be scaleable so the user can select the number of questions displayed per round. I can't seem to work out how to do this without manually setting creating views with different numbers of questionControllers.
Things I've tried
Adding controller gridPane,
Importing the controller in the FXML
But this aren't scaleable as my buttons just become button0 - buttonN
If you require any more information, please let me know.
Question Controller.java
public class QuestionController implements Initializable {
#FXML
private ComboBox<String> chordCombo;
#FXML
private Label questionField;
#FXML
private Button submitButton;
#FXML
private ToggleGroup answerGroup;
#FXML
private RadioButton toggle0, toggle1, toggle2, toggle3, toggle4;
#Override
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
submitButton.setOnAction((event) -> {
System.out.println("Submit Logic");
});
answerGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
public void changed(ObservableValue<? extends Toggle> ov, Toggle old_toggle, Toggle new_toggle) {
if (answerGroup.getSelectedToggle() != null) {
System.out.println(answerGroup.getSelectedToggle().toString());
}
}
});
}
QuestionController.fxml
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="235.0" prefWidth="203.0" styleClass="layout" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="uk.co.temp.QuestionController">
<children>
<BorderPane layoutX="-11.0" layoutY="35.0" prefHeight="235.0" prefWidth="203.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<bottom>
<Button fx:id="submitButton" mnemonicParsing="false" text="Submit" BorderPane.alignment="CENTER">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<BorderPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</BorderPane.margin>
</Button>
</bottom>
<top>
<Label fx:id="questionField" text="Insert Question Here" BorderPane.alignment="CENTER">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<BorderPane.margin>
<Insets left="10.0" right="10.0" top="10.0" />
</BorderPane.margin>
</Label>
</top>
<center>
<GridPane BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<RadioButton fx:id="toggle0" mnemonicParsing="false" text="Answer A">
<toggleGroup>
<ToggleGroup fx:id="answerGroup" />
</toggleGroup></RadioButton>
<RadioButton fx:id="toggle1" mnemonicParsing="false" text="Answer B" toggleGroup="$answerGroup" GridPane.rowIndex="1" />
<RadioButton fx:id="toggle2" mnemonicParsing="false" text="Answer C" toggleGroup="$answerGroup" GridPane.rowIndex="2" />
<RadioButton fx:id="toggle3" mnemonicParsing="false" text="Answer D" toggleGroup="$answerGroup" GridPane.rowIndex="3" />
<RadioButton fx:id="toggle4" mnemonicParsing="false" text="Answer E" toggleGroup="$answerGroup" GridPane.rowIndex="4" />
</children>
<padding>
<Insets top="10.0" />
</padding>
</GridPane>
</center>
</BorderPane>
</children>
</AnchorPane>
I think you are looking for something like this:
TilePane pane = new TilePane(); // or whatever you are putting the questions in...
int numQuestions = ... ;
QuestionController[] controllers = new QuestionController[numQuestions];
for (int questionNumber = 0 ; questionNumber < numQuestions ; questionNumber++) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("path/to/QuestionController.fxml"));
pane.getChildren().add(loader.load());
controllers[questionNumber] = loader.getController();
}
Obviously if the TilePane (or whatever) is defined in another FXML file and injected into another controller, you can put the same code in an event handler or initialize method, as needed, in that controller.

javafx tabpane's pane content not working and tab not switching

Hi i have made a javafx app with loading New Tabs inside TabPane.Application running great except the New Tabs Button,TextFields are not working and also the switching of in tabpane is not working.
FXML
<GridPane id="content" alignment="CENTER" prefHeight="310.0" prefWidth="800.0" styleClass="mainParent" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.gvj.sndp.view.controller.HomeController">
<children>
<SplitPane dividerPositions="0.18421052631578946" prefHeight="160.0" prefWidth="200.0" GridPane.rowIndex="2">
<items>
<TreeView fx:id="menuTreeView" prefHeight="273.0" prefWidth="115.0" />
<TabPane fx:id="tabPane" mouseTransparent="true" prefHeight="200.0" prefWidth="200.0" rotateGraphic="true" />
</items>
</SplitPane>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" GridPane.rowIndex="1">
<children>
<JFXButton buttonType="RAISED" onAction="#addMemberSelect" prefHeight="33.0" prefWidth="37.0" text="AM">
<opaqueInsets>
<Insets />
</opaqueInsets>
<HBox.margin>
<Insets left="5.0" />
</HBox.margin>
</JFXButton>
<JFXButton buttonType="RAISED" onAction="#addSelfHelpSelect" prefHeight="33.0" prefWidth="55.0" text="ASH">
<HBox.margin>
<Insets left="5.0" />
</HBox.margin>
</JFXButton>
<JFXButton onAction="#addOfficeBarrierSelect" prefHeight="35.0" prefWidth="43.0" text="AOB">
<HBox.margin>
<Insets left="5.0" />
</HBox.margin>
</JFXButton>
</children>
</HBox>
<HBox />
</children>
<columnConstraints>
<ColumnConstraints hgrow="ALWAYS" minWidth="-1.0" prefWidth="-1.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="35.0" minHeight="22.0" prefHeight="30.0" valignment="CENTER" vgrow="ALWAYS" />
<RowConstraints maxHeight="35.0" minHeight="22.0" prefHeight="22.0" valignment="CENTER" vgrow="ALWAYS" />
<RowConstraints valignment="CENTER" vgrow="ALWAYS" />
</rowConstraints>
</GridPane>
When the user click the button a new tab is create and loaded to the TabPane.
Why isn't not working.What i'm doing wrong??
Controller
#FXML
private TabPane tabPane;
#FXML
private void addMemberSelect() {
App.getInstance().showProgressIndicator(myController);
Task<FXMLLoader> task = new Task<FXMLLoader>() {
#Override
protected FXMLLoader call() throws Exception {
return new FXMLLoader(App.class.getResource(Screens.ADD_MEMBER));
}
#Override
protected void succeeded() {
super.succeeded();
Tab tabA = new Tab("Add Member");
final ScrollPane scrollPane = new ScrollPane();
scrollPane.setFitToHeight(true);
scrollPane.setFitToWidth(true);
try {
scrollPane.setContent(get().load());
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
tabA.setContent(scrollPane);
tabPane.getTabs().add(tabA);
App.getInstance().hideProgressIndicator(myController);
}
#Override
protected void failed() {
super.failed();
App.getInstance().hideProgressIndicator(myController);
}
};
new Thread(task).start();
}

Categories