ListView did not show items in app. Why not? - javafx

I got items on my listview roles but they are not showing in my program after running in window. Gui done in Scene Builder. When i run program field where should be items is white blank just empty.
printscreen of program
https://zapodaj.net/c60f99a10f300.jpg.html
scene builder
https://zapodaj.net/26fbf2123397c.jpg.html
When i use show sample data in Scene Builder it shows lorem ipsum etc.
here u got code from controller and fmxl document.
package sample;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import java.net.URL;
import java.util.*;
public class ControllerBindColors implements Initializable {
#FXML
private ListView<String > roles;
#FXML
private ListView<String> colors;
#FXML
private ListView<?> binded;
String selectedRole;
String selectedColor;
int numberOfPlayers;
#Override
public void initialize(URL location, ResourceBundle resources) {
}
public void initData(int numberOfPlayers, String selectedRole, String selectedColor) {
this.numberOfPlayers = numberOfPlayers;
this.selectedRole = selectedRole;
this.selectedColor = selectedColor;
ObservableList<String> itemsRoles = FXCollections.observableArrayList("test","test2","test3");
roles = new ListView<>();
roles.setItems(itemsRoles);
roles.getItems().addAll("test555");
roles.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
System.out.println(roles.getItems());
}
}
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.ListView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox spacing="10.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.ControllerBindColors">
<children>
<HBox alignment="CENTER">
<children>
<ListView fx:id="roles" prefHeight="200.0" prefWidth="200.0" />
<VBox alignment="CENTER" spacing="10.0">
<children>
<Button fx:id="backBtn" ellipsisString="BACK" mnemonicParsing="false" onAction="#back" text="BACK" />
<Label prefHeight="51.0" prefWidth="71.0" text="Select role and color then bind." textAlignment="CENTER" wrapText="true" />
<Button fx:id="bindbtn" ellipsisString="BIND" mnemonicParsing="false" onAction="#bind" text="BIND" />
<Button fx:id="nextBtn" ellipsisString="NEXT" mnemonicParsing="false" onAction="#next" text="NEXT" />
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
</VBox>
<ListView fx:id="colors" prefHeight="200.0" prefWidth="200.0" />
</children>
</HBox>
<ListView fx:id="binded" prefHeight="200.0" prefWidth="200.0" />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</VBox>
initData
ControllerNewGame.java startGame() is called when is activaded button
#FXML
void startGame() {
try{
Parent root= FXMLLoader.load(getClass().getResource("BindColor.fxml"));
ControllerBindColors controller = new ControllerBindColors();
int number = (int)slider.getValue();
controller.initData(number,selectedRole.getText(),selectedColor.getText());
Stage toGame = (Stage)startButton.getScene().getWindow();
toGame.setTitle("Parasite Pip-Boy Bind Color Menu");
toGame.setScene(new Scene(root, 600, 300));
toGame.show();
}catch(IOException e) {
startButton.setText("Reinstall program. BindColor.fxml is missing or is corrupted. :(");
}
}
list has items for sure in console there are printed all 4 items
[test, test2, test3, test555]

To get a reference to the controller used by the FXML (instead of constructing a new one):
FXMLLoader loader = new FXMLLoader();
Parent root= loader.load(getClass().getResource("BindColor.fxml").openStream());
ControllerBindColors controller = fxmlLoader.getController();
after getting a reference to it, you can use it as you did:
controller.initData(number,selectedRole.getText(),selectedColor.getText());

Related

Is there some form of priority or functionality issue when loading new FXML files into a Scene? (adopting framework provided by jewelsea)

I've realised only through clearing up code that I thought I could clear up and had cleared from some of my Controller classes where my issue is. To word my question better and provide more clarity to the concept of prioritisation I'll give a brief explanation followed by some code. Imagine an interface with a side of the screen being consistent and permanent to the interface where you have Buttons for menu-ing where you essentially move between tabs changing the rest of the Stage passing fxml files to the Pane associated.
Reading examples and questions I understood this to be viable and using the framework from this link https://gist.github.com/jewelsea/6460130 seeing a header remain consistent while switching Scene between vista1.fxml and vista2.fxml, my understanding was that if the Header remains from main.fxml as part of the scene that the Stage is therefore using either both main.fxml and vista1.fxml or main.fxml and vista2.fxml concurrently and thus both associated controllers exist and will have access to corresponding methods according to the Pane interacted with.
That is clearly not the case though and the consistent Pane in this case main.fxml does not have its controller since when the other is loaded and creates its controller it replaces or clears the other controller in some form or another I now presume is the case. I thought I could avoid code duplication by using separate Panes with the consistency of the menu bar but also being able to use its buttons.
Some code as an example:
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception{
stage.setTitle("Vista Viewer");
stage.setScene(createScene(loadMainPane()));
stage.show();
}
private Pane loadMainPane() throws IOException {
FXMLLoader loader = new FXMLLoader();
Pane mainPane = (Pane) loader.load(getClass().getResourceAsStream(
VistaNavigator.MAIN));
MainController mainController = loader.getController();
VistaNavigator.setMainController(mainController);
VistaNavigator.loadVista(VistaNavigator.VISTA_1);
return mainPane;
}
private Scene createScene(Pane mainPane) {
Scene scene = new Scene(mainPane);
return scene;
}
public static void main(String[] args) {
launch(args);
}
}
VistaNavigator.java
package sample;
import javafx.fxml.FXMLLoader;
import java.io.IOException;
public class VistaNavigator {
public static final String MAIN = "main.fxml";
public static final String VISTA_1 = "vista1.fxml";
public static final String VISTA_2 = "vista2.fxml";
public static final String OTHER_MENU_OPTION = "otherMenu.fxml";
private static MainController mainController;
public static void setMainController(MainController mainController) {
VistaNavigator.mainController = mainController;
}
public static void loadVista(String fxml) {
try {
mainController.setVista(
FXMLLoader.load(
VistaNavigator.class.getResource(
fxml
)
)
);
} catch (IOException e) {
e.printStackTrace();
}
}
}
MainController.java
package sample;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.StackPane;
public class MainController {
#FXML
private StackPane vistaHolder;
public void setVista(Node node) {
vistaHolder.getChildren().setAll(node);
}
public void homebtn() throws Exception{
VistaNavigator.loadVista(VistaNavigator.VISTA_1);
}
public void otherMenuOption() throws Exception{
VistaNavigator.loadVista(VistaNavigator.OTHER_MENU_OPTION);
}
}
main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="200.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.MainController">
<children>
<VBox prefWidth="155.0">
<children>
<Label fx:id="headerLabel" maxWidth="120.0" text="Header" />
</children>
</VBox>
<Pane layoutY="40.0" prefHeight="200.0" prefWidth="120.0" style="-fx-background-color: #091D34;">
<children>
<Button fx:id="button1" layoutY="60.0" mnemonicParsing="false" onAction="#homebtn" prefHeight="44.0" prefWidth="120.0" style="-fx-background-color: #133863;" text="Home" textFill="WHITE">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
<Button fx:id="button2" layoutY="110.0" mnemonicParsing="false" onAction="#otherMenuOption" prefHeight="44.0" prefWidth="120.0" style="-fx-background-color: #133863;" text="OtherMenuOption" textFill="WHITE">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
</children>
</Pane>
<StackPane fx:id="vistaHolder" VBox.vgrow="ALWAYS" />
</children>
</AnchorPane>
otherMenu.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<StackPane fx:id="otherMenuOption" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Vista1Controller">
<children>
<Label fx:id="label" layoutX="135.0" layoutY="57.0" maxWidth="120.0" text="other program stuff" VBox.vgrow="NEVER" />
</children>
</StackPane>
Vista1Controller.java
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
public class Vista1Controller {
#FXML
void nextPane(ActionEvent event) {
VistaNavigator.loadVista(VistaNavigator.VISTA_2);
}
}
vista1.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Vista1Controller" prefHeight="400.0" prefWidth="600.0">
<children>
<Pane fx:id="Vista1" layoutX="155.0" layoutY="-1.0" prefHeight="430.0" prefWidth="574.0" style="-fx-background-color: transparent">
<Button fx:id="btn" layoutX="135.0" layoutY="57.0" mnemonicParsing="false" onAction="#nextPane" prefHeight="149.0" prefWidth="318.0" style="-fx-background-color: #133863;" text="Open Vista2" textFill="#ffffff">
<font>
<Font name="Britannic Bold" size="20.0" />
</font>
</Button>
</Pane>
</children>
</AnchorPane>
Vista2Controller.java
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
public class Vista2Controller {
#FXML
void previousPane(ActionEvent event) {
VistaNavigator.loadVista(VistaNavigator.VISTA_1);
}
}
vista2.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Vista2Controller" prefHeight="400.0" prefWidth="600.0">
<children>
<Pane fx:id="Vista2" layoutX="155.0" layoutY="-1.0" prefHeight="430.0" prefWidth="574.0" style="-fx-background-color: transparent">
<Button fx:id="btn" layoutX="135.0" layoutY="57.0" mnemonicParsing="false" onAction="#previousPane" prefHeight="149.0" prefWidth="318.0" style="-fx-background-color: #133863;" text="Open Vista1" textFill="#ffffff">
<font>
<Font name="Britannic Bold" size="20.0" />
</font>
</Button>
</Pane>
</children>
</AnchorPane>
Is there a good way to maintain this design and prevent code duplication within the specific controllers and would it be through inheritance or passing parameters, I'm not sure what this is comparable to nor whether it's possible?

JFXDatePicker not working when program is exported to a jar file

I have a application which uses two JFXDatePickers from JFoenix. They work perfectly when running from Eclipse but do not work when I export the program to a JAR file. They still return the text that is set by default but when I try to select a date using it the window freezes and the application does not respond.
I've searched trough stackoverflow and javafx and jfoenix documentation but can't seem to find an answer. Can someone please help me? This is for a school project. Any suggestions?
#FXML
private JFXDatePicker startDatePicker;
#FXML
private JFXDatePicker endDatePicker;
#FXML
private void initialize() {
this.endDatePicker.setValue(LocalDate.now());
this.startDatePicker.setValue(LocalDate.now().withDayOfMonth(1));
}
Thank you very much!
Edit: Thank you for the suggestion, Slaw. I've launched it trough the command line and didn't get any exceptions. The controller class where the error happens is this one:
package application;
import java.time.LocalDate;
import com.jfoenix.controls.JFXDatePicker;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
public class SeeAllQuotesFilterScreenController {
#FXML
private Button cancelButton;
#FXML
private Button enterButton;
#FXML
private JFXDatePicker startDatePicker;
#FXML
private JFXDatePicker endDatePicker;
#FXML
private void initialize() {
this.endDatePicker.setValue(LocalDate.now());
this.startDatePicker.setValue(LocalDate.now().withDayOfMonth(1));
}
#FXML
void cancelButtonClicked(ActionEvent event) {
new Alert(Alert.AlertType.INFORMATION, "changes to a different screen on the complete application").showAndWait();
}
#FXML
void enterButtonClicked(ActionEvent event) {
new Alert(Alert.AlertType.INFORMATION, "changes to a different screen on the complete application").showAndWait();
}
}
Also, the fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXDatePicker?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<BorderPane style="-fx-background-color: #E2FAFE;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SeeAllQuotesFilterScreenController">
<center>
<BorderPane prefHeight="500.0" prefWidth="500.0" BorderPane.alignment="CENTER">
<center>
<VBox alignment="TOP_CENTER">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Please choose the search criteria below.">
<VBox.margin>
<Insets left="5.0" right="5.0" top="5.0" />
</VBox.margin>
</Text>
<HBox alignment="CENTER">
<children>
<JFXDatePicker fx:id="startDatePicker" promptText="Start Date">
<HBox.margin>
<Insets bottom="12.0" left="12.0" right="12.0" top="12.0" />
</HBox.margin>
</JFXDatePicker>
<JFXDatePicker fx:id="endDatePicker" promptText="End Date">
<HBox.margin>
<Insets bottom="12.0" left="12.0" right="12.0" top="12.0" />
</HBox.margin>
</JFXDatePicker>
</children>
<padding>
<Insets bottom="40.0" top="40.0" />
</padding>
</HBox>
<JFXButton fx:id="enterButton" onAction="#enterButtonClicked" text="Enter">
<VBox.margin>
<Insets bottom="20.0" left="20.0" right="20.0" />
</VBox.margin></JFXButton>
</children>
<padding>
<Insets bottom="3.0" left="5.0" right="3.0" top="30.0" />
</padding>
</VBox>
</center>
</BorderPane>
</center>
<top>
<HBox alignment="CENTER" prefHeight="37.0" prefWidth="500.0" BorderPane.alignment="CENTER">
<children>
<HBox alignment="CENTER_LEFT" HBox.hgrow="NEVER">
<children>
<JFXButton fx:id="cancelButton" onAction="#cancelButtonClicked" prefHeight="25.0" prefWidth="54.0" text="Cancel" />
</children>
</HBox>
<HBox>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Search Criteria">
<font>
<Font size="18.0" />
</font>
<HBox.margin>
<Insets bottom="12.0" left="12.0" right="12.0" top="12.0" />
</HBox.margin>
</Text>
</children>
</HBox>
</children>
</HBox>
</top>
</BorderPane>
My Main():
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane) FXMLLoader.load(getClass().getResource("SeeAllQuotesFilterScreen.fxml"));
Scene scene = new Scene(root, 500, 600);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public void initialize() {
}
public static void main(String[] args) {
launch(args);
}
}
I appreciate all and any help!

Pass data from one controller to another before close stage JavaFX

I have a JavaFx project with two stages. The main stage has the ability to launch second stage .In second stage I have a textfield I want to put some data into textfield and send it to the main stage and set it as a label .Here is my code:
MAIN:
package sample;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/sample/sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
MAIN CONTROLLER:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
#FXML
private javafx.scene.control.Button kbutton;
#FXML
private javafx.scene.control.Button obutton;
#FXML
public Label label;
public TextField popField;
#FXML
public String text;
#FXML
private void konfButton() throws Exception{
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample/konfview.fxml"));
Parent root = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.showAndWait();
} catch(Exception e) {
e.printStackTrace();
}
}
public void myFunction( String text){
popField.setText(text);
}
#FXML
private void oblButton() throws Exception{
try {
// get a handle to the stage
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample/sample.fxml"));
Parent root = (Parent) fxmlLoader.load();
Controller Controller=fxmlLoader.getController();
Stage stage = (Stage) obutton.getScene().getWindow();
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
MAIN FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints />
</rowConstraints>
<children>
<Pane prefHeight="400.0" prefWidth="600.0">
<children>
<Label layoutX="69.0" layoutY="81.0" text="previous reading" />
<Label layoutX="69.0" layoutY="115.0" text="current reading" />
<Label layoutX="71.0" layoutY="154.0" text="months" />
<Label layoutX="108.0" layoutY="14.0" text="Price">
<font>
<Font name="Comic Sans MS" size="18.0" />
</font>
</Label>
<Label layoutX="97.0" layoutY="51.0" text="Give details">
<font>
<Font name="Comic Sans MS" size="12.0" />
</font>
</Label>
<Button fx:id="kbutton" layoutX="74.0" layoutY="197.0" mnemonicParsing="false" onAction="#konfButton" text="Configure" />
<Button fx:id="obutton" layoutX="197.0" layoutY="197.0" mnemonicParsing="false" onAction="#oblButton" text="OK" />
<TextField fx:id="popField" layoutX="172.0" layoutY="77.0" />
<TextField fx:id="bieField" layoutX="172.0" layoutY="111.0" />
<TextField fx:id="mcField" layoutX="172.0" layoutY="150.0" />
<Label fx:id="label" layoutX="119.0" layoutY="245.0" prefHeight="37.0" prefWidth="333.0" text="text" />
</children>
</Pane>
</children>
</GridPane>
SECOND CONTROLLER:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.io.IOException;
public class ControllerConf {
#FXML
public TextField palField;
public Label label;
public String text;
#FXML
private TextField dysField1;
#FXML
private javafx.scene.control.Button cancelB;
#FXML
public javafx.scene.control.Button saveB;
#FXML
private void cancelButton(){
// get a handle to the stage
Stage stage = (Stage) cancelB.getScene().getWindow();
// do what you have to do
stage.close();
}
#FXML
public Stage saveButton(){
try {
FXMLLoader loader=new
FXMLLoader(getClass().getResource("/sample/sample.fxml"));
Parent root = (Parent) loader.load();
Controller Controller=loader.getController();
Controller.myFunction(palField.getText());
Stage primaryStage = (Stage) saveB.getScene().getWindow();
primaryStage.close();
return primaryStage;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
SECOND FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.ControllerConf">
<children>
<Label layoutX="185.0" layoutY="28.0" prefHeight="17.0" prefWidth="314.0" text="Give details">
<font>
<Font name="Comic Sans MS" size="20.0" />
</font>
</Label>
<Label layoutX="48.0" layoutY="95.0" text="Price pre KWh" />
<Label layoutX="48.0" layoutY="135.0" text="Price pre item" />
<TextField fx:id="palField" layoutX="252.0" layoutY="91.0" onAction="#saveButton" />
<TextField fx:id="dysField" layoutX="252.0" layoutY="131.0" />
<Button fx:id="cancelB" layoutX="159.0" layoutY="208.0" mnemonicParsing="false" onAction="#cancelButton" text="Cancel">
<font>
<Font name="Comic Sans MS" size="12.0" />
</font>
</Button>
<Button fx:id="saveB" layoutX="292.0" layoutY="209.0" mnemonicParsing="false" onAction="#saveButton" text="Save">
<font>
<Font name="Comic Sans MS" size="12.0" />
</font>
</Button>
</children>
</AnchorPane>
I created MyFunction wchich set text and it's working fine when created new stage but doesnt for main stage.Do you know how to solve it?Thanks in advance.enter code here

Javafx How can I hide/unhide tabs on a tabpane

I have tabpane and depending upon what is going on I'd like to make certain tabs invisible and others visible. The scene and tabs are all defined in FXML.
I know I can use:
tabPane.getTabs().add(0, tab1);
and
tabPane.getTabs().remove(tab1);
but all my tabs, including tab1 are defined in FXML. If I could get and save all the defined tabs and save them so I could re-add them later that would be OK.
Your solution above is fine. Just make sure that in your FXML you give an ID to each tab you want to manipulate.
<Tab fx:id="myTab1">....</Tab>
For this example I used the closing policy of the tab control to remove tabs from the scene. But this should give you enough information to start with. Here is a working solution that might point you in the right direction:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="250.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.TestController">
<children>
<TabPane fx:id="tabPane" layoutX="14.0" layoutY="14.0" prefHeight="294.0" prefWidth="446.0" AnchorPane.bottomAnchor="30.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab fx:id="tab1" text="Tab 1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab fx:id="tab2" text="Tab 2">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab fx:id="tab3" text="Tab 3">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab fx:id="tab4" text="Tab 4">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
<ButtonBar layoutX="138.0" layoutY="216.0" prefHeight="40.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0">
<buttons>
<Button mnemonicParsing="false" onAction="#saveTabs" text="Save" />
<Button mnemonicParsing="false" onAction="#openTabs" text="Open" />
</buttons>
</ButtonBar>
</children>
</AnchorPane>
Main Class:
package application;
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) {
try {
FXMLLoader loader = new FXMLLoader();
Parent node = loader.load(Main.class.getClassLoader().getResource("fxml/TestFXML.fxml").openStream());
Scene scene = new Scene(node,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Controller:
package application;
import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;
import javafx.fxml.FXML;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
public class TestController {
#FXML
private TabPane tabPane;
#FXML
private Tab tab1, tab2, tab3, tab4;
Collection<Tab> tabs = new ArrayList<>();
Collection<String> openTabIds = new ArrayList<>();
#FXML
private void initialize() {
tabs.add(tab1);
tabs.add(tab2);
tabs.add(tab3);
tabs.add(tab4);
}
#FXML
void openTabs() {
openTabIds.stream().forEach(string -> {
tabs.stream()
.filter(tab -> tab.getId().equals(string)).findFirst()
.ifPresent(tab -> tabPane.getTabs().add(tab));
});
}
#FXML
void saveTabs() {
openTabIds = tabPane.getTabs().stream().map(tab -> tab.getId()).collect(Collectors.toList());
}
}

Binding listener isn't working properly in JavaFX 8

Currently I am learning how to use Bindings and Binding-Events properly. I already read a chapter of a book about it and in general I have no problem in using Bindings.
For testing my knowledge, I wrote a little JavaFX8 Application. I got 2 TextFields, but at the moment I am focusing on one TextField, called "firstName". I am using a BooleanBinding. Whenever the TextField is getting filled, the BooleanBinding is set to "true". If there's no Input in the Field, the BooleanBinding is set to "false". My goal is to update Label called "statusLabel", whenever the BooleanBinding got changed.
This is how the binding looks:
BooleanBinding nameEntered = firstName.textProperty().isNotEmpty();
This is my ChangeListener:
nameEntered.addListener((o, oldValue, newValue) -> {
statusLabel.setText(newValue.toString());
});
For a short amount of time, the Listener is working properly. When the BooleanBinding got changed, the Label is getting updated. But after some input changes (deleting the input, filling again etc...) the Label isn't getting updated anymore. Any ideas how to fix this?
Here is the full code:
FXMLController:
package gui;
/*
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.binding.StringBinding;
import javafx.beans.binding.When;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
*/
public class LayoutController implements Initializable {
/**
* Initializes the controller class.
*/
#FXML
private TextField firstName;
#FXML
private TextField secondName;
#FXML
private CheckBox checkBox1;
#FXML
private CheckBox checkBox2;
#FXML
private CheckBox checkBox3;
#FXML
private Label statusLabel;
#Override
public void initialize(URL url, ResourceBundle rb) {
BooleanBinding nameEntered = firstName.textProperty().isNotEmpty();
nameEntered.addListener((o, oldValue, newValue) -> {
statusLabel.setText(newValue.toString());
});
}
}
MainView.java:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author xyz
*/
public class MainView extends Application{
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Layout.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("Benutzerauswahl");
primaryStage.show();
}
public static void main(String args[]){
launch(args);
}
}
FXMLLayout:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="310.0" prefWidth="343.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.LayoutController">
<center>
<AnchorPane prefHeight="371.0" prefWidth="380.0" BorderPane.alignment="CENTER">
<children>
<GridPane layoutX="50.0" layoutY="103.0" prefHeight="234.0" prefWidth="281.0" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="49.0" AnchorPane.topAnchor="50.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="155.0" minWidth="10.0" prefWidth="110.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="197.0" minWidth="10.0" prefWidth="171.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="40.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="40.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="400.0" minHeight="10.0" prefHeight="88.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label prefHeight="17.0" prefWidth="56.0" text="Vorname:" GridPane.halignment="CENTER" />
<Label prefHeight="17.0" prefWidth="69.0" text="Nachname:" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
<TextField fx:id="firstName" prefHeight="25.0" prefWidth="144.0" GridPane.columnIndex="1" />
<TextField fx:id="secondName" prefHeight="25.0" prefWidth="144.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2">
<children>
<CheckBox fx:id="checkBox1" layoutX="14.0" layoutY="42.0" mnemonicParsing="false" text="Kurs 1" />
<CheckBox fx:id="checkBox2" layoutX="14.0" layoutY="69.0" mnemonicParsing="false" text="Kurs 2" />
<CheckBox fx:id="checkBox3" layoutX="14.0" layoutY="96.0" mnemonicParsing="false" text="Kurs 3" />
<Label fx:id="statusLabel" layoutX="43.0" layoutY="132.0" prefHeight="17.0" prefWidth="84.0" text="Status" />
</children>
</AnchorPane>
</children>
</GridPane>
</children>
</AnchorPane>
</center>
</BorderPane>
This is really a duplicate of JavaFX Beans Binding suddenly stops working: the problem is that the binding is getting "prematurely garbage collected" because there are no live references to it that are retained. Forcing the reference to be retained in a controller seems to be a little tricky.
First note that if you actually bind a property of a UI element (which is necessarily in scope as long as it is displayed), then the UI element indirectly keeps a reference to the binding. Consequently in your code this will fix the problem:
statusLabel.textProperty().bind(nameEntered.asString());
(instead of the listener you currently have). If you can't actually use a binding, then it seems that first you need to get the controller to retain a reference to the binding:
public class LayoutController implements Initializable {
// existing code...
private BooleanBinding nameEntered ;
public void initialize(URL url, ResourceBundle rb) {
nameEntered = firstName.textProperty().isNotEmpty();
nameEntered.addListener((o, oldValue, newValue) -> {
statusLabel.setText(newValue.toString());
});
}
}
and then additionally you need to force an reference to the controller itself to stay in scope:
public class MainView extends Application{
private LayoutController controller ;
#Override
public void start(Stage primaryStage) throws Exception {
FMXLLoader loader = new FXMLLoader(getClass().getResource("Layout.fxml"));
Parent root = loader.load();
controller = loader.getController();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("Benutzerauswahl");
primaryStage.show();
}
public static void main(String args[]){
launch(args);
}
}
There may be a more obvious way to do this, but I can't find one that works.

Resources