Dynamic change of JavaFX controls from other objects - java

I'm working on a simple javaFX desktop software, it essentially allow a user to control various lights via serial communication.
It's composed of three classes:
he UI Controller (written using javaFX scene builder 2.0 and a custom library for material-like controls, jfoenix)
a "Storage" object with static properties (like which light is on and what color it should display), this class must contain only static properites, so that there are no conflicts with other objects trying to modify some values.
This object is also responsable for the serial communication (using jssc)
A threaded object that controls lights using timers
I must say that i have a working solution built using swing, but i honestly don't like it at all...
Here's the compiled UI: http://imgur.com/esWFsgh
There are two problems:
I want that each button on the top (like "barra 1") displays the status of the corrisponding light, for example, if "barra 1" is on, it should be green, when it's off, it should be red. the problem here is that i have to create an object of the controller class in order to access to all the buttons, but as it should be, in the instantiated object, all the buttons are gone, i should recreate them, and that's obivuosly not the solution... what can i do?
That's simple, I already know that my "listener" is incorrect, but I just can't find the mistake... as soon as I drag any of the sliders, a Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch exception is thrown... (and there's no reference to any of my classes)
Here you can find the code:
Storage class
package com.matteoformenti.rgbcontroller;
import java.awt.*;
/**
* Created by Matteo on 28/12/2015.
*/
public class Storage
{
private static final String ON_COLOR = "#4CAF50";
private static final String OFF_COLOR = "#F44336";
private static Color color;
private static boolean bars[] = {false, false, false, false, false};
private static boolean circles[] = new boolean[2];
private static boolean usePot = false;
//-------------------------------------------------------//
public static boolean getBarValue(int barNumber)
{
return bars[barNumber];
}
public static void setAllBars(boolean value)
{
for(boolean v : bars)
v = value;
setAllBarsColors();
}
public static boolean[] getAllBars()
{
return bars;
}
public static void setCircleValue(int circleNumber, boolean value)
{
circles[circleNumber-1] = value;
}
public static boolean getCircleValue(int circleNumber)
{
return circles[circleNumber-1];
}
public static void setAllCircles(boolean value)
{
for(boolean v : circles)
v = value;
}
public static boolean[] getAllCircles()
{
return circles;
}
public static void setColor(Color c)
{
color = c;
}
public static void setRed(int red)
{
if(red > 255)
red = 255;
if(red < 0)
red = 0;
int g = color.getGreen();
int b = color.getBlue();
color = new Color(red, g, b);
}
public static void setGreen(int green)
{
if(green > 255)
green = 255;
if(green < 0)
green = 0;
int r = color.getRed();
int b = color.getBlue();
color = new Color(r, green, b);
}
public static void setBlue(int blue)
{
if(blue > 255)
blue = 255;
if(blue < 0)
blue = 0;
int g = color.getGreen();
int r = color.getRed();
color = new Color(r, g, blue);
}
public static void setHue(int h)
{
int rgb = Color.HSBtoRGB(h, 100, 100);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
color = new Color(red, green, blue);
}
public static void invertBarValue(int i)
{
bars[i] = !bars[i];
setBarColor(i);
}
public static void invertCircleValue(int i)
{
circles[i] = !circles[i];
}
public static void setBarColor(int i)
{
Controller c = new Controller();
c.test();
/*Controller.bar1.setStyle("-fx-background-color: " + ON_COLOR + ";");
if (bars[i] == true)
Controller.getBarButton(i).setStyle("-fx-background-color: " + ON_COLOR + ";");
if (bars[i] == false)
Controller.getBarButton(i).setStyle("-fx-background-color: " + OFF_COLOR + ";");*/
}
public static void setAllBarsColors()
{/*
for (int i = 0; i < 5; i++)
{
if (bars[i] == true)
Controller.getBarButton(i + 1).setStyle("-fx-background-color: " + ON_COLOR + ";");
if (bars[i] == false)
Controller.getBarButton(i + 1).setStyle("-fx-background-color: " + OFF_COLOR + ";");
}*/
}
}
Controller class:
package com.matteoformenti.rgbcontroller;
import com.jfoenix.controls.JFXButton;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Slider;
import javafx.scene.layout.Background;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import java.awt.*;
public class Controller
{
#FXML
private JFXButton bar1;
#FXML
private JFXButton bar2;
#FXML
private JFXButton bar3;
#FXML
private JFXButton bar4;
#FXML
private JFXButton bar5;
#FXML
private JFXButton circle1;
#FXML
private JFXButton circle2;
#FXML
private Slider rSlider;
#FXML
private Slider gSlider;
#FXML
private Slider bSlider;
#FXML
private Slider aSlider;
#FXML
private JFXButton pot;
#FXML
private JFXButton stroboBars;
#FXML
private Button stroboBarsConfig;
#FXML
private JFXButton stroboCircles;
#FXML
private Button stroboCirclesConfig;
#FXML
private JFXButton fadeColors;
#FXML
private Button fadeColorsConfig;
#FXML
private ColorPicker colorPicker;
#FXML
private Pane frame;
public void test()
{
bar1.setStyle("-fx-background-color: #33b5e5;");
}
#FXML
void bar1Pressed(ActionEvent event)
{
Storage.invertBarValue(0);
}
#FXML
void bar2Pressed(ActionEvent event) {Storage.invertBarValue(1);}
#FXML
void bar3Pressed(ActionEvent event)
{
Storage.invertBarValue(2);
}
#FXML
void bar4Pressed(ActionEvent event) {Storage.invertBarValue(3);}
#FXML
void bar5Pressed(ActionEvent event) {Storage.invertBarValue(4);}
#FXML
void circle1Pressed(ActionEvent event) {
}
#FXML
void circle2Pressed(ActionEvent event) {
}
#FXML
void potPressed(ActionEvent event) {
}
#FXML
void rSliderChanged(ActionEvent event)
{
}
#FXML
void gSliderChanged(ActionEvent event) {
}
#FXML
void bSliderChanged(ActionEvent event) {
}
#FXML
void aSliderChanged(ActionEvent event) {
}
#FXML
void colorPickerChanged(ActionEvent event)
{
Color colorAWT = Utility.javaFXtoAWTColor(colorPicker.getValue());
frame.setStyle("-fx-background-color: #" + colorPicker.getValue().toString().substring(2,8) + ";");
}
#FXML
void stroboBarsPressed(ActionEvent event) {
}
#FXML
void stroboBarsConfigPressed(ActionEvent event) {
}
#FXML
void stroboCirclesPressed(ActionEvent event) {
}
#FXML
void stroboCirclesConfigPressed(ActionEvent event) {
}
#FXML
void fadeColorsPressed(ActionEvent event) {
}
#FXML
void fadeColorsConfigPressed(ActionEvent event) {
}
}
And the fxml layout
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.paint.*?>
<?import com.jfoenix.controls.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<Pane fx:id="frame" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="260.0" prefWidth="620.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.matteoformenti.rgbcontroller.Controller">
<children>
<HBox fx:id="buttonsBox1" layoutY="30.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="40.0" prefWidth="620.0">
<children>
<JFXButton fx:id="bar1" alignment="CENTER" buttonType="RAISED" onAction="#bar1Pressed" ripplerFill="WHITE" style="-fx-background-color: #F44336;" text="Barra 1">
<font>
<Font name="Roboto Light" size="12.0" />
</font>
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="5.0" top="10.0" />
</HBox.margin>
</JFXButton>
<JFXButton fx:id="bar2" alignment="CENTER" buttonType="RAISED" layoutX="20.0" layoutY="20.0" onAction="#bar2Pressed" ripplerFill="WHITE" style="-fx-background-color: #F44336;" text="Barra 2">
<font>
<Font name="Roboto Light" size="12.0" />
</font>
<HBox.margin>
<Insets bottom="10.0" left="5.0" right="5.0" top="10.0" />
</HBox.margin>
</JFXButton>
<JFXButton fx:id="bar3" alignment="CENTER" buttonType="RAISED" layoutX="80.0" layoutY="10.0" onAction="#bar3Pressed" ripplerFill="WHITE" style="-fx-background-color: #F44336;" text="Barra 3">
<font>
<Font name="Roboto Light" size="12.0" />
</font>
<HBox.margin>
<Insets bottom="10.0" left="5.0" right="5.0" top="10.0" />
</HBox.margin>
</JFXButton>
<JFXButton fx:id="bar4" alignment="CENTER" buttonType="RAISED" layoutX="135.0" layoutY="10.0" onAction="#bar4Pressed" ripplerFill="WHITE" style="-fx-background-color: #F44336;" text="Barra 4">
<font>
<Font name="Roboto Light" size="12.0" />
</font>
<HBox.margin>
<Insets bottom="10.0" left="5.0" right="5.0" top="10.0" />
</HBox.margin>
</JFXButton>
<JFXButton fx:id="bar5" alignment="CENTER" buttonType="RAISED" layoutX="190.0" layoutY="10.0" onAction="#bar5Pressed" ripplerFill="WHITE" style="-fx-background-color: #F44336;" text="Barra 5">
<font>
<Font name="Roboto Light" size="12.0" />
</font>
<HBox.margin>
<Insets bottom="10.0" left="5.0" right="5.0" top="10.0" />
</HBox.margin>
</JFXButton>
<JFXButton fx:id="circle1" alignment="CENTER" buttonType="RAISED" layoutX="280.0" layoutY="20.0" onAction="#circle1Pressed" ripplerFill="WHITE" style="-fx-background-color: #F44336;" text="Cerchio 1">
<font>
<Font name="Roboto Light" size="12.0" />
</font>
<HBox.margin>
<Insets bottom="10.0" left="20.0" right="5.0" top="10.0" />
</HBox.margin>
</JFXButton>
<JFXButton fx:id="circle2" alignment="CENTER" buttonType="RAISED" layoutX="340.0" layoutY="10.0" onAction="#circle2Pressed" ripplerFill="WHITE" style="-fx-background-color: #F44336;" text="Cerchio 2">
<font>
<Font name="Roboto Light" size="12.0" />
</font>
<HBox.margin>
<Insets left="5.0" right="5.0" top="10.0" />
</HBox.margin>
</JFXButton>
<JFXButton fx:id="pot" alignment="CENTER" buttonType="RAISED" layoutX="395.0" layoutY="10.0" onAction="#potPressed" ripplerFill="WHITE" style="-fx-background-color: #F44336;" text="Potenziometri">
<font>
<Font name="Roboto Light" size="12.0" />
</font>
<HBox.margin>
<Insets left="20.0" top="10.0" />
</HBox.margin>
</JFXButton>
</children>
</HBox>
<HBox alignment="CENTER" fillHeight="false" layoutY="80.0" prefHeight="40.0" prefWidth="620.0">
<children>
<VBox prefHeight="200.0" prefWidth="100.0" HBox.hgrow="ALWAYS">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" prefHeight="17.0" prefWidth="135.0" text="Rosso" textAlignment="CENTER" />
<Slider fx:id="rSlider" max="255.0" onDragDetected="#rSliderChanged" value="128.0" />
</children>
<HBox.margin>
<Insets left="10.0" right="10.0" />
</HBox.margin>
</VBox>
<VBox prefHeight="200.0" prefWidth="100.0" HBox.hgrow="ALWAYS">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" prefHeight="17.0" prefWidth="135.0" text="Verde" textAlignment="CENTER" />
<Slider fx:id="gSlider" max="255.0" onDragDetected="#gSliderChanged" value="128.0" />
</children>
<HBox.margin>
<Insets left="10.0" right="10.0" />
</HBox.margin>
</VBox>
<VBox layoutX="110.0" layoutY="10.0" prefHeight="200.0" prefWidth="100.0" HBox.hgrow="ALWAYS">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" prefHeight="17.0" prefWidth="135.0" text="Blu" textAlignment="CENTER" VBox.vgrow="ALWAYS" />
<Slider fx:id="bSlider" max="255.0" onDragDetected="#bSliderChanged" value="128.0" />
</children>
<HBox.margin>
<Insets left="10.0" right="10.0" />
</HBox.margin>
</VBox>
<VBox layoutX="210.0" layoutY="10.0" prefHeight="200.0" prefWidth="100.0" HBox.hgrow="ALWAYS">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" prefHeight="17.0" prefWidth="135.0" text="Luminosità" textAlignment="CENTER" />
<Slider fx:id="aSlider" max="255.0" onDragDetected="#aSliderChanged" value="128.0" />
</children>
<HBox.margin>
<Insets left="10.0" right="10.0" />
</HBox.margin>
</VBox>
</children>
</HBox>
<ColorPicker fx:id="colorPicker" layoutX="10.0" layoutY="130.0" onAction="#colorPickerChanged" prefHeight="25.0" prefWidth="600.0" />
<HBox alignment="TOP_CENTER" layoutY="175.0" prefHeight="50.0" prefWidth="620.0">
<children>
<HBox prefHeight="50.0" prefWidth="150.0">
<children>
<JFXButton fx:id="stroboBars" alignment="CENTER" buttonType="RAISED" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onAction="#stroboBarsPressed" ripplerFill="WHITE" style="-fx-background-color: #F44336;" text="Strobo Barre" textAlignment="CENTER" HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="5.0" top="10.0" />
</HBox.margin>
</JFXButton>
<Button fx:id="stroboBarsConfig" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#stroboBarsConfigPressed" prefHeight="25.0" prefWidth="25.0" style="-fx-background-color: #33b5e5;" textFill="#33b5e5">
<HBox.margin>
<Insets bottom="10.0" right="10.0" top="10.0" />
</HBox.margin>
</Button>
</children>
</HBox>
<HBox layoutX="150.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="50.0" prefWidth="150.0">
<children>
<JFXButton fx:id="stroboCircles" alignment="CENTER" buttonType="RAISED" onAction="#stroboCirclesPressed" ripplerFill="WHITE" style="-fx-background-color: #F44336;" text="Strobo Cerchi" textAlignment="CENTER" HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="5.0" top="10.0" />
</HBox.margin>
</JFXButton>
<Button fx:id="stroboCirclesConfig" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#stroboCirclesConfigPressed" prefHeight="25.0" prefWidth="25.0" style="-fx-background-color: #33b5e5;" textFill="#33b5e5">
<HBox.margin>
<Insets bottom="10.0" right="10.0" top="10.0" />
</HBox.margin>
</Button>
</children>
</HBox>
<HBox layoutX="320.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="50.0" prefWidth="150.0">
<children>
<JFXButton fx:id="fadeColors" alignment="CENTER" buttonType="RAISED" onAction="#fadeColorsPressed" ripplerFill="WHITE" style="-fx-background-color: #F44336;" text="Strobo Cerchi" textAlignment="CENTER" HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="5.0" top="10.0" />
</HBox.margin>
</JFXButton>
<Button fx:id="fadeColorsConfig" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#fadeColorsConfigPressed" prefHeight="25.0" prefWidth="25.0" style="-fx-background-color: #33b5e5;" textFill="#33b5e5">
<HBox.margin>
<Insets bottom="10.0" right="10.0" top="10.0" />
</HBox.margin>
</Button>
</children>
</HBox>
</children>
</HBox>
</children>
</Pane>
Thanks a ton for your help, have a nice day!!
Formenti Matteo

The exception you are getting is because you are trying to hook a MouseEvent handler to an ActionEvent handler - see here. Changing the argument type to MouseEvent should get rid of the exception.
However! this event deals with drag-and-drop, which is probably not what you are after! See this tutorial - you should either bind to the value property of the slider, or add a ChangeListener to it.

Related

javaFX tableview auto scroll to last

I'm new to java/javaFX so be patient with me.
The problem I'm having is that I don't get auto scrolling to work.
I've tried these solutions but didn't get them to work:
javafx tableview auto scroll to the last
JavaFX how to add ChangeListener to auto scroll TableView
Main.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.Image;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("DogRegister.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setMinHeight(900);
stage.setMinWidth(1200);
stage.setResizable(false);
stage.setTitle("Dog Registry");
Image image = new Image("/image/paw.png");
stage.getIcons().add(image);
stage.show();
stage.setOnCloseRequest(event -> {
event.consume();
exit(stage);
});
} catch(Exception e) {
e.printStackTrace();
}
}
public void exit(Stage stage){
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Exit");
alert.setHeaderText("You're about to exit!");
alert.setContentText("Are you sure you want to exit?: ");
if(alert.showAndWait().get() == ButtonType.OK){
System.out.println("You successfully exited!");
stage.close();
}
}
public static void main(String[] args) {
launch(args);
}
}
Controller.java
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
public class DogRegisterController implements Initializable {
#FXML
private TableView<Dog> dogTable;
#FXML
private TableColumn<Dog, Integer> idCol;
#FXML
private TableColumn<Dog, String> nameCol;
#FXML
private TableColumn<Dog, String> BreedCol;
#FXML
private TableColumn<Dog, Integer> ageCol;
#FXML
private TableColumn<Dog, Integer> weightCol;
#FXML
private TableColumn<Dog, Double> tailCol;
#FXML
private TextField idField;
#FXML
private TextField nameField;
#FXML
private TextField breedField;
#FXML
private TextField ageField;
#FXML
private TextField weightField;
#FXML
private Button removeBtn;
#FXML
private Button submitBtn;
#Override
public void initialize(URL location, ResourceBundle resources) {
idCol.setCellValueFactory(new PropertyValueFactory<Dog, Integer>("id"));
nameCol.setCellValueFactory(new PropertyValueFactory<Dog, String>("name"));
BreedCol.setCellValueFactory(new PropertyValueFactory<Dog, String>("breed"));
ageCol.setCellValueFactory(new PropertyValueFactory<Dog, Integer>("age"));
weightCol.setCellValueFactory(new PropertyValueFactory<Dog, Integer>("weight"));
tailCol.setCellValueFactory(new PropertyValueFactory<Dog, Double>("tailLength"));
setupTable();
}
#FXML
void submit(ActionEvent event){
Dog dog = new Dog(nameField.getText(), breedField.getText(),
Integer.parseInt(ageField.getText()), Integer.parseInt(weightField.getText()));
ObservableList<Dog> dogs = dogTable.getItems();
dogs.add(dog);
dogTable.setItems(dogs);
}
#FXML
void edit(ActionEvent event){
ObservableList<Dog> currentTableData = dogTable.getItems();
int currentDogId = Integer.parseInt(idField.getText());
for(Dog dog : currentTableData){
if(dog.getId() == currentDogId) {
dog.setId(Integer.parseInt(idField.getText()));
dog.setName(nameField.getText());
dog.setBreed(breedField.getText());
dog.setAge(Integer.parseInt(ageField.getText()));
dog.setWeight(Integer.parseInt(weightField.getText()));
dogTable.setItems(currentTableData);
dogTable.refresh();
idField.clear();
nameField.clear();
breedField.clear();
ageField.clear();
weightField.clear();
break;
}
}
}
#FXML
void rowClicked(MouseEvent event){
Dog clickedDog = dogTable.getSelectionModel().getSelectedItem();
idField.setText(String.valueOf(clickedDog.getId()));
nameField.setText(String.valueOf(clickedDog.getName()));
breedField.setText(String.valueOf(clickedDog.getBreed()));
ageField.setText(String.valueOf(clickedDog.getAge()));
weightField.setText(String.valueOf(clickedDog.getWeight()));
}
#FXML
void removeDog(ActionEvent event){
int selectedID = dogTable.getSelectionModel().getSelectedIndex();
dogTable.getItems().remove(selectedID);
idField.clear();
nameField.clear();
breedField.clear();
ageField.clear();
weightField.clear();
dogTable.refresh();
}
public static <S> void addAutoScroll(final TableView<S> dogTable) {
if (dogTable == null) {
throw new NullPointerException();
}
dogTable.getItems().addListener((ListChangeListener<S>) (c -> {
c.next();
final int size = dogTable.getItems().size();
if (size > 0) {
dogTable.scrollTo(size - 1);
}
}));
}
private void setupTable(){
Dog dog0 = new Dog("Luna", "Eurasier", 3, 22);
Dog dog1 = new Dog("Skye", "Dachshund", 4, 5);
Dog dog2 = new Dog("Bella", "Dachs", 2, 3);
Dog dog3 = new Dog("Sky", "Tax", 3, 3);
dogTable.getItems().addAll(dog0, dog1, dog2, dog3);
}
}
Dog.java
import java.util.concurrent.atomic.AtomicInteger;
public class Dog {
private static final AtomicInteger _ID = new AtomicInteger(0);
private String name;
private String breed;
private int age;
private int weight;
private double tailLength;
private int id;
public Dog(String name, String breed, int age, int weight){
this.id = _ID.incrementAndGet();
this.name = name;
this.breed = breed;
this.age = age;
this.weight = weight;
this.tailLength = getTailLength();
}
public double getTailLength(){
if (breed.toLowerCase().equals("tax") || breed.toLowerCase().equals("dachshund")|| breed.toLowerCase().equals("dachs")){
return 3.7;
} else {
return (age * weight) / 10.0;
}
}
public int getId() { return id; }
public void setId(int id){ this.id = id; }
public String getName() { return name; }
public void setName(String name){ this.name = name; }
public String getBreed() { return breed; }
public void setBreed(String breed){this.breed = breed;}
public int getWeight() { return weight; }
public void setWeight(int weight) { this.weight = weight; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
}
GUI.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.String?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane fx:id="mainPane" minHeight="900.0" minWidth="1200.0" prefHeight="900.0" prefWidth="1200.0" styleClass="pane" stylesheets="#StyleSheet.css" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="DogRegisterController">
<children>
<TableView fx:id="dogTable" layoutX="586.0" layoutY="160.0" onMouseClicked="#rowClicked" prefHeight="700.0" prefWidth="600.0" stylesheets="#StyleSheet.css" AnchorPane.rightAnchor="60.0" AnchorPane.topAnchor="100.0">
<columns>
<TableColumn fx:id="idCol" minWidth="50.0" prefWidth="50.0" text="ID" />
<TableColumn fx:id="nameCol" minWidth="140.0" prefWidth="140.0" text="Name" />
<TableColumn fx:id="BreedCol" minWidth="140.0" prefWidth="140.0" text="Breed" />
<TableColumn fx:id="ageCol" minWidth="80.0" text="Age" />
<TableColumn fx:id="weightCol" minWidth="80.0" text="Weight" />
<TableColumn fx:id="tailCol" minWidth="96.0" prefWidth="96.0" text="Tail-length" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
<styleClass>
<String fx:value="column-header" />
<String fx:value="column-header-background" />
<String fx:value="corner" />
<String fx:value="filler" />
<String fx:value="table-cell" />
<String fx:value="table-row-cell" />
<String fx:value="table-view" />
</styleClass>
</TableView>
<ImageView fitHeight="100.0" fitWidth="150.0" layoutX="220.0" layoutY="100.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#image/paw.png" />
</image>
</ImageView>
<VBox prefHeight="900.0" prefWidth="560.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="ID" textAlignment="CENTER">
<font>
<Font size="14.0" />
</font>
<VBox.margin>
<Insets left="100.0" right="100.0" top="260.0" />
</VBox.margin>
</Text>
<TextField fx:id="idField" editable="false" prefHeight="35.0" styleClass="text-field" stylesheets="#StyleSheet.css">
<VBox.margin>
<Insets bottom="5.0" left="100.0" right="100.0" top="5.0" />
</VBox.margin>
<padding>
<Insets left="10.0" />
</padding>
</TextField>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Name" textAlignment="CENTER">
<font>
<Font size="14.0" />
</font>
<VBox.margin>
<Insets left="100.0" right="100.0" />
</VBox.margin>
</Text>
<TextField fx:id="nameField" layoutX="10.0" layoutY="27.0" prefHeight="35.0" styleClass="text-field" stylesheets="#StyleSheet.css">
<VBox.margin>
<Insets bottom="5.0" left="100.0" right="100.0" top="5.0" />
</VBox.margin>
<padding>
<Insets left="10.0" />
</padding>
</TextField>
<Text layoutX="10.0" layoutY="39.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Breed" textAlignment="CENTER">
<font>
<Font size="14.0" />
</font>
<VBox.margin>
<Insets left="100.0" right="100.0" />
</VBox.margin>
</Text>
<TextField fx:id="breedField" layoutX="10.0" layoutY="127.0" prefHeight="35.0" styleClass="text-field" stylesheets="#StyleSheet.css">
<VBox.margin>
<Insets bottom="5.0" left="100.0" right="100.0" top="5.0" />
</VBox.margin>
<padding>
<Insets left="10.0" />
</padding>
</TextField>
<Text layoutX="10.0" layoutY="57.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Age" textAlignment="CENTER">
<font>
<Font size="14.0" />
</font>
<VBox.margin>
<Insets left="100.0" right="100.0" />
</VBox.margin>
</Text>
<TextField fx:id="ageField" layoutX="10.0" layoutY="144.0" prefHeight="35.0" styleClass="text-field" stylesheets="#StyleSheet.css">
<VBox.margin>
<Insets bottom="5.0" left="100.0" right="100.0" top="5.0" />
</VBox.margin>
<padding>
<Insets left="10.0" />
</padding>
</TextField>
<Text layoutX="10.0" layoutY="71.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Weight" textAlignment="CENTER">
<font>
<Font size="14.0" />
</font>
<VBox.margin>
<Insets left="100.0" right="100.0" />
</VBox.margin>
</Text>
<TextField fx:id="weightField" layoutX="10.0" layoutY="160.0" prefHeight="35.0" styleClass="text-field" stylesheets="#StyleSheet.css">
<VBox.margin>
<Insets bottom="5.0" left="100.0" right="100.0" top="5.0" />
</VBox.margin>
<padding>
<Insets left="10.0" />
</padding>
</TextField>
<HBox prefHeight="100.0" prefWidth="200.0">
<children>
<Button fx:id="submitBtn" alignment="CENTER" mnemonicParsing="false" onAction="#submit" prefHeight="40.0" prefWidth="140.0" stylesheets="#../../Assignment-3/src/style.css" text="Submit">
<font>
<Font size="14.0" />
</font>
<HBox.margin>
<Insets left="25.0" right="25.0" top="10.0" />
</HBox.margin>
</Button>
<Button fx:id="editBtn" alignment="CENTER" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" onAction="#edit" prefHeight="40.0" prefWidth="140.0" stylesheets="#../../Assignment-3/src/style.css" text="Edit">
<font>
<Font size="14.0" />
</font>
<HBox.margin>
<Insets left="25.0" right="25.0" top="10.0" />
</HBox.margin>
</Button>
</children>
<VBox.margin>
<Insets bottom="5.0" left="100.0" right="100.0" top="5.0" />
</VBox.margin>
</HBox>
</children>
</VBox>
<Button fx:id="removeBtn" alignment="CENTER" layoutX="770.0" layoutY="825.0" mnemonicParsing="false" onAction="#removeDog" prefHeight="40.0" prefWidth="140.0" stylesheets="#../../Assignment-3/src/style.css" text="Remove" AnchorPane.bottomAnchor="35.0">
<font>
<Font size="14.0" />
</font>
</Button>
</children>
</AnchorPane>
got it working thanks to #jewelsea
had forgotten to add "addAutoScroll(dogTable)" to setupTable method so the auto scroll was never called.
Controller.java
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
public class DogRegisterController implements Initializable {
public static <S> void addAutoScroll(final TableView<S> dogTable) {
if (dogTable == null) {
throw new NullPointerException();
}
dogTable.getItems().addListener((ListChangeListener<S>) (c -> {
c.next();
final int size = dogTable.getItems().size();
if (size > 0) {
dogTable.scrollTo(size - 1);
}
}));
}
private void setupTable(){
Dog dog0 = new Dog("Luna", "Eurasier", 3, 22);
Dog dog1 = new Dog("Skye", "Dachshund", 4, 5);
Dog dog2 = new Dog("Bella", "Dachs", 2, 3);
Dog dog3 = new Dog("Sky", "Tax", 3, 3);
dogTable.getItems().addAll(dog0, dog1, dog2, dog3);
addAutoScroll(dogTable);
}
}

JavaFX pass arguments

I create the following class:
package sample;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextArea;
import com.jfoenix.controls.JFXTextField;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import vom.CAPlatform;
public class SecureCAController extends Application {
private double xOffset;
private double yOffset;
public CAPlatform myAgent;
public static boolean ready = false;
public SecureCAController(CAPlatform caPlatform) {
myAgent = caPlatform;
System.out.println(myAgent.getAID());
}
public SecureCAController() {
}
public void show(){
launch();
}
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
root.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
xOffset = mouseEvent.getSceneX();
yOffset = mouseEvent.getSceneY();
}
});
root.setOnMouseDragged(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
primaryStage.setX(mouseEvent.getScreenX() - xOffset);
primaryStage.setY(mouseEvent.getScreenY() - yOffset);
}
});
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setTitle("CA");
Scene sceneNew = new Scene(root);
sceneNew.setFill(Color.TRANSPARENT);
primaryStage.setScene(sceneNew);
primaryStage.show();
ready = true;
}
public static void main(String[] args) {
launch(args);
}
public void setAgent(CAPlatform caPlatform) {
myAgent = caPlatform;
}
#FXML
private ImageView userArrow;
#FXML private ImageView printerArrow;
#FXML private ImageView crudArrow;
#FXML private ImageView exitArrow;
#FXML private AnchorPane userPanel;
#FXML private AnchorPane printerPanel;
#FXML private AnchorPane crudPanel;
#FXML private JFXTextField userText;
#FXML private JFXPasswordField passwordText;
#FXML private JFXTextField AIDText;
#FXML private JFXButton startButton;
#FXML private JFXButton pendingButton;
#FXML private JFXButton validateButton;
#FXML private JFXButton validateRButton;
#FXML private JFXTextArea PList;
#FXML private JFXTextArea AreaList;
public void onstartButton(ActionEvent event){
if(userText.getText().isEmpty() || passwordText.getText().isEmpty()){
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("ERROR");
alert.setContentText("PLEASE INSERT USERNAME AND PASSWORD");
}else{
System.out.println(this.myAgent.getAID());
}
}
public void onpendingButton(ActionEvent event){
}
public void onvalidateButton(ActionEvent event){
if(AIDText.getText().isEmpty()){
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("ERROR");
alert.setContentText("PLEASE INSERT AN AID");
}
}
public void onvalidateRButton(ActionEvent event){
}
public void onExitButtonClicked(MouseEvent event){
Platform.exit();
System.exit(0);
}
public void onUserButtonClicked(MouseEvent event){
System.out.println("si");
userPanel.setVisible(true);
userArrow.setVisible(true);
printerPanel.setVisible(false);
crudPanel.setVisible(false);
printerArrow.setVisible(false);
crudArrow.setVisible(false);
exitArrow.setVisible(false);
}
public void onPrinterButtonClicked(MouseEvent event){
printerPanel.setVisible(true);
printerArrow.setVisible(true);
crudPanel.setVisible(false);
userPanel.setVisible(false);
userArrow.setVisible(false);
crudArrow.setVisible(false);
exitArrow.setVisible(false);
}
public void onCRUDButtonClicked(MouseEvent event){
crudPanel.setVisible(true);
crudArrow.setVisible(true);
userPanel.setVisible(false);
userArrow.setVisible(false);
printerPanel.setVisible(false);
printerArrow.setVisible(false);
exitArrow.setVisible(false);
}
}
In the secureCAController, initialize the agent and launch the application, but when i try to execute one method for example myAgent.getname() return null.
Anyone can help me? I dont know a lot of about javafx sorry if it is a dummy error.
myAgent is an object, that contains a lot of methods.
My fxml is the following:
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextArea?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="474.0" prefWidth="562.0" style="-fx-background-color: #85c0cc;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.SecureCAController">
<children>
<AnchorPane prefHeight="0.0" prefWidth="562.0" style="-fx-background-color: #0f6b7d;">
<children>
<HBox prefHeight="63.0" prefWidth="562.0">
<children>
<Separator prefWidth="200.0" visible="false" />
<ImageView fitHeight="46.0" fitWidth="69.0" onMouseClicked="#onUserButtonClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/user.png" />
</image>
<HBox.margin>
<Insets top="10.0" />
</HBox.margin>
</ImageView>
<Separator prefWidth="200.0" visible="false" />
<ImageView fitHeight="46.0" fitWidth="69.0" onMouseClicked="#onPrinterButtonClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/print.png" />
</image>
<HBox.margin>
<Insets top="10.0" />
</HBox.margin>
</ImageView>
<Separator prefWidth="200.0" visible="false" />
<ImageView fitHeight="46.0" fitWidth="69.0" onMouseClicked="#onCRUDButtonClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/accept.png" />
</image>
<HBox.margin>
<Insets top="10.0" />
</HBox.margin>
</ImageView>
<Separator prefWidth="200.0" visible="false" />
<ImageView fitHeight="46.0" fitWidth="69.0" onMouseClicked="#onExitButtonClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/exit.png" />
</image>
<HBox.margin>
<Insets top="10.0" />
</HBox.margin>
</ImageView>
<Separator prefWidth="200.0" visible="false" />
</children>
</HBox>
</children>
</AnchorPane>
<AnchorPane fx:id="userPanel" layoutY="86.0" prefHeight="398.0" prefWidth="562.0" style="-fx-background-color: #0f6b7d;">
<children>
<AnchorPane layoutX="20.0" layoutY="28.0" prefHeight="280.0" prefWidth="522.0" style="-fx-background-color: #85c0cc;" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0">
<children>
<Label layoutX="60.0" layoutY="126.0" prefHeight="26.0" prefWidth="120.0" text=" USER:">
<font>
<Font name="SansSerif Bold" size="20.0" />
</font>
</Label>
<Label layoutX="57.0" layoutY="191.0" text="PASSWORD:">
<font>
<Font name="SansSerif Bold Italic" size="20.0" />
</font>
</Label>
<JFXTextField fx:id="userText" layoutX="228.0" layoutY="126.0" prefHeight="27.0" prefWidth="236.0" promptText="Enter your username" />
<ImageView fitHeight="100.0" fitWidth="100.0" layoutX="220.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/shelt.png" />
</image>
</ImageView>
<JFXPasswordField fx:id="passwordText" layoutX="228.0" layoutY="189.0" prefHeight="27.0" prefWidth="236.0" promptText="Enter your password" />
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
</AnchorPane>
<JFXButton fx:id="startButton" layoutX="203.0" layoutY="337.0" onAction="#onstartButton" prefHeight="27.0" prefWidth="157.0" style="-fx-background-color: #85c0cc;" text="Start">
<font>
<Font size="20.0" />
</font>
</JFXButton>
</children>
</AnchorPane>
<ImageView fx:id="userArrow" fitHeight="30.0" fitWidth="90.0" layoutX="83.0" layoutY="59.0" pickOnBounds="true" preserveRatio="true" rotate="180.0">
<image>
<Image url="#Images/arrow.png" />
</image>
</ImageView>
<ImageView fx:id="printerArrow" fitHeight="30.0" fitWidth="90.0" layoutX="208.0" layoutY="59.0" pickOnBounds="true" preserveRatio="true" rotate="180.0" visible="false">
<image>
<Image url="#Images/arrow.png" />
</image>
</ImageView>
<ImageView fx:id="crudArrow" fitHeight="30.0" fitWidth="90.0" layoutX="327.0" layoutY="59.0" pickOnBounds="true" preserveRatio="true" rotate="180.0" visible="false">
<image>
<Image url="#Images/arrow.png" />
</image>
</ImageView>
<ImageView fx:id="exitArrow" fitHeight="30.0" fitWidth="90.0" layoutX="444.0" layoutY="59.0" pickOnBounds="true" preserveRatio="true" rotate="180.0" visible="false">
<image>
<Image url="#Images/arrow.png" />
</image>
</ImageView>
<AnchorPane fx:id="printerPanel" layoutX="10.0" layoutY="96.0" prefHeight="398.0" prefWidth="562.0" style="-fx-background-color: #0f6b7d;" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="86.0">
<children>
<AnchorPane layoutX="20.0" layoutY="28.0" prefHeight="280.0" prefWidth="522.0" style="-fx-background-color: #85c0cc;" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0">
<opaqueInsets>
<Insets />
</opaqueInsets>
<children>
<Separator layoutY="24.0" prefHeight="3.0" prefWidth="522.0" style="-fx-background-color: #000000;" />
<Label layoutX="203.0" layoutY="2.0" text="Platform List" textAlignment="CENTER">
<font>
<Font name="SansSerif Regular" size="20.0" />
</font>
</Label>
<JFXTextArea fx:id="AreaList" layoutY="27.0" prefHeight="252.0" prefWidth="523.0" />
</children>
</AnchorPane>
<JFXButton fx:id="pendingButton" layoutX="20.0" layoutY="334.0" onAction="#onpendingButton" prefHeight="40.0" prefWidth="222.0" style="-fx-background-color: #85c0cc;" text="Pending Requests">
<font>
<Font size="20.0" />
</font>
</JFXButton>
<JFXButton fx:id="validateRButton" layoutX="320.0" layoutY="334.0" onAction="#onvalidateRButton" prefHeight="40.0" prefWidth="222.0" style="-fx-background-color: #85c0cc;" text="Validated Requests">
<font>
<Font size="20.0" />
</font>
</JFXButton>
</children>
</AnchorPane>
<AnchorPane fx:id="crudPanel" layoutX="20.0" layoutY="106.0" prefHeight="398.0" prefWidth="562.0" style="-fx-background-color: #0f6b7d;" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="86.0">
<children>
<AnchorPane layoutX="20.0" layoutY="28.0" prefHeight="280.0" prefWidth="522.0" style="-fx-background-color: #85c0cc;" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0">
<opaqueInsets>
<Insets />
</opaqueInsets>
<children>
<Separator layoutY="24.0" prefHeight="3.0" prefWidth="522.0" style="-fx-background-color: #000000;" />
<Label layoutX="184.0" layoutY="2.0" text="Pending Platforms" textAlignment="CENTER">
<font>
<Font name="SansSerif Regular" size="20.0" />
</font>
</Label>
<JFXTextArea fx:id="PList" layoutY="27.0" prefHeight="252.0" prefWidth="523.0" />
</children>
</AnchorPane>
<JFXButton fx:id="ValidateButton" layoutX="322.0" layoutY="333.0" onAction="#onvalidateButton" prefHeight="40.0" prefWidth="219.0" style="-fx-background-color: #85c0cc;" text="Validate">
<font>
<Font size="20.0" />
</font>
</JFXButton>
<JFXTextField fx:id="AIDText" focusColor="BLACK" layoutX="20.0" layoutY="344.0" prefHeight="27.0" prefWidth="290.0" unFocusColor="WHITE" />
<Label layoutX="19.0" layoutY="333.0" text="PLATFORM AID:" textFill="WHITE" />
</children>
</AnchorPane>
</children>
</AnchorPane>
I have cleaned up your project a little:
Your Aplication Class:
package vom;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author mipog
*/
public class SecureCA extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("SecureCA.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Your FXML-File:
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextArea?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="474.0" prefWidth="562.0" style="-fx-background-color: #85c0cc;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="vom.SecureCAController">
<children>
<AnchorPane prefHeight="0.0" prefWidth="562.0" style="-fx-background-color: #0f6b7d;">
<children>
<HBox prefHeight="63.0" prefWidth="562.0">
<children>
<Separator prefWidth="200.0" visible="false" />
<ImageView fitHeight="46.0" fitWidth="69.0" onMouseClicked="#onUserButtonClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/user.png" />
</image>
<HBox.margin>
<Insets top="10.0" />
</HBox.margin>
</ImageView>
<Separator prefWidth="200.0" visible="false" />
<ImageView fitHeight="46.0" fitWidth="69.0" onMouseClicked="#onPrinterButtonClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/print.png" />
</image>
<HBox.margin>
<Insets top="10.0" />
</HBox.margin>
</ImageView>
<Separator prefWidth="200.0" visible="false" />
<ImageView fitHeight="46.0" fitWidth="69.0" onMouseClicked="#onCRUDButtonClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/accept.png" />
</image>
<HBox.margin>
<Insets top="10.0" />
</HBox.margin>
</ImageView>
<Separator prefWidth="200.0" visible="false" />
<ImageView fitHeight="46.0" fitWidth="69.0" onMouseClicked="#onExitButtonClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/exit.png" />
</image>
<HBox.margin>
<Insets top="10.0" />
</HBox.margin>
</ImageView>
<Separator prefWidth="200.0" visible="false" />
</children>
</HBox>
</children>
</AnchorPane>
<AnchorPane fx:id="userPanel" layoutY="86.0" prefHeight="398.0" prefWidth="562.0" style="-fx-background-color: #0f6b7d;">
<children>
<AnchorPane layoutX="20.0" layoutY="28.0" prefHeight="280.0" prefWidth="522.0" style="-fx-background-color: #85c0cc;" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0">
<children>
<Label layoutX="60.0" layoutY="126.0" prefHeight="26.0" prefWidth="120.0" text=" USER:">
<font>
<Font name="SansSerif Bold" size="20.0" />
</font>
</Label>
<Label layoutX="57.0" layoutY="191.0" text="PASSWORD:">
<font>
<Font name="SansSerif Bold Italic" size="20.0" />
</font>
</Label>
<JFXTextField fx:id="userText" layoutX="228.0" layoutY="126.0" prefHeight="27.0" prefWidth="236.0" promptText="Enter your username" />
<ImageView fitHeight="100.0" fitWidth="100.0" layoutX="220.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Images/shelt.png" />
</image>
</ImageView>
<JFXPasswordField fx:id="passwordText" layoutX="228.0" layoutY="189.0" prefHeight="27.0" prefWidth="236.0" promptText="Enter your password" />
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
</AnchorPane>
<JFXButton fx:id="startButton" layoutX="203.0" layoutY="337.0" onAction="#onstartButton" prefHeight="27.0" prefWidth="157.0" style="-fx-background-color: #85c0cc;" text="Start">
<font>
<Font size="20.0" />
</font>
</JFXButton>
</children>
</AnchorPane>
<ImageView fx:id="userArrow" fitHeight="30.0" fitWidth="90.0" layoutX="83.0" layoutY="59.0" pickOnBounds="true" preserveRatio="true" rotate="180.0">
<image>
<Image url="#Images/arrow.png" />
</image>
</ImageView>
<ImageView fx:id="printerArrow" fitHeight="30.0" fitWidth="90.0" layoutX="208.0" layoutY="59.0" pickOnBounds="true" preserveRatio="true" rotate="180.0" visible="false">
<image>
<Image url="#Images/arrow.png" />
</image>
</ImageView>
<ImageView fx:id="crudArrow" fitHeight="30.0" fitWidth="90.0" layoutX="327.0" layoutY="59.0" pickOnBounds="true" preserveRatio="true" rotate="180.0" visible="false">
<image>
<Image url="#Images/arrow.png" />
</image>
</ImageView>
<ImageView fx:id="exitArrow" fitHeight="30.0" fitWidth="90.0" layoutX="444.0" layoutY="59.0" pickOnBounds="true" preserveRatio="true" rotate="180.0" visible="false">
<image>
<Image url="#Images/arrow.png" />
</image>
</ImageView>
<AnchorPane fx:id="printerPanel" layoutX="10.0" layoutY="96.0" prefHeight="398.0" prefWidth="562.0" style="-fx-background-color: #0f6b7d;" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="86.0">
<children>
<AnchorPane layoutX="20.0" layoutY="28.0" prefHeight="280.0" prefWidth="522.0" style="-fx-background-color: #85c0cc;" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0">
<opaqueInsets>
<Insets />
</opaqueInsets>
<children>
<Separator layoutY="24.0" prefHeight="3.0" prefWidth="522.0" style="-fx-background-color: #000000;" />
<Label layoutX="203.0" layoutY="2.0" text="Platform List" textAlignment="CENTER">
<font>
<Font name="SansSerif Regular" size="20.0" />
</font>
</Label>
<JFXTextArea fx:id="AreaList" layoutY="27.0" prefHeight="252.0" prefWidth="523.0" />
</children>
</AnchorPane>
<JFXButton fx:id="pendingButton" layoutX="20.0" layoutY="334.0" onAction="#onpendingButton" prefHeight="40.0" prefWidth="222.0" style="-fx-background-color: #85c0cc;" text="Pending Requests">
<font>
<Font size="20.0" />
</font>
</JFXButton>
<JFXButton fx:id="validateRButton" layoutX="320.0" layoutY="334.0" onAction="#onvalidateRButton" prefHeight="40.0" prefWidth="222.0" style="-fx-background-color: #85c0cc;" text="Validated Requests">
<font>
<Font size="20.0" />
</font>
</JFXButton>
</children>
</AnchorPane>
<AnchorPane fx:id="crudPanel" layoutX="20.0" layoutY="106.0" prefHeight="398.0" prefWidth="562.0" style="-fx-background-color: #0f6b7d;" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="86.0">
<children>
<AnchorPane layoutX="20.0" layoutY="28.0" prefHeight="280.0" prefWidth="522.0" style="-fx-background-color: #85c0cc;" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0">
<opaqueInsets>
<Insets />
</opaqueInsets>
<children>
<Separator layoutY="24.0" prefHeight="3.0" prefWidth="522.0" style="-fx-background-color: #000000;" />
<Label layoutX="184.0" layoutY="2.0" text="Pending Platforms" textAlignment="CENTER">
<font>
<Font name="SansSerif Regular" size="20.0" />
</font>
</Label>
<JFXTextArea fx:id="PList" layoutY="27.0" prefHeight="252.0" prefWidth="523.0" />
</children>
</AnchorPane>
<JFXButton fx:id="ValidateButton" layoutX="322.0" layoutY="333.0" onAction="#onvalidateButton" prefHeight="40.0" prefWidth="219.0" style="-fx-background-color: #85c0cc;" text="Validate">
<font>
<Font size="20.0" />
</font>
</JFXButton>
<JFXTextField fx:id="AIDText" focusColor="BLACK" layoutX="20.0" layoutY="344.0" prefHeight="27.0" prefWidth="290.0" unFocusColor="WHITE" />
<Label layoutX="19.0" layoutY="333.0" text="PLATFORM AID:" textFill="WHITE" />
</children>
</AnchorPane>
</children>
</AnchorPane>
And your Controller Class:
package vom;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextArea;
import com.jfoenix.controls.JFXTextField;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
/**
*
* #author mipog
*/
public class SecureCAController implements Initializable {
#FXML
private ImageView userArrow;
#FXML
private ImageView printerArrow;
#FXML
private ImageView crudArrow;
#FXML
private ImageView exitArrow;
#FXML
private AnchorPane userPanel;
#FXML
private AnchorPane printerPanel;
#FXML
private AnchorPane crudPanel;
#FXML
private JFXTextField userText;
#FXML
private JFXPasswordField passwordText;
#FXML
private JFXTextField AIDText;
#FXML
private JFXButton startButton;
#FXML
private JFXButton pendingButton;
#FXML
private JFXButton validateButton;
#FXML
private JFXButton validateRButton;
#FXML
private JFXTextArea PList;
#FXML
private JFXTextArea AreaList;
//You can either here init your myAgent or in the initialize method
CAPlatform myAgent = new CAPlatform();
#FXML
private void onstartButton() {
if (userText.getText().isEmpty() || passwordText.getText().isEmpty()) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("ERROR");
alert.setContentText("PLEASE INSERT USERNAME AND PASSWORD");
} else {
System.out.println(this.myAgent.getAID());
}
}
#FXML
private void onpendingButton() {
}
#FXML
private void onvalidateButton() {
if (AIDText.getText().isEmpty()) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("ERROR");
alert.setContentText("PLEASE INSERT AN AID");
}
}
#FXML
private void onvalidateRButton() {
}
#FXML
private void onExitButtonClicked() {
Platform.exit();
System.exit(0);
}
#FXML
private void onUserButtonClicked() {
System.out.println("si");
userPanel.setVisible(true);
userArrow.setVisible(true);
printerPanel.setVisible(false);
crudPanel.setVisible(false);
printerArrow.setVisible(false);
crudArrow.setVisible(false);
exitArrow.setVisible(false);
}
#FXML
private void onPrinterButtonClicked() {
printerPanel.setVisible(true);
printerArrow.setVisible(true);
crudPanel.setVisible(false);
userPanel.setVisible(false);
userArrow.setVisible(false);
crudArrow.setVisible(false);
exitArrow.setVisible(false);
}
#FXML
private void onCRUDButtonClicked() {
crudPanel.setVisible(true);
crudArrow.setVisible(true);
userPanel.setVisible(false);
userArrow.setVisible(false);
printerPanel.setVisible(false);
printerArrow.setVisible(false);
exitArrow.setVisible(false);
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// Here you init you myAgent
myAgent = new CAPlatform();
}
}
Maybe this will help you a little..
if you want to pass myAgent to the Controller Class from the Application Class. Your Application class has to look like the following. And you have to make a setter for the myAgent variable in the Controller Class.
package vom;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author mipog
*/
public class SecureCA extends Application {
CAPlatform myAgent = new CAPlatform();
#Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("SecureCA.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
SecureCAController controller = loader.getController();
controller.setMyAgent(myAgent);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

Setting a Textfield in MainController through Controller Instance Reference in another file

Im trying to set the Text of a Textfield in my MainController from another file.
I read that this:
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
MainController mc = (MainController) loader.getController();
is how you're supposed to get a reference to the Controller Instance in Javafx. I tried to implement it, but it gives me a NullPointerException when trying to set the TextField.
Here is my Code:
Main.java:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
public class Main extends Application {
static MainController mc;
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
Scene scene = new Scene(root,348,212);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Flextime calculator");
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("assets/icon.png")));
primaryStage.setResizable(false);
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
mc = (MainController) loader.getController();
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
public static MainController getMc() {
return mc;
}
}
MainController.java:
public class MainController {
#FXML Label fxFlextime;
#FXML Label fxTotalFlextime;
#FXML TextField fxStarttime;
#FXML TextField fxEndtime;
#FXML TextField fxDailyWorktime;
#FXML TextField fxBreaktime;
#FXML TextField fxAddBreaktime;
#FXML TextField fxBreakGap;
#FXML CheckBox fxAddBreak;
//Create Model, Userconfig
Model model = new Model();
UserConfig config = new UserConfig();
public void initialize() {
config.initialize();
}
}
UserConfig.java:
public class UserConfig {
public void initialize() {
insertDefault();
}
void insertDefault() {
List<String> prefValues = getPreference();
//FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
//MainController mc = (MainController) loader.getController();
// ^Tried also putting it here to see wether im referencing the variable wrong or something,
// so maybe im getting the Controller wrong? With mc.fxStarttime.setText("") it also didnt work here
Main.mc.fxStarttime.setText("");
Main.mc.fxEndtime.setText(prefValues.get(2));
Main.mc.fxDailyWorktime.setText(prefValues.get(3));
Main.mc.fxBreaktime.setText(prefValues.get(4));
Main.mc.fxAddBreaktime.setText(prefValues.get(5));
Main.mc.fxBreakGap.setText(prefValues.get(6));
}
static List<String> getPreference() {
// This will define a node in which the preferences can be stored
Preferences userPrefs = Preferences.userNodeForPackage(Main.class);
try {
String[] keys = userPrefs.keys();
if (keys == null) {
userPrefs.put("DEFStarttime" , "1");
userPrefs.put("DEFEndtime" , "");
userPrefs.put("DEFDailyWorktime", "");
userPrefs.put("DEFBreaktime" , "");
userPrefs.put("DEFAddBreaktime" , "");
userPrefs.put("DEFBreakGap" , "");
}
} catch (BackingStoreException ex) {
System.err.println(ex);
}
List<String> prefValues = new ArrayList<String>();
prefValues.add(userPrefs.get("DEFStarttime" , ""));
prefValues.add(userPrefs.get("DEFEndtime" , ""));
prefValues.add(userPrefs.get("DEFDailyWorktime" , ""));
prefValues.add(userPrefs.get("DEFBreaktime" , ""));
prefValues.add(userPrefs.get("DEFAddBreaktime" , ""));
prefValues.add(userPrefs.get("DEFBreakGap" , ""));
/*
for(String string : prefValues) {
System.out.println(string);
}
*/
return prefValues;
}
}
The Error:
javafx.fxml.LoadException:
/C:/eclipse-workspace/project/bin/application/main.fxml
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at application.Main.start(Main.java:18)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2591)
... 17 more
Caused by: java.lang.NullPointerException
at application.UserConfig.insertDefault(UserConfig.java:25)
at application.UserConfig.initialize(UserConfig.java:18)
at application.MainController.initialize(MainController.java:39)
... 28 more
I tried to cut it down to the minimal amount of code.
Edit:
Here the Main.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="212.0" prefWidth="348.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<children>
<MenuBar fx:id="fxMenuBar">
<menus>
<Menu fx:id="fxFileMenu" mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="fxExitItem" mnemonicParsing="false" onAction="#exitProgram" text="Exit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Settings">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<FlowPane alignment="CENTER_LEFT" prefHeight="25.0" prefWidth="403.0">
<children>
<Label alignment="CENTER" prefHeight="15.0" prefWidth="65.0" text="Starttime:" underline="true">
<FlowPane.margin>
<Insets left="10.0" top="10.0" />
</FlowPane.margin>
</Label>
<TextField fx:id="fxStarttime" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00:00">
<FlowPane.margin>
<Insets top="10.0" />
</FlowPane.margin></TextField>
<Label alignment="CENTER" prefHeight="17.0" prefWidth="112.0" text="Required daily hours:" underline="true">
<FlowPane.margin>
<Insets left="10.0" right="5.0" top="10.0" />
</FlowPane.margin>
</Label>
<TextField fx:id="fxDailyWorktime" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00.00">
<FlowPane.margin>
<Insets top="10.0" />
</FlowPane.margin></TextField>
</children>
</FlowPane>
<FlowPane alignment="CENTER_LEFT" prefHeight="36.0" prefWidth="300.0">
<children>
<Label alignment="CENTER" prefHeight="15.0" prefWidth="65.0" text="Endtime:" underline="true">
<FlowPane.margin>
<Insets left="10.0" />
</FlowPane.margin>
</Label>
<TextField fx:id="fxEndtime" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00:00" />
<Label alignment="CENTER" prefHeight="15.0" prefWidth="65.0" text="Breaktime:" underline="true">
<FlowPane.margin>
<Insets left="57.0" right="5.0" />
</FlowPane.margin>
</Label>
<TextField fx:id="fxBreaktime" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00" />
</children>
</FlowPane>
<FlowPane prefHeight="38.0" prefWidth="300.0">
<children>
<CheckBox fx:id="fxAddBreak" mnemonicParsing="false" text="Add:">
<FlowPane.margin>
<Insets left="20.0" top="5.0" />
</FlowPane.margin>
</CheckBox>
<TextField fx:id="fxAddBreaktime" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00">
<FlowPane.margin>
<Insets left="10.0" right="5.0" top="5.0" />
</FlowPane.margin>
</TextField>
<Label text="breaktime from ">
<FlowPane.margin>
<Insets top="5.0" />
</FlowPane.margin>
</Label>
<TextField fx:id="fxBreakGap" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00.00">
<FlowPane.margin>
<Insets top="5.0" />
</FlowPane.margin>
</TextField>
<Label text="working hours">
<FlowPane.margin>
<Insets left="5.0" top="5.0" />
</FlowPane.margin>
</Label>
</children>
</FlowPane>
<FlowPane alignment="CENTER_RIGHT" prefHeight="32.0" prefWidth="300.0">
<children>
<Label text="Flextime:">
<FlowPane.margin>
<Insets right="10.0" />
</FlowPane.margin>
</Label>
<Label fx:id="fxFlextime" alignment="CENTER" prefHeight="10.0" prefWidth="35.0" text="0.00" textAlignment="CENTER">
<FlowPane.margin>
<Insets />
</FlowPane.margin>
</Label>
<Label text="h">
<FlowPane.margin>
<Insets right="10.0" />
</FlowPane.margin>
</Label>
</children>
</FlowPane>
<FlowPane alignment="CENTER_RIGHT" prefHeight="32.0" prefWidth="300.0">
<children>
<Label text="Total flextime:" underline="true">
<FlowPane.margin>
<Insets right="10.0" />
</FlowPane.margin>
</Label>
<Label fx:id="fxTotalFlextime" alignment="CENTER" prefHeight="10.0" prefWidth="35.0" text="0.00" textAlignment="CENTER">
<FlowPane.margin>
<Insets />
</FlowPane.margin>
</Label>
<Label text="h">
<FlowPane.margin>
<Insets right="10.0" />
</FlowPane.margin>
</Label>
</children>
</FlowPane>
<FlowPane alignment="CENTER_RIGHT" prefHeight="38.0" prefWidth="300.0">
<children>
<Button mnemonicParsing="false" onAction="#calculate" text="Calculate" underline="true">
<FlowPane.margin>
<Insets right="10.0" />
</FlowPane.margin>
</Button>
<Button mnemonicParsing="false" onAction="#addToTotal" text="Add to total" />
<Button mnemonicParsing="false" onAction="#clear" text="Clear">
<FlowPane.margin>
<Insets left="10.0" right="10.0" />
</FlowPane.margin>
</Button>
</children>
</FlowPane>
</children>
</VBox>
The reason why you are getting the NullPointerException is because the Main.mc is null at the point where you are trying to access it fields in UserConfig.
In the line where you load your main.fxml Parent root = FXMLLoader.load(getClass().getResource("main.fxml")); the initialize() method in the controller MainController is invoked and at that point you are invoking userConfig.initialize() which also make a backward reference to Main.mc which at that point is null.
You can make the config public in your MainController class and initialize user config after you set the mc field in the Main class. Or you can inject the MainController as parameter into the UserConfig initialize method.
Main.java
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
Parent root = (Parent) loader.load();
Scene scene = new Scene(root,348,212);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Flextime calculator");
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("assets/icon.png")));
primaryStage.setResizable(false);
mc = (MainController) loader.getController();
mc.config.initialize();
MainController.java
public UserConfig config = new UserConfig();
#FXML
public void initialize() {
//remove the config.initialize(); from here
}

Couldn't add listener to text field javafx

I am trying to add listener to the textfield in the controler but cudnt do it . i coudnt find any options of textfield in the controler while using the fx:id given to that specific text field.
sample.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.RadioButton?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.effect.Reflection?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="294.0" prefWidth="883.0" style="-fx-background-color: #000000;" stylesheets="#style.css" type="AnchorPane" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controler">
<children>
<Label layoutX="28.0" layoutY="55.0" text="Enter Text : " AnchorPane.leftAnchor="28.0">
<font>
<Font size="15.0" />
</font>
</Label>
<TextField fx:id="msg_tb" alignment="CENTER" layoutX="117.0" layoutY="52.0" prefHeight="25.0" prefWidth="342.0" style="-fx-background-radius: 10;" stylesheets="#style.css" AnchorPane.leftAnchor="117.0" AnchorPane.rightAnchor="424.0" />
<TextField fx:id="n_msg_tb" alignment="CENTER" layoutX="117.0" layoutY="93.0" prefHeight="25.0" prefWidth="342.0" style="-fx-background-radius: 10;" stylesheets="#style.css" AnchorPane.leftAnchor="117.0" AnchorPane.rightAnchor="424.0" />
<Button fx:id="reset_b" alignment="CENTER" layoutX="602.0" layoutY="171.0" mnemonicParsing="false" onAction="#reset" style="-fx-text-fill: #FFFFFF; -fx-background-radius: 20;" stylesheets="#style.css" text="RESET" AnchorPane.rightAnchor="161.0">
<font>
<Font name="Calibri Bold" size="31.0" />
</font>
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
<VBox layoutX="495.0" layoutY="31.0" prefHeight="169.0" prefWidth="167.0" spacing="3.0" stylesheets="#style.css" AnchorPane.rightAnchor="221.0">
<children>
<RadioButton fx:id="er" mnemonicParsing="false" onAction="#er_action" prefHeight="17.0" prefWidth="92.0" stylesheets="#application.css" text="ENCRIPTION" textFill="#797979">
<toggleGroup>
<ToggleGroup fx:id="group1" />
</toggleGroup>
</RadioButton>
<AnchorPane fx:id="ebox" prefHeight="150.0" prefWidth="167.0">
<children>
<Label contentDisplay="RIGHT" graphicTextGap="20.0" layoutX="7.0" layoutY="2.0" prefHeight="25.0" prefWidth="131.0" stylesheets="#application.css" text="NUMBER LETTER">
<graphic>
<Button minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#e_nl" prefHeight="15.0" prefWidth="15.0" text="*">
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Label contentDisplay="RIGHT" graphicTextGap="60.0" layoutX="8.0" layoutY="27.0" prefHeight="25.0" prefWidth="131.0" stylesheets="#application.css" text="AT-BASH">
<graphic>
<Button graphicTextGap="0.0" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#e_at" prefHeight="15.0" prefWidth="15.0" text="Button">
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Label contentDisplay="RIGHT" graphicTextGap="20.0" layoutX="8.0" layoutY="52.0" prefHeight="25.0" prefWidth="105.0" stylesheets="#style.css" text="CEASER">
<graphic>
<TextField fx:id="e_key_tb" onAction="#e_c_key_tb" prefHeight="25.0" prefWidth="40.0" promptText="KEY" style="-fx-background-radius: 10;" stylesheets="#style.css" />
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Button alignment="CENTER" disable="true" layoutX="110.0" layoutY="67.0" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#e_c" prefHeight="25.0" prefWidth="40.0" style="-fx-background-radius: 10;" stylesheets="#application.css" text="CEASER" textAlignment="CENTER">
<font>
<Font name="System Italic" size="8.0" />
</font>
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</children>
</AnchorPane>
</children>
</VBox>
<VBox layoutX="662.0" layoutY="31.0" prefHeight="169.0" prefWidth="167.0" spacing="3.0" stylesheets="#style.css" AnchorPane.rightAnchor="54.0">
<children>
<RadioButton fx:id="dr" mnemonicParsing="false" onAction="#dr_action" prefHeight="17.0" prefWidth="98.0" stylesheets="#application.css" text="DECRIPTION" textFill="#797979" toggleGroup="$group1" />
<AnchorPane fx:id="dbox" prefHeight="150.0" prefWidth="167.0" visible="false">
<children>
<Label contentDisplay="RIGHT" graphicTextGap="20.0" layoutX="7.0" layoutY="2.0" prefHeight="25.0" prefWidth="131.0" stylesheets="#application.css" text="NUMBER LETTER">
<graphic>
<Button minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="15.0" prefWidth="15.0" text="*">
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Label contentDisplay="RIGHT" graphicTextGap="60.0" layoutX="8.0" layoutY="27.0" prefHeight="25.0" prefWidth="131.0" stylesheets="#application.css" text="AT-BASH">
<graphic>
<Button graphicTextGap="0.0" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="15.0" prefWidth="15.0" text="Button">
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Label contentDisplay="RIGHT" graphicTextGap="20.0" layoutX="8.0" layoutY="52.0" prefHeight="25.0" prefWidth="105.0" stylesheets="#style.css" text="CEASER">
<graphic>
<TextField fx:id="d_key_tb" onAction="#d_c_key_tb" prefHeight="25.0" prefWidth="40.0" promptText="KEY" style="-fx-background-radius: 10;" stylesheets="#style.css" />
</graphic>
<padding>
<Insets top="15.0" />
</padding>
</Label>
<Button disable="true" layoutX="110.0" layoutY="67.0" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#d_c" prefHeight="25.0" prefWidth="40.0" style="-fx-background-radius: 10;" stylesheets="#application.css" text="CEASER" textAlignment="CENTER">
<font>
<Font name="System Italic" size="8.0" />
</font>
<effect>
<Reflection fraction="0.41" topOffset="0.65" topOpacity="0.73" />
</effect>
</Button>
</children>
</AnchorPane>
</children>
</VBox>
</children>
</fx:root>
controler.java
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
public class controler implements Initializable {
#FXML
private TextField msg_tb;
#FXML
private TextField n_msg_tb;
#FXML
private Button reset_b;
#FXML
private RadioButton er;
#FXML
private ToggleGroup group1;
#FXML
private RadioButton dr;
#FXML
private AnchorPane ebox;
#FXML
private AnchorPane dbox;
#FXML
public TextField e_key_tb;
#FXML
private TextField d_key_tb;
#FXML
void d_c(ActionEvent event) {
}
#FXML
void d_c_key_tb(ActionEvent event) {
}
#FXML
void dr_action(ActionEvent event) {
dbox.setVisible(true);
dbox.setDisable(false);
ebox.setVisible(false);
ebox.setDisable(true);
}
#FXML
void e_at(ActionEvent event) {
}
#FXML
void e_c(ActionEvent event) {
}
#FXML
void e_c_key_tb(ActionEvent event) {
}
#FXML
void e_nl(ActionEvent event) {
}
#FXML
void er_action(ActionEvent event) {
ebox.setVisible(true);
ebox.setDisable(false);
dbox.setVisible(false);
dbox.setDisable(true);
}
#FXML
void reset(ActionEvent event) {
}
#Override
public void initialize(URL location, ResourceBundle resources) {
ArrayList<morse> mkey = new ArrayList<>();
try {
morse.load(mkey);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public TextField getMsg_tb() {
return msg_tb;
}
public void setMsg_tb(TextField msg_tb) {
this.msg_tb = msg_tb;
}
public TextField getN_msg_tb() {
return n_msg_tb;
}
public void setN_msg_tb(TextField n_msg_tb) {
this.n_msg_tb = n_msg_tb;
}
public Button getReset_b() {
return reset_b;
}
public void setReset_b(Button reset_b) {
this.reset_b = reset_b;
}
public RadioButton getEr() {
return er;
}
public void setEr(RadioButton er) {
this.er = er;
}
public ToggleGroup getGroup1() {
return group1;
}
public void setGroup1(ToggleGroup group1) {
this.group1 = group1;
}
public RadioButton getDr() {
return dr;
}
public void setDr(RadioButton dr) {
this.dr = dr;
}
public AnchorPane getEbox() {
return ebox;
}
public void setEbox(AnchorPane ebox) {
this.ebox = ebox;
}
public AnchorPane getDbox() {
return dbox;
}
public void setDbox(AnchorPane dbox) {
this.dbox = dbox;
}
public TextField getE_key_tb() {
return e_key_tb;
}
public void setE_key_tb(TextField e_key_tb) {
this.e_key_tb = e_key_tb;
}
public TextField getD_key_tb() {
return d_key_tb;
}
public void setD_key_tb(TextField d_key_tb) {
this.d_key_tb = d_key_tb;
}
e_key_tb.textProperty().addListener((obs, oldText, newText) -> {
System.out.println("Text changed from "+ oldText +" to "+newText);
});
}
}
when I am trying to add the listener at the end it is not working. I can't get any options for textfield when I do e_key_tb. and ctrl+space where it is supposed to give a drop down box with a bunch of suggestions ... I used the scene-builder to provide id and copied the controller skeliton from the scene-builder ... later when I'm trying to use the textfield it made me create getters and setters for all the textfields can you explain me y it s happening and I never used listeners before.
Here is a short demonstration of adding a listener to a TextField. This can also serve as an example for mcve for the issue:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.TextField?>
<HBox prefHeight="75.0" prefWidth="150.0" alignment="CENTER"
xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tests.Controler">
<children>
<TextField fx:id="msg_tb" prefHeight="25.0" prefWidth="100.0" style="-fx-background-radius: 10;" />
</children>
</HBox>
The controller :
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
public class Controler implements Initializable {
#FXML
private TextField msg_tb;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
msg_tb.textProperty().addListener((obs, oldText, newText) -> {
System.out.println("Text changed from "+ oldText +" to "+newText);
});
}
}
Test it:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class FxmlMain extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Pane root = FXMLLoader.load(getClass().getResource("xml/FxmlMain.fxml"));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) { launch(args);}
}

Why is the Capitalization delayed in this JavaFx App when using change listener?

I have this simple app on JavaFx that creates lazy instanziators for javafx beans, the app is entirely gui, it only has an fxml and main class
<?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.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.SuperLazyfxController">
<children>
<StackPane layoutX="3.0" prefHeight="400.0" prefWidth="594.0">
<children>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#0f5694" height="392.0" stroke="BLACK" strokeType="INSIDE" width="594.0" />
<AnchorPane prefHeight="344.0" prefWidth="594.0">
<children>
<VBox alignment="CENTER" layoutX="38.0" layoutY="59.0" prefHeight="144.0" prefWidth="459.0" spacing="10.0" AnchorPane.bottomAnchor="134.0" AnchorPane.leftAnchor="38.0" AnchorPane.rightAnchor="67.0" AnchorPane.topAnchor="59.0">
<children>
<HBox prefHeight="29.0" prefWidth="537.0">
<children>
<Label alignment="CENTER_RIGHT" prefHeight="65.0" prefWidth="262.0" text="Accesibilidad Property :" textFill="#ae9c3e">
<font>
<Font name="Ebrima Bold" size="18.0" />
</font>
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Label>
<TextField fx:id="accesib" prefHeight="72.0" prefWidth="277.0" />
</children>
</HBox>
<HBox prefHeight="29.0" prefWidth="537.0">
<children>
<Label alignment="CENTER_RIGHT" prefHeight="65.0" prefWidth="262.0" text="TipoProperty" textFill="#ae9c3e">
<font>
<Font name="Ebrima Bold" size="18.0" />
</font>
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Label>
<TextField fx:id="Tipo" prefHeight="72.0" prefWidth="277.0" />
</children>
</HBox>
<HBox prefHeight="29.0" prefWidth="537.0">
<children>
<Label alignment="CENTER_RIGHT" contentDisplay="RIGHT" prefHeight="29.0" prefWidth="261.0" text="Nombre Property :" textFill="#ae9c3e">
<font>
<Font name="Ebrima Bold" size="18.0" />
</font>
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Label>
<TextField fx:id="nombre" onKeyReleased="#mayusculas" prefHeight="72.0" prefWidth="277.0" />
</children>
</HBox>
<HBox prefHeight="29.0" prefWidth="537.0">
<children>
<Label alignment="CENTER_RIGHT" contentDisplay="RIGHT" prefHeight="29.0" prefWidth="261.0" text="Shadowfield type" textFill="#ae9c3e">
<font>
<Font name="Ebrima Bold" size="18.0" />
</font>
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Label>
<TextField fx:id="tiposhadow" prefHeight="72.0" prefWidth="277.0" />
</children>
</HBox>
<Button alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" onAction="#Clean" text="Clean" textFill="#907c39">
<font>
<Font name="System Bold" size="13.0" />
</font>
</Button>
</children>
</VBox>
<TextArea fx:id="area" layoutX="15.0" layoutY="267.0" prefHeight="118.0" prefWidth="563.0" AnchorPane.bottomAnchor="15.0" AnchorPane.leftAnchor="15.0" AnchorPane.rightAnchor="16.0" AnchorPane.topAnchor="267.0" />
<Label alignment="CENTER" layoutX="139.0" prefHeight="62.0" prefWidth="321.0" text="Super-Lazy Insta JavaFX" textFill="RED" AnchorPane.bottomAnchor="338.0" AnchorPane.leftAnchor="139.0" AnchorPane.rightAnchor="134.0" AnchorPane.topAnchor="0.0">
<font>
<Font name="System Bold" size="25.0" />
</font>
</Label>
</children>
</AnchorPane>
</children>
</StackPane>
</children>
</AnchorPane>
Here the main class:
package app;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("SuperLazyfx.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Lazy-instanziatior");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
and here is the controller class
package app;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
public class SuperLazyfxController implements Initializable {
#FXML
private TextField Tipo;
#FXML
private TextField nombre;
#FXML
private TextField tiposhadow;
#FXML
private TextField accesib;
#FXML
private TextArea area;
private StringBinding sbind;
private StringProperty nombreMayus = new SimpleStringProperty("Nombrevar");
private StringProperty nombreMin = new SimpleStringProperty("");
#Override
public void initialize(URL url, ResourceBundle rb) {
sbind = new StringBinding() {
{
super.bind(Tipo.textProperty(), nombre.textProperty(), tiposhadow.textProperty(), accesib.textProperty());
}
#Override
protected String computeValue() {
return accesib.textProperty().get() + " " + Tipo.textProperty().get() + " " + nombre.textProperty().get() + ";\n"
+ accesib.textProperty().get() + " " + tiposhadow.textProperty().get() + " _" + nombre.textProperty().get() + ";\n\n"
+ "public " + tiposhadow.textProperty().get() + " get" + nombreMayus.get() + nombreMin.get()+"(){"
+ "\n\t return ("+nombre.textProperty().get()+" ==null)? _"+nombre.textProperty().get()+": "+nombre.textProperty().get()+".get();\n"
+"}\n\n"
+ "public void set"+ nombreMayus.get() + nombreMin.get()+"("+tiposhadow.textProperty().get()+" "+nombre.textProperty().get()+"){"
+ "\t\nif(this."+nombre.textProperty().get()+"==null){\n"
+ "\t_"+nombre.textProperty().get()+"="+nombre.textProperty().get()+";\n"
+ "\t} else{\n"
+ "\tthis."+nombre.textProperty().get()+".set("+nombre.textProperty().get()+");\n"
+ "\t}\n"
+ "}\n\n"
+ "public "+ Tipo.textProperty().get()+" "+nombre.textProperty().get()+"Property(){\n"
+ "\treturn ("+nombre.textProperty().get()+"== null)? "+nombre.textProperty().get()+"= new Simple"+Tipo.textProperty().get()+"(this,\""+nombre.textProperty().get()+"\", _"+nombre.textProperty().get()+"): "+nombre.textProperty().get()+";\n"
+ "}"
;
}
};
area.textProperty().bind(sbind);
}
#FXML
private void mayusculas(KeyEvent event) {
nombre.textProperty().addListener((ov, oldV, newV) -> {
if (!(nombre.textProperty().get().isEmpty())) {
nombreMayus.set(newV.substring(0, 1).toUpperCase());
} else {
nombreMayus.set("Nombrevar");
}
});
nombre.textProperty().addListener((ov, oldV, newV) -> {
if (!(nombre.textProperty().get().isEmpty())) {
nombreMin.set(newV.substring(1));
} else {
nombreMin.set("");
}
});
}
#FXML
private void Clean(ActionEvent event) {
Tipo.textProperty().set("");
nombre.textProperty().set("");
tiposhadow.textProperty().set("");
accesib.textProperty().set("");
}
}
The problem falls here:
#FXML
private void mayusculas(KeyEvent event) {
nombre.textProperty().addListener((ov, oldV, newV) -> {
if (!(nombre.textProperty().get().isEmpty())) {
nombreMayus.set(newV.substring(0, 1).toUpperCase());
} else {
nombreMayus.set("Nombrevar");
}
});
nombre.textProperty().addListener((ov, oldV, newV) -> {
if (!(nombre.textProperty().get().isEmpty())) {
nombreMin.set(newV.substring(1));
} else {
nombreMin.set("");
}
});
}
And here:
+ "public void set"+ nombreMayus.get() + nombreMin.get()+"("+tiposhadow.textProperty().get()+" "+nombre.textProperty().get()+"){"
the nombreMayus.get() shows, when the first letter is typed "Nombrevar" on the Text Area while when the second one is typed it shows the first letter you see!!! It is delayed
It is not correct to add change listener in onKeyReleased() event. This way you are adding a brand new listener every single time the user keys something on the keyboard.
You can probably do this:
nombreMayus.bind(() -> nombre.getText().substring(0, 1).toUpperCase(), nombre.textProperty());
nombreMin.bind(() -> nombre.getText().substring(1), nombre.textProperty());

Categories