Can Anybody tell me how to construct HiddenSidesPane in FXML not in the controller?
I am having the basic controller code for this but I am not able to understand how to create fxml structure from that.
Can I have something like this? Below code;
<HiddenSidesPane prefWidth="800.0" pinnedSide="TOP">
<content>
<HBox fillHeight="false" nodeOrientation="RIGHT_TO_LEFT"
prefHeight="27.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="3.0" StackPane.alignment="TOP_RIGHT">
<children>
<Label prefHeight="14.0" prefWidth="94.0" text="Value Date From">
<HBox.margin>
<Insets right="2.0" top="5.0" />
</HBox.margin>
</Label>
</children>
<StackPane.margin>
<Insets top="2.0" />
</StackPane.margin>
</HBox>
</content>
</HiddenSidesPane>
This how I made a fast example with the offical FXSampler of ControlsFX in mind:
Assumptions
You already set up your FXML Project and added the ControlsFX.jar as dependency on your build path.
FXMLDocument.fxml
Watch for the import statements.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.*?>
<?import org.controlsfx.control.*?>
<StackPane xmlns:fx="http://javafx.com/fxml/1" prefHeight="200" prefWidth="320" fx:controller="javafxapplication17.FXMLDocumentController">
<children>
<HiddenSidesPane fx:id="pane">
<content>
<Label alignment="CENTER" style="-fx-background-color: white; -fx-border-color: black;" maxHeight="1000.0" maxWidth="1000.0" text="Content Node" />
</content>
<top>
<Label fx:id="pinLabel" style="-fx-background-color: rgba(0,255,0,.25);" text="(Click to pin / unpin)" alignment="CENTER" prefHeight="50.0" prefWidth="50.0" onMouseClicked="#handleMouseClicked" />
</top>
</HiddenSidesPane>
</children>
</StackPane>
FXMLController.java
Inject your variables pane and pinLabel to set them.
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Side;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import org.controlsfx.control.HiddenSidesPane;
public class FXMLDocumentController implements Initializable {
#FXML
private HiddenSidesPane pane;
#FXML
private Label pinLabel;
#FXML
private void handleMouseClicked(MouseEvent event) {
if (pane.getPinnedSide() != null) {
pinLabel.setText("(unpinned)");
pane.setPinnedSide(null);
} else {
pinLabel.setText("(pinned)");
pane.setPinnedSide(Side.TOP);
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
JavaFXApplication17.java
Sorry for that name :-)
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JavaFXApplication17 extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.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);
}
}
As far as you already know here is the JavaDoc of HiddenSidesPane: http://controlsfx.bitbucket.org/org/controlsfx/control/HiddenSidesPane.html
And if you need an example download the Zip http://fxexperience.com/downloads/controlsfx-8.40.9.zip and unzip it, there is a file controlsfx-samples-8.40.9.jar inside. Double click it and show the sources.
Related
I've realised only through clearing up code that I thought I could clear up and had cleared from some of my Controller classes where my issue is. To word my question better and provide more clarity to the concept of prioritisation I'll give a brief explanation followed by some code. Imagine an interface with a side of the screen being consistent and permanent to the interface where you have Buttons for menu-ing where you essentially move between tabs changing the rest of the Stage passing fxml files to the Pane associated.
Reading examples and questions I understood this to be viable and using the framework from this link https://gist.github.com/jewelsea/6460130 seeing a header remain consistent while switching Scene between vista1.fxml and vista2.fxml, my understanding was that if the Header remains from main.fxml as part of the scene that the Stage is therefore using either both main.fxml and vista1.fxml or main.fxml and vista2.fxml concurrently and thus both associated controllers exist and will have access to corresponding methods according to the Pane interacted with.
That is clearly not the case though and the consistent Pane in this case main.fxml does not have its controller since when the other is loaded and creates its controller it replaces or clears the other controller in some form or another I now presume is the case. I thought I could avoid code duplication by using separate Panes with the consistency of the menu bar but also being able to use its buttons.
Some code as an example:
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception{
stage.setTitle("Vista Viewer");
stage.setScene(createScene(loadMainPane()));
stage.show();
}
private Pane loadMainPane() throws IOException {
FXMLLoader loader = new FXMLLoader();
Pane mainPane = (Pane) loader.load(getClass().getResourceAsStream(
VistaNavigator.MAIN));
MainController mainController = loader.getController();
VistaNavigator.setMainController(mainController);
VistaNavigator.loadVista(VistaNavigator.VISTA_1);
return mainPane;
}
private Scene createScene(Pane mainPane) {
Scene scene = new Scene(mainPane);
return scene;
}
public static void main(String[] args) {
launch(args);
}
}
VistaNavigator.java
package sample;
import javafx.fxml.FXMLLoader;
import java.io.IOException;
public class VistaNavigator {
public static final String MAIN = "main.fxml";
public static final String VISTA_1 = "vista1.fxml";
public static final String VISTA_2 = "vista2.fxml";
public static final String OTHER_MENU_OPTION = "otherMenu.fxml";
private static MainController mainController;
public static void setMainController(MainController mainController) {
VistaNavigator.mainController = mainController;
}
public static void loadVista(String fxml) {
try {
mainController.setVista(
FXMLLoader.load(
VistaNavigator.class.getResource(
fxml
)
)
);
} catch (IOException e) {
e.printStackTrace();
}
}
}
MainController.java
package sample;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.StackPane;
public class MainController {
#FXML
private StackPane vistaHolder;
public void setVista(Node node) {
vistaHolder.getChildren().setAll(node);
}
public void homebtn() throws Exception{
VistaNavigator.loadVista(VistaNavigator.VISTA_1);
}
public void otherMenuOption() throws Exception{
VistaNavigator.loadVista(VistaNavigator.OTHER_MENU_OPTION);
}
}
main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="200.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.MainController">
<children>
<VBox prefWidth="155.0">
<children>
<Label fx:id="headerLabel" maxWidth="120.0" text="Header" />
</children>
</VBox>
<Pane layoutY="40.0" prefHeight="200.0" prefWidth="120.0" style="-fx-background-color: #091D34;">
<children>
<Button fx:id="button1" layoutY="60.0" mnemonicParsing="false" onAction="#homebtn" prefHeight="44.0" prefWidth="120.0" style="-fx-background-color: #133863;" text="Home" textFill="WHITE">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
<Button fx:id="button2" layoutY="110.0" mnemonicParsing="false" onAction="#otherMenuOption" prefHeight="44.0" prefWidth="120.0" style="-fx-background-color: #133863;" text="OtherMenuOption" textFill="WHITE">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
</children>
</Pane>
<StackPane fx:id="vistaHolder" VBox.vgrow="ALWAYS" />
</children>
</AnchorPane>
otherMenu.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<StackPane fx:id="otherMenuOption" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Vista1Controller">
<children>
<Label fx:id="label" layoutX="135.0" layoutY="57.0" maxWidth="120.0" text="other program stuff" VBox.vgrow="NEVER" />
</children>
</StackPane>
Vista1Controller.java
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
public class Vista1Controller {
#FXML
void nextPane(ActionEvent event) {
VistaNavigator.loadVista(VistaNavigator.VISTA_2);
}
}
vista1.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Vista1Controller" prefHeight="400.0" prefWidth="600.0">
<children>
<Pane fx:id="Vista1" layoutX="155.0" layoutY="-1.0" prefHeight="430.0" prefWidth="574.0" style="-fx-background-color: transparent">
<Button fx:id="btn" layoutX="135.0" layoutY="57.0" mnemonicParsing="false" onAction="#nextPane" prefHeight="149.0" prefWidth="318.0" style="-fx-background-color: #133863;" text="Open Vista2" textFill="#ffffff">
<font>
<Font name="Britannic Bold" size="20.0" />
</font>
</Button>
</Pane>
</children>
</AnchorPane>
Vista2Controller.java
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
public class Vista2Controller {
#FXML
void previousPane(ActionEvent event) {
VistaNavigator.loadVista(VistaNavigator.VISTA_1);
}
}
vista2.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Vista2Controller" prefHeight="400.0" prefWidth="600.0">
<children>
<Pane fx:id="Vista2" layoutX="155.0" layoutY="-1.0" prefHeight="430.0" prefWidth="574.0" style="-fx-background-color: transparent">
<Button fx:id="btn" layoutX="135.0" layoutY="57.0" mnemonicParsing="false" onAction="#previousPane" prefHeight="149.0" prefWidth="318.0" style="-fx-background-color: #133863;" text="Open Vista1" textFill="#ffffff">
<font>
<Font name="Britannic Bold" size="20.0" />
</font>
</Button>
</Pane>
</children>
</AnchorPane>
Is there a good way to maintain this design and prevent code duplication within the specific controllers and would it be through inheritance or passing parameters, I'm not sure what this is comparable to nor whether it's possible?
So I have a program that starts with a home screen that has button and a label. I want to make it so that when someone click the button, a new window pops out. And by entering some text in the text field in the new window and hit submit button, the new window closes and the text of label in the home screen is set to the user input.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="HomeController" prefHeight="400.0" prefWidth="600.0">
<VBox prefHeight="400.0" prefWidth="600.0">
<children>
<Label fx:id="label" prefWidth="100" prefHeight="50">
<VBox.margin>
<Insets top="75" left="250"/>
</VBox.margin>
</Label>
<Button fx:id="button" prefWidth="100" prefHeight="50" text="Button" onAction="#buttonClick">
<VBox.margin>
<Insets top="150" left="250"/>
</VBox.margin>
</Button>
</children>
</VBox>
</AnchorPane>
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;
public class HomeController {
#FXML
private Button button;
#FXML
private static Label label;
public void buttonClick(ActionEvent e) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("alert.fxml"));
Stage window = new Stage();
window.initStyle(StageStyle.UNDECORATED);
window.initModality(Modality.APPLICATION_MODAL);
window.setScene(new Scene(root));
window.show();
}
public static Label getLabel() {
return label;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="AlertController" prefHeight="200.0" prefWidth="300.0">
<VBox prefHeight="200.0" prefWidth="300.0">
<children>
<TextField fx:id="text" prefWidth="200" prefHeight="25">
<VBox.margin>
<Insets top="37.5" left="50" right="50"/>
</VBox.margin>
</TextField>
<Button fx:id="submit" prefWidth="100" prefHeight="25" text="Submit" onAction="#submit">
<VBox.margin>
<Insets top="75" left="100"/>
</VBox.margin>
</Button>
</children>
</VBox>
</AnchorPane>
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class AlertController {
#FXML
private TextField text;
public void submit(ActionEvent actionEvent) {
String str = text.getText();
Stage window = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow();
HomeController.getLabel().setText(str);
window.close();
}
}
How do different fxml controllers communicate to each other? Since creating an instance of controller class doesn't seem to work, I try to make the label in home controller static, but when I try to update its text in another controller, a NullPointerException is thrown.
Is there any other way
I found the answer at JavaFX Stage close event handler
It's as simple as adding a onHidding event handler for the new window in the original calling function
Using the FXML project template in netbeans, I've been experiencing an issue with defining the FX controller in my FXML file.
The main class, fxml document and fxml controller are all in the same package: "login".
I am importing the controller into the fxml document using it's fully qualified name.
Error occurs at the import line: "Class login.MyController does not exist."
FXML Document
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Polygon?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<?import login.MyController?>
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="login.MyController">
<children>
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" style="-fx-background-color: #091D34;" VBox.vgrow="ALWAYS">
<children>
<Polygon fill="#000000b1" layoutX="363.0" layoutY="103.0" points="-50.0, 40.0, 50.0, 40.0, 50.0, -73.0" stroke="BLACK" strokeType="INSIDE" style="-fx-fill: #133863;" />
<ButtonBar layoutX="233.0" layoutY="256.0" prefHeight="40.0" prefWidth="161.0">
<buttons>
<Button layoutX="135.0" layoutY="18.0" minWidth="49.0" mnemonicParsing="false" prefHeight="25.0" text="Login" onAction="#doLogin" />
<Button layoutX="165.0" layoutY="18.0" mnemonicParsing="false" text="About" />
</buttons>
</ButtonBar>
<PasswordField layoutX="186.0" layoutY="216.0" prefHeight="25.0" prefWidth="273.0" promptText="Password" onKeyPressed="#onEnter" />
<TextField layoutX="186.0" layoutY="175.0" prefHeight="25.0" prefWidth="273.0" promptText="Username" />
<Text layoutX="253.0" layoutY="76.0" strokeType="OUTSIDE" strokeWidth="0.0" style="-fx-fill: #236AB9;" text="Anatomy Law" textAlignment="CENTER" wrappingWidth="120.58984375">
<font>
<Font name="Aparajita" size="22.0" />
</font>
</Text>
</children>
</AnchorPane>
Controller
package login;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
public class MyController implements Initializable {
#FXML
public void doLogin(Event e){
System.out.println("do login called");
}
#FXML
public void onEnter(KeyEvent ke){
if(ke.getCode() == KeyCode.ENTER){
System.out.println("on enter called");
login();
}
}
private void login(){
}
#Override
public void initialize(URL location, ResourceBundle resources) {
}
}
Main
package login;
import java.net.URL;
import javafx.application.Application;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader();
MyController fxmlController = new MyController();
fxmlLoader.setController(fxmlController);
fxmlLoader.setLocation(new URL("C:/.../login/Login.fxml"));
VBox vbox = fxmlLoader.<VBox>load();
Scene scene = new Scene(vbox);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Note
I recently tried the following line in my FXML document:
<?import login.*?>
Which resulted in "package login does not exist."
And
<?import MyController?>
Which resulted in class does not exist.
So i'm assuming it's some kind of build path error but they're all in the same package. I even added the package that they're in to additional source packages to build from just to make sure but that didn't solve anything. My project layout is the following:
Project Name
Source Packages
login
Login.fxml
MyController.java
Main.java
I reviewed the other question postings for this problem but no one seemed to have this specific problem. If I did however miss someone's post with an identical answer, I will gladly take that reference.
You don't have to import it. You have to specify the full name of your controller class though.
In your case it is login.MyController so fx:controller="login.MyController".
Though you might have some other problems because I'm not having any issues when i put that import statement in the fxml. It just isn't necessary.
Make sure that the document you are editing is definitely the document in your package.
Somehow, in my case I was actually editing a document outside of the package with the same exact name. This is why it couldn't find the controller class when I tried to import it.
I have a JavaFx project with two stages. The main stage has the ability to launch second stage .In second stage I have a textfield I want to put some data into textfield and send it to the main stage and set it as a label .Here is my code:
MAIN:
package sample;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/sample/sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
MAIN CONTROLLER:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
#FXML
private javafx.scene.control.Button kbutton;
#FXML
private javafx.scene.control.Button obutton;
#FXML
public Label label;
public TextField popField;
#FXML
public String text;
#FXML
private void konfButton() throws Exception{
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample/konfview.fxml"));
Parent root = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.showAndWait();
} catch(Exception e) {
e.printStackTrace();
}
}
public void myFunction( String text){
popField.setText(text);
}
#FXML
private void oblButton() throws Exception{
try {
// get a handle to the stage
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample/sample.fxml"));
Parent root = (Parent) fxmlLoader.load();
Controller Controller=fxmlLoader.getController();
Stage stage = (Stage) obutton.getScene().getWindow();
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
MAIN FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints />
</rowConstraints>
<children>
<Pane prefHeight="400.0" prefWidth="600.0">
<children>
<Label layoutX="69.0" layoutY="81.0" text="previous reading" />
<Label layoutX="69.0" layoutY="115.0" text="current reading" />
<Label layoutX="71.0" layoutY="154.0" text="months" />
<Label layoutX="108.0" layoutY="14.0" text="Price">
<font>
<Font name="Comic Sans MS" size="18.0" />
</font>
</Label>
<Label layoutX="97.0" layoutY="51.0" text="Give details">
<font>
<Font name="Comic Sans MS" size="12.0" />
</font>
</Label>
<Button fx:id="kbutton" layoutX="74.0" layoutY="197.0" mnemonicParsing="false" onAction="#konfButton" text="Configure" />
<Button fx:id="obutton" layoutX="197.0" layoutY="197.0" mnemonicParsing="false" onAction="#oblButton" text="OK" />
<TextField fx:id="popField" layoutX="172.0" layoutY="77.0" />
<TextField fx:id="bieField" layoutX="172.0" layoutY="111.0" />
<TextField fx:id="mcField" layoutX="172.0" layoutY="150.0" />
<Label fx:id="label" layoutX="119.0" layoutY="245.0" prefHeight="37.0" prefWidth="333.0" text="text" />
</children>
</Pane>
</children>
</GridPane>
SECOND CONTROLLER:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.io.IOException;
public class ControllerConf {
#FXML
public TextField palField;
public Label label;
public String text;
#FXML
private TextField dysField1;
#FXML
private javafx.scene.control.Button cancelB;
#FXML
public javafx.scene.control.Button saveB;
#FXML
private void cancelButton(){
// get a handle to the stage
Stage stage = (Stage) cancelB.getScene().getWindow();
// do what you have to do
stage.close();
}
#FXML
public Stage saveButton(){
try {
FXMLLoader loader=new
FXMLLoader(getClass().getResource("/sample/sample.fxml"));
Parent root = (Parent) loader.load();
Controller Controller=loader.getController();
Controller.myFunction(palField.getText());
Stage primaryStage = (Stage) saveB.getScene().getWindow();
primaryStage.close();
return primaryStage;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
SECOND FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.ControllerConf">
<children>
<Label layoutX="185.0" layoutY="28.0" prefHeight="17.0" prefWidth="314.0" text="Give details">
<font>
<Font name="Comic Sans MS" size="20.0" />
</font>
</Label>
<Label layoutX="48.0" layoutY="95.0" text="Price pre KWh" />
<Label layoutX="48.0" layoutY="135.0" text="Price pre item" />
<TextField fx:id="palField" layoutX="252.0" layoutY="91.0" onAction="#saveButton" />
<TextField fx:id="dysField" layoutX="252.0" layoutY="131.0" />
<Button fx:id="cancelB" layoutX="159.0" layoutY="208.0" mnemonicParsing="false" onAction="#cancelButton" text="Cancel">
<font>
<Font name="Comic Sans MS" size="12.0" />
</font>
</Button>
<Button fx:id="saveB" layoutX="292.0" layoutY="209.0" mnemonicParsing="false" onAction="#saveButton" text="Save">
<font>
<Font name="Comic Sans MS" size="12.0" />
</font>
</Button>
</children>
</AnchorPane>
I created MyFunction wchich set text and it's working fine when created new stage but doesnt for main stage.Do you know how to solve it?Thanks in advance.enter code here
I've been trying button events between 2 scenes in Javafx.
This is Start.fxml (in view package):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="292.0" prefWidth="383.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.StartController">
<children>
<Label fx:id="lbl1" layoutX="87.0" layoutY="60.0" prefHeight="17.0" prefWidth="209.0" />
<TextField fx:id="txt1" layoutX="87.0" layoutY="121.0" prefHeight="25.0" prefWidth="209.0" />
<Button fx:id="btn1" layoutX="171.0" layoutY="191.0" mnemonicParsing="false" onAction="#btn1Click" text="Click" />
</children>
</AnchorPane>
This is Preserences.fxml (in view package):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="292.0" prefWidth="383.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.PreferencesController">
<children>
<Label fx:id="lbl2" layoutX="87.0" layoutY="60.0" prefHeight="17.0" prefWidth="209.0" />
<TextField fx:id="txt2" layoutX="87.0" layoutY="121.0" prefHeight="25.0" prefWidth="209.0" />
<Button fx:id="btn2" layoutX="171.0" layoutY="191.0" mnemonicParsing="false" onAction="#btn2Click" text="Click" />
</children>
</AnchorPane>
This is Main.fxml which is like a container for the previous 2 files.(in view package):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="481.0" prefWidth="468.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.MainController">
<children>
<TabPane layoutX="14.0" layoutY="14.0" prefHeight="481.0" prefWidth="468.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab closable="false" text="Start">
<content>
<fx:include source="Start.fxml" />
</content>
</Tab>
<Tab closable="false" text="Preferences">
<content>
<fx:include source="Preferences.fxml" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
This is StartController.java (in controller package):
package controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class StartController implements Initializable {
#FXML public static Label lbl1;
#FXML public static TextField txt1;
#FXML public static Button btn1;
#Override
public void initialize(URL location, ResourceBundle resources) {
}
#FXML private void btn1Click(ActionEvent e){
System.out.println("Button 1 clicked");
lbl1.setText(PreferencesController.txt2.getText());
}
}
This is PreferencesController.java (in controller package):
package controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class PreferencesController implements Initializable {
#FXML public static Label lbl2;
#FXML public static TextField txt2;
#FXML public static Button btn2;
#Override
public void initialize(URL location, ResourceBundle resources) {
}
#FXML private void btn2Click(ActionEvent e){
System.out.println("Button 2 clicked");
lbl2.setText(StartController.txt1.getText());
}
}
This is MainController.java (in controller package):
package controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
public class MainController implements Initializable{
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
}
Finally, this is Main.java in application package:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/view/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) {
launch(args);
}
}
But when I clicked the buttons, it gives me the errors:
"Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException...
...
Caused by: java.lang.NullPointerException
at controller.StartController.btn1Click(StartController.java:26)
... 61 more"
Please help me! :(
OK so the NPE is being thrown on this line:
lbl1.setText(PreferencesController.txt2.getText());
At the basic Java level, are only two ways that you could get that to happen:
when lnl1 is null, or
when PreferencesController.txt2 is null.
No other explanations are possible ... given that PreferencesController.txt2 is a static field.
I'm not familiar with JavaFX or FXML, but I suspect that your mistake is declaring the fields as static. I suspect that this causes the FXML mechanisms to not inject the reference to the UI component correctly. So those variables remain as null ... and you get an NPE.
Confirmation is here: https://stackoverflow.com/a/23109125/139985. (Thanks for the link #James_D)