Access directly to object declared into fxml file - java

I'm pretty new to Java FX and fxml templates, I know that to access to an object's methods you need to use setOnAction and declare a specific method into the controller class.
Is there a way to access to its methods from code without using setOnAction?
Example what I want to do:
FXML Document
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<VBox 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" fx:controller="package.Controller">
<children>
<Label fx:id="myLabel" text="Label" />
</children>
</VBox>
Controller class
public class Controller {
#FXML
Label myLabel;
public Controller() {
this.mylabel = new Label();
//some code here
myLabel.setText("modified text 0");
//some code here
myLabel.setText("modified text 1");
}
}
Is that possible in some way by not using setOnAction custom method?

Related

field that is supposed to be assigned by fxml is throwing NullPointerException when accesed from inner class - javaFX

I have a controller for an fxml file. The controller has a field public BorderPane mainBorderPane; which is supposed to be filled with the Border pane of the same fx:id found in the fxml file. When I try to access it from an inner class it gives a NullPointerExcetion
The clearBorderPaneCenter(); and the clearBorderPaneRight work just fine but when I run the cashSceneController.show() it crashes on the line mainBorderPane.setRight(rightLayout);
fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane fx:id="mainBorderPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Program.gui.MainSceneController">
<top>
<Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</top>
<left>
<Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</left>
<center>
<Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
<right>
<Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</right>
<bottom>
<Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</bottom>
</BorderPane>
Simplified version of controller:
public class MainSceneController {
MainSceneController.CashSceneController cashSceneController = new MainSceneController.CashSceneController();
public BorderPane mainBorderPane;
public void switchLayout(byte ID){
//todo Make this work switchScene()
clearBorderPaneCenter();
clearBorderPaneRight();
cashSceneController.show();
}
public void clearBorderPaneRight(){
try {
mainBorderPane.setRight(null);
OutputHelper.log("cleared right of mainBorderPane");
} catch (Exception e){
OutputHelper.log("clearing right of mainBorderPane not required - already cleared");
}
}
public void clearBorderPaneCenter(){
try {
mainBorderPane.setCenter(null);
OutputHelper.log("cleared centre of mainBorderPane");
} catch (Exception e){
OutputHelper.log("clearing centre of mainBorderPane not required - already cleared");
}
}
public class CashSceneController{
VBox rightLayout;
public void show() {
setVBox();
mainBorderPane.setRight(rightLayout);
}
public void setVBox(){
rightLayout = new VBox();
//......
}
}
}
This is how the fxml file is loaded
SceneController = new MainSceneController(new Scene(FXMLLoader.load(getClass().getResource("gui/MainScreen.fxml"))));
I hope that I explained my problem well enough. I'm new to stack overflow and have no idea of what is a good question
Edit: it appears that the problem is caused by the mainBorderPane not being assigned at all. The clearBorderPaneCenter and clearBorderPaneRight seem to not crash cause they are caught by the try catch. Any ideas why the mainBorderPane might not be getting assigned correctly?
You are missing the fxml annotation. It is required for the FXMLLoader to inject the BorderPane into the code
#FXML
public BorderPane mainBorderPane;

JavaFX: Add MapView to VBox

I want to add a MapView from JXMaps to the VBox in JavaFX, i tried to add the MapView to the border panel and then added to the VBox, but i canĀ“t find any solution to this, i hope you can help me, this is my code:
public class ControlerMap
{
#FXML private BorderPane borderPanel;
#FXML private Button butom;
#FXML VBox cosa;
#FXML public void initialize()
{
MapView sample = new MapView();
sample.setMaxHeight(394.0);
sample.setPrefWidth(704.0);
sample.setVisible(true);
borderPanel = new BorderPane(sample);
borderPanel.setVisible(true);
borderPanel.setLayoutX(76.0);
borderPanel.setLayoutY(134.0);
borderPanel.setPrefHeight(200.0);
borderPanel.setPrefWidth(467.0);
cosa.getChildren().add(borderPanel);
}
}
And here is my code from the .FXML file:
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="496.0" prefWidth="757.0"
xmlns="http://javafx.com/javafx/8.0.102" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="prueba.web.googleMap.ControlerMap">
<children>
<Button fx:id="butom" layoutX="25.0" layoutY="14.0"
mnemonicParsing="false" text="Button" />
<VBox fx:id="cosa" layoutX="32.0" layoutY="68.0" prefHeight="394.0"
prefWidth="704.0">
<children>
<BorderPane fx:id="borderPanel" prefHeight="400.0"
prefWidth="704.0" />
</children>
</VBox>
</children>
</AnchorPane>
As I can see you created empty MapView.
MapView sample = new MapView();
Maps will be actually drawn only after initialization of its properties (at least zoom and center). You have to set it in following way:
sample.setOnMapReadyHandler(new MapReadyHandler() {
#Override
public void onMapReady(MapStatus status) {
if (status == MapStatus.MAP_STATUS_OK) {
final Map map = mapView.getMap();
map.setCenter(new LatLng(35.91466, 10.312499));
map.setZoom(2.0);
}
}
});

Java FXML calling methods on button clicks

I am creating a basic game launcher in JavaFX with SceneBuilder. Since SceneBuilder works in FXML, my launcher layout is in FXML. I have a method in my main class that I want to call on a button click. I read that you could use
#methodName
In the button's
onAction
property, but this does not work.
My main Java class:
#FXML
private void launchGame(ActionEvent e) {
System.out.println("Launching...");
}
#Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(Main.class.getResource("launcher.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("First Week Login");
primaryStage.setResizable(false);
primaryStage.sizeToScene();
primaryStage.show();
}
My FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.web.WebView?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/8.0.102">
<children>
<BorderPane prefHeight="493.0" prefWidth="664.0" styleClass="background"
stylesheets="#launcher.css">
<bottom>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0"
BorderPane.alignment="CENTER">
<children>
<Button alignment="CENTER" mnemonicParsing="false"
text="Launch Game" onAction="#launchGame" />
</children>
</HBox>
</bottom>
<top>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0"
BorderPane.alignment="CENTER">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0"
styleClass="title" text="First Week" />
</children>
</HBox>
</top>
<center>
<WebView prefHeight="200.0" prefWidth="200.0"
BorderPane.alignment="CENTER" />
</center>
</BorderPane>
</children>
</AnchorPane>
You need to create a separate Controller class and specify it in top AnchorPane tag with fx:controller="packageName.Classname"
Like this:
<AnchorPane xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/8.0.102"
fx:controller="com.packageName.Controller">
The called method should be inside the specified Controller class.
com.packageName is just an example, you should use the name of the package where you put the Controller class or no package name if it's not in any package.

JavaFX - Adding buttons dynamically to fxml created VBox

I am trying to add x number of buttons to a VBox located in a scrollpane.
The Vbox, and scrollpane are made with scenebuilder.
However i can't seem to connect the VBox to the controller as a variable.
The piece of code that spawns the scene is the following:
Stage stage = MyMain.stage;
stage.setScene(new Scene(FXMLLoader.load(getClass().getResource("/main/fxml/catEdit.fxml"))));
stage.show();
My FXML is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<BorderPane 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"
fx:controller="main.controllers.CatEditCon">
<center>
<ScrollPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0">
<children>
<VBox fx:id="catBox" alignment="CENTER" prefHeight="200.0" prefWidth="100.0" />
</children></AnchorPane>
</content>
</ScrollPane>
</center>
</BorderPane>
And the controller is:
package main.controllers;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import main.MyMain;
import main.database.master.Category;
import main.fxml.custom.CatBut;
public class CatEditCon{
#FXML public VBox catBox;
public CatEditCon(){
catBox = new VBox();
updateScroll();
}
public void updateScroll(){
Category cat = MyMain.baseData.getCategories();
System.out.println(catBox);
int len = MyMain.baseData.getArticles().size();
for(int i = 0 ; i < len; i++ ){
System.out.println(""+i);
Button b = new Button();
catBox.getChildren().add(b);
}
}
}
Any tips to what i do wrong?
The scene opens, and the sysout counts upwards as it should. But no buttons appear?
If the catBox isnt initialized in the constructor, i get a nullPionter to that object. So it is as if i don't make the connection to the fxml object.

Adding columns to a Table view when initialized in FXML

I am trying to add columns to a table view like this:
#FXML
private TableView tblView;
//private Label tblViewTitle;
#FXML
public void showTable(ActionEvent e) throws Exception{
Stage TableView = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("tableView.fxml"));
TableView.setTitle("Table");
TableView.setScene(new Scene(root));
//tblViewTitle.setText("test");
//For dynamic calculate later on
TableColumn clm1 = new TableColumn("Test");
TableColumn clm2 = new TableColumn("Test");
TableColumn clm3 = new TableColumn("Test");
TableColumn clm4 = new TableColumn("Test");
tblView.getColumns().addAll(clm1, clm2, clm3, clm4);
TableView.show();
}
The fxml file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?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.101" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<TableView fx:id="tblView" editable="true" layoutX="84.0" layoutY="94.0" prefHeight="306.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="94.0" />
<Label fx:id="tblViewTitle" layoutX="14.0" layoutY="26.0" prefHeight="49.0" prefWidth="181.0">
<font>
<Font size="39.0" />
</font>
</Label>
</children>
</AnchorPane>
I'm getting a nullPointerException when I'm trying to add the columns to the table view and I don't understand why.
Is it that I can't add columns because the table view is not an object?

Categories

Resources