I'm new in Java and Java FX and I'm trying to make a panel with buttons using scene builder. I want my application to respond only on arrow key pressed. I made the following method in my Controller class:
public void keyPressed(KeyEvent key) {
switch(key.getCode()) {
...some code here
}
}
After that I selected this method in scene builder, but when I run my application nothing happens when I press an arrow key.
Can somebody help me?
Without seeing the rest of your code and FXML it is difficult to tell, here is full example
Possible things you missed
Adding keyPress as an action in the FXML
Adding the #FXML annotation to the keyPressed() method
Code
public class Main extends Application {
private class Controller {
#FXML // <== perhaps you had this missing??
void keyPressed(KeyEvent event) {
switch (event.getCode()) {
case LEFT:
case KP_LEFT:
System.out.println("to the left");
break;
case RIGHT:
case KP_RIGHT:
System.out.println("to the right");
break;
default:
break;
}
}
}
#Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/foo.fxml"));
loader.setController(new Controller());
primaryStage.setScene(new Scene(loader.load()));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane onKeyPressed="#keyPressed" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button mnemonicParsing="false" text="Button" />
</children>
</GridPane>
KeyCode also allows to compare with a specific key
#FXML
private void keyPressed(KeyEvent keyEvent)
if (keyEvent.getCode() == KeyCode.ENTER) {
// do some actions
}
}
You can get all the key code from here. Without using switch it's one of the good way.
KeyCode has a method isArrowKey(), so if you're calling your keyPressed method from your event handler, you could do:
public void keyPressed(KeyEvent key){
if(key.getCode().isArrowKey()){
...some code here
}
}
If you need to do different things based on which arrow key is pressed, make sure your switch cases are comparing to KeyCode.UP/DOWN/LEFT/RIGHT. If they are, it's likely that either you didn't set the event handler properly or your GUI is hung because of a threading problem. Post where your handling the event if you need more help.
Related
I'm trying to write my first application with javaFX and FXML files, but I got stuck with accesing variables of the FXMLs.
I am able to start the program with the first scene (first FXML). There is a button I'm able to interact with, and the first scene hides and the second scene appears. In that scene, the user has to insert a value in a text field. I'm able to save this into a variable by confirming this input over another button.
This button click hides the second scene and opens the first scene again. Now I want to set the value of a label in that first scene to the value of the variable I saved the user's input in. But this throws an error message.
It seems like this label value is not accessable, even if this label is part of the currently loaded FXML, but I am able to get the value of a label of the second FXML (user interaction field), which isn't visible anymore.
Can anyone help me, how to make this Label of the first FXML readable and changable?
My code is quite long, but I tired to give you the most important parts:
#FXML
private Label RollsMax;
#FXML
private TextField roll_input;
#FXML
private String amountRolls;
... // several more declarations
#FXML
void btn_confirmInput_onClick(ActionEvent event){ //btn_confirmInput is part of scene2.fxml
event.consume();
amountRolls=roll_input.getText();
System.out.println(amountRolls); // here I get the result of the user's input
try{
root=FXMLLoader.load(getClass().getResource("project/scene1.fxml"));
stage=(Stage)((Node)event.getSource()).getScene().getWindow();
scene=new Scene(root);
stage.setScene(scene);
stage.show();
} //end try
catch (IOException e){
e.printStackTrace();
} //end catch
RollsMax.setText(amountRolls); // THIS LINE IS NOT WORKING AS RollsMax SEEMS NOT AVAILABLE, EVEN IF IT IS PART OF SCENE1.fxml
} // end btn_confirmInput_onClick()
Any ideas? The error I get is 'java.lang.RuntimeException: java.lang.reflect.InvocationtargetException' as well as a'java.lang.NullPointerException'.
Thanks a lot!
Endcoder
To share data between the scenes, we'll introduce a Model class that holds the shared information:
public class Model {
private SimpleStringProperty valueProperty = new SimpleStringProperty("N/A");
public SimpleStringProperty getValueProperty() {
return valueProperty;
}
}
And an interface to be implemented by the controllers of the two scenes (actually the controllers of the two roots of the two scenes).
The interface adds the functionality of injection a Model into the controller:
public interface Controller {
void setModel(Model model);
}
sceneOne.fxml with a button to switch scene and a label to display user's input:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="300.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/16"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="SceneOneController">
<children>
<Label layoutX="159.0" layoutY="40.0" text="Scene 1">
<font>
<Font size="24.0" />
</font>
</Label>
<Button layoutX="264.0" layoutY="246.0" mnemonicParsing="false" onAction="#changeScene" prefHeight="26.0" prefWidth="102.0" text="Get Input" />
<Label fx:id="inputValue" layoutX="187.0" layoutY="142.0" text="Label" />
</children>
</AnchorPane>
And its controller which implements the Controller interface:
public class SceneOneController implements Controller {
#FXML Label inputValue;
private Model model;
public void changeScene(ActionEvent e) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("sceneTwo.fxml"));
Parent root = loader.load();
Controller controller = loader.getController(); //get a reference to sceneTwo controller
controller.setModel(model);
Stage stage = (Stage)((Node)e.getSource()).getScene().getWindow();
stage.setScene(new Scene(root));
} catch (Exception ex) {
ex.printStackTrace();
}
}
#Override
public void setModel(Model model) {
this.model = model;
if(model != null){
inputValue.textProperty().unbind();
inputValue.textProperty().bind(model.valueProperty);
}
}
}
sceneTwo.fxml with a button to switch scene and a TextField for the user's input:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane fx:id="main" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SceneTwoController">
<children>
<Label layoutX="150.0" layoutY="41.0" text="Scene 2">
<font>
<Font size="24.0" />
</font>
</Label>
<Button layoutX="264.0" layoutY="246.0" mnemonicParsing="false" onAction="#changeScene" prefHeight="26.0" prefWidth="102.0" text="Update" />
<TextField fx:id="inputValue" layoutX="121.0" layoutY="138.0" prefHeight="25.0" prefWidth="162.0" />
</children>
</AnchorPane>
And its controller:
public class SceneTwoController implements Controller {
#FXML TextField inputValue;
private Model model;
public void changeScene(ActionEvent e) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("sceneOne.fxml"));
Parent root = loader.load();
Controller controller = loader.getController(); //get a reference to sceneOne controller
controller.setModel(model);
Stage stage = (Stage)((Node)e.getSource()).getScene().getWindow();
stage.setScene(new Scene(root));
} catch (Exception ex) {
ex.printStackTrace();
}
}
#Override
public void setModel(Model model) {
this.model = model;
if(model != null){
inputValue.textProperty().unbind();
model.valueProperty.bind(inputValue.textProperty());
}
}
}
And finally the application to test it all:
public class SwitchSceneMVC extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("sceneOne.fxml"));
Parent root = loader.load();
Model model = new Model();
Controller controller = loader.getController();
controller.setModel(model);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(null);
}
}
Use a controller class for every FXML. Then you should be able to interact between them with public methods.
Especially when just hidinng the first scene you should then be able to just write from one scene to the other one.
Alternatively: while starting the second scene make a parameter where you bring the instance of the first scene. Than you should be able to use the needed methods on the first scenes instance.
Like
Code Scene one
#FXML
private void startSceneTwoButton(){
new SceneTwo(this);
}
Code Scene two
FirstScene scene;
public SecondScene(FirstScene scene){
this.scene = scene;
//some code
}
#FXML
private void button(){
scene.enterValue(value);
}
I am working on a project involves RFID technology, what i am trying to do at the moment, once the RFIDTagListener method runs, I basically pass a tag to a reader which its serial number will be sent to a server to get some relevant data back and pop them on a GUI screen. what I have done so far is getting the data upon sending reader's data manually without passing a tag becasue I don't know how to do it otherwise and here is the problem. where I am working on a javafx project and when I tried to put the RFIDTagListener method within the MainController class and on compiling, the taglistner method wouldn't be triggered and just ignored, it's only the GUI screen will be opened.However, I also tried to have RFIDTagListener within the main class but on compiling, the taglistner method would be run first and when it's finished in 5 seconds, my GUI window will be opened next. SO I Don't know where this method should be located exactly. Essentially What I want is to have them both running at the same time, the taglistener running in the background with the GUI window opened simulitniously.
Any recommendation guys would be much appricated.
MainController class :
public class MainController {
RFID rfid = new RFID();
String ReaderNo = null;
String walletJson = new String();
Gson gson = new Gson();
public static String sensorServerURL = "http://localhost:8080/PhidgetServer2019/SensorServerRFIDdata";
walletDAO dao = new walletDAO();
ArrayList<wallet> allwallets = new ArrayList<wallet>();
#FXML VBox ConsultHR;
#FXML private Label message;
#FXML private Label WalletName;
#FXML private ListView<ArrayList<wallet>> list;
#FXML private ListView<ArrayList<wallet>> RoomAList;
#FXML private TableView<wallet> tableViewData;
#FXML private TableColumn<wallet, String> NameColumn;
#FXML private TableColumn<wallet, String> LocationColumn;
#FXML private TableColumn<wallet, String> TagColumn;
public void getTags(ActionEvent event) throws SQLException {
allwallets = dao.getWalletTag();
try {
allwallets = dao.getWalletTag();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(allwallets);
list.getItems().add(allwallets);
}
public MainController() throws PhidgetException {
// Make the RFID Phidget able to detect loss or gain of an rfid card
rfid.addTagListener(new RFIDTagListener() {
// What to do when a tag is found
public void onTag(RFIDTagEvent e) {
try {
ReaderNo = String.valueOf(rfid.getDeviceSerialNumber());
} catch (PhidgetException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("Reader serial number is " + ' '+ReaderNo);
wallet walletData = new wallet("385055");
walletJson = gson.toJson(walletData);
String resultfromserver = sendToServer(walletJson);
System.out.println("DEBUG: data in json : " +resultfromserver);
wallet walletObject = gson.fromJson(resultfromserver, wallet.class);
System.out.println("DEBUG: The wallet's Data: "+' '+ walletObject);
WalletName.setText(walletObject.getWalletName());
}
});
rfid.addTagLostListener(new RFIDTagLostListener() {
// What to do when a tag is lost
public void onTagLost(RFIDTagLostEvent e) {
// optional print, used as debug here
System.out.println("DEBUG: Tag lost: " + e.getTag());
}
});
}
}
Main class :
public class Main extends Application {
//RFID rfid = new RFID();
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Wallet locator !");
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
public static void main(String[] args) throws PhidgetException {
new MainController();
launch(args);
}
}
Main.FXML :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="269.0" prefWidth="403.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<Button layoutX="149.0" layoutY="251.0" mnemonicParsing="false" onAction="#getTags" prefHeight="46.0" prefWidth="82.0" text="tags" />
<Label fx:id="message" layoutX="139.0" layoutY="209.0" prefHeight="35.0" prefWidth="101.0" />
<ListView id="studentObservableList" fx:id="list" layoutY="209.0" prefHeight="131.0" prefWidth="139.0" />
<TableView fx:id="tableViewData" prefHeight="200.0" prefWidth="231.0" style="-fx-border-color: red;">
<columns>
<TableColumn fx:id="NameColumn" prefWidth="75.0" text="Name"/>
<TableColumn fx:id="LocationColumn" prefWidth="75.0" text="Location" />
<TableColumn fx:id="TagColumn" prefWidth="75.0" text="Tag" />
</columns>
</TableView>
<Button layoutX="251.0" layoutY="14.0" mnemonicParsing="false" onAction="#getWallets" prefHeight="56.0" prefWidth="82.0" text="wallets" />
</AnchorPane>
1st point
The main method is inside the MainController class and it's instantiating itself, which is possible but not conventional.
2nd point
Never put your main method into a controller! Put it into your Main class instead.
3rd point
You must define the application launch inside your main method, by calling Application.launch(). This method will call your overriden start method (among other things) and display the GUI:
public static void main(String[] args) throws PhidgetException {
launch(args);
new MainController();
}
4th point
You don't need to instantiate directly a controller in JavaFX. With your current code you're creating a MainController instance which is unlinked to your GUI; that's why you can see your GUI without interacting with it. You need to remove the new MainController(); line into your main method, then check if the fx:controller attribute is defined in the root of your FXML file.
You can call this controller instance using root.getController() in your start method.
How to know which button has envoked the function. I have read other answers on stackoverflow like this one . I tried creating a new button and giving it a value of event.getSource() but it is not working
#FXML
Button v1;
#FXML
Button v2;
#FXML
Button v3;
#FXML
Button v4;
#FXML
Button v5;
#FXML
Button v6;
public void printButton(ActionEvent event){
Button sourceButton = (Button) event.getSource();
if(sourceButton == v1){
System.out.print("v1");
}
else if(sourceButton == v2){
System.out.print("v2");
}
else if(sourceButton == v3){
System.out.print("v3");
}
else if(sourceButton == v4){
System.out.print("v4");
}
else if(sourceButton == v5){
System.out.print("v5");
}
else if(sourceButton == v6){
System.out.print("v6");
}
}
I have created the button in fxml and it calls the same function printButton();
This answer is using java 8 update 211 for testing.
The comments are suggesting that changing == to .equals() was the solution to this. However, Button does not override .equals(), so both of those ways are doing effectively the same thing.
Running up the sample application below to test resulted in all of the 3 buttons working as expected. Therefore, there may have been something incorrect in the FXML file with OP's code, which (as I write this) has not been shown from OP.
In the example below, note that the fxml file:
Specifies the controller with fx:controller="sample.Controller"
Contains 3 buttons with their ids matching exactly to the ones declared in Controller
On each button, includes onAction="#printButton" , and the name in quotes matches the method name in Controller onAction="#printButton".
Please note all of these are within the same package.
Main.java:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller.java
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class Controller {
#FXML
Button v1;
#FXML
Button v2;
#FXML
Button v3;
public void printButton(ActionEvent event){
Button sourceButton = (Button) event.getSource();
if(sourceButton.equals(v1)){
System.out.print("v1");
}
else if(sourceButton == v2){
System.out.print("v2");
}
else if(sourceButton == v3){
System.out.print("v3");
}
}
}
sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="CENTER" spacing="10.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
</padding>
<HBox alignment="CENTER" spacing="10.0">
<Button fx:id="v1" mnemonicParsing="false" onAction="#printButton" text="Button 1"/>
<Button fx:id="v2" mnemonicParsing="false" onAction="#printButton" text="Button 2"/>
<Button fx:id="v3" mnemonicParsing="false" onAction="#printButton" text="Button 3"/>
</HBox>
<Label text="Source:"/>
<Label fx:id="lblSource"/>
</VBox>
make your life easy how about using isPressed() function ?
if( v1.isPressed() ) {
name2 = n1.getText();
System.out.println(" V1 got called ");
}
v1.isPressed(); means check whether v1 has been clicked or not it return true or false
i'm not sure about also v1.isfire(); I think this one can make auto click
This question already has an answer here:
Handle event on disabled Node
(1 answer)
Closed 4 years ago.
Hello I have two problems.
First one:
When I start my app, I can press my ToggleButton with spacebar.
Second one:
(Even without ToggleButton) My EventHandler on my button is not working, on pressing spacebar nothing is going on.
Main class:
public class Main extends Application {
#Override
public void start(Stage stage) throws IOException {
Parent parent = (Parent) FXMLLoader.load(getClass().getResource("/application/MainView.fxml"));
Scene scene = new Scene(parent);
scene.getRoot().setFocusTraversable(true);
stage.setScene(scene);
stage.setTitle("Login Page");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Fxml file:
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="501.0" prefWidth="597.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<children>
<Button fx:id="button" layoutX="136.0" layoutY="184.0" mnemonicParsing="false" prefHeight="133.0" prefWidth="127.0" text="Button" />
<Label fx:id="label" layoutX="199.0" layoutY="106.0" prefHeight="61.0" prefWidth="198.0" text="Label" />
<ToggleButton fx:id="toggleButton" layoutX="318.0" layoutY="186.0" mnemonicParsing="false" prefHeight="133.0" prefWidth="127.0" text="ToggleButton" />
</children>
</AnchorPane>
Controller class:
public class MainController implements Initializable {
#FXML
private Button button;
#FXML
private Label label;
#FXML
private ToggleButton toggleButton;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
button.setDisable(true);
button.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.SPACE) {
System.out.println("space pressed");
button.setDisable(false);
}
}
});
button.addEventFilter(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.SPACE) {
System.out.println("space pressed");
button.setDisable(true);
}
}
});
}
}
At first as #c0der mentioned you hasn't been assined event handler
as the button is disabled via button.setDisabled(true) so it can't receive any events such as mouse and key events see that javafx doc
Edit with some search i found another similar question in stackoverflow
How i can do button action for editing TableView. I need to put text from TextArea to table when i touch button. And if put System.out.println in inputToTable() it is work.
public class InputController {
public TextArea inputArea;
public Button inputButton;
private TableController tableController;
public void initialize() {
tableControllerInit();
}
public void inputToTable() {
if(inputArea.getText() != "") {
tableController.tableInfo.setItems(FXCollections.observableArrayList(new InputObject(inputArea.getText())));
}
}
private void tableControllerInit() {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("table.fxml"));
fxmlLoader.load();
tableController = fxmlLoader.getController();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class TableController {
#FXML TableView<InputObject> tableInfo;
#FXML TableColumn<InputObject, String> col1;
public void initialize() {
col1.setCellValueFactory(new PropertyValueFactory<>("text"));
}
}
public class Controller implements Initializable {
#Override
public void initialize(URL location, ResourceBundle resources) {
}
}
public class InputObject {
String text;
public InputObject(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
<BorderPane fx:controller="sample.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<left>
<fx:include source="table.fxml"/>
</left>
<center>
<fx:include source="input.fxml"/>
</center>
</BorderPane>
<TableView fx:controller="sample.TableController" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:id="tableInfo" prefHeight="400.0" prefWidth="330.0">
<columns>
<TableColumn fx:id="col1" prefWidth="75.0" text="Output" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
<VBox fx:controller="sample.InputController" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" alignment="TOP_CENTER" prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
<children>
<TextArea fx:id="inputArea" prefHeight="188.0" prefWidth="270.0" />
<Button fx:id="inputButton" onAction="#inputToTable" mnemonicParsing="false" text="Input">
<VBox.margin>
<Insets bottom="30.0" left="30.0" right="30.0" top="30.0" />
</VBox.margin>
</Button>
</children>
</VBox>
You load table.fxml twice: once via the fx:include in the main FXML file, and once in InputController, via the FXMLLoader you create in the tableControllerInit() method. Consequently, two instances of TableController are created, one associated with the first UI you load from table.fxml, and one associated with the second UI you load from table.fxml.
The UI you load via the fx:include is displayed in the VBox defined in the main FXML file. The UI you load with the FXMLLoader is never displayed (in fact, you never even keep a reference to it, you just call loader.load() and discard the result). When you try to update the table's items (do you really intend to replace all the existing items, by the way?), you refer to the second controller instance, which is associated with the UI which is never displayed. Consequently, you are updating a table that is not displayed, and you never see any results.
What you really need to do is share the same data between the two controllers associated with the two fx:includes. You can do this simply by injecting those two controllers into the main controller, as described in the "Nested Controllers" section in the documentation.
First, give the fx:include elements fx:id attributes:
<BorderPane fx:controller="sample.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<left>
<fx:include fx:id="table" source="table.fxml"/>
</left>
<center>
<fx:include fx:id="input" source="input.fxml"/>
</center>
</BorderPane>
Then you can inject the controllers into the main controller by creating fields with the word "Controller" appended to the fx:id. Create a single observable list, which will represent the list of items displayed in the table, and pass it to each controller:
public class Controller implements Initializable {
#FXML
private TableController tableController ;
#FXML
private InputController inputController ;
#Override
public void initialize(URL location, ResourceBundle resources) {
ObservableList<InputObject> items = FXCollections.observableArrayList();
tableController.setTableItems(items);
inputController.setTableItems(items);
}
}
Finally, just define the obvious methods in each of the other two controllers:
public class TableController {
#FXML
private TableView<InputObject> tableInfo;
#FXML
private TableColumn<InputObject, String> col1;
public void initialize() {
col1.setCellValueFactory(new PropertyValueFactory<>("text"));
}
public void setTableItems(ObservableList<InputObject> tableItems) {
tableInfo.setItems(tableItems);
}
}
Now the table is displaying the contents of the items list created in the main controller's initalize() method, and the InputController has a reference to the same list. So all you need to do is update that list in the InputController. I assume you just want to add items to the table (not replace them all):
public class InputController {
#FXML
private TextArea inputArea;
#FXML
private Button inputButton;
private ObservableList<InputObject> tableItems ;
public void setTableItems(ObservableList<InputObject> tableItems) {
this.tableItems = tableItems ;
}
public void inputToTable() {
if(! inputArea.getText().isEmpty()) {
tableItems.add(new InputObject(inputArea.getText()));
}
}
}
More generally, if you have more data to share among the different controllers, you would create one or more "model" classes and share a model instance with the controllers. Then you can observe the properties of the model and update them. See Applying MVC With JavaFx for a more comprehensive example.