JavaFX Scene 2 button on actions not working - java

I have 2 scenes. One of the scenes is Login and the other is Register. When I click the register button on the Log in screen it switches me to the register scene. The actions I have made in the controller for the button is not working. Do I need to do something in the main or controller to get the Register.fxml to work?
ModelController:
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.sql.*;
public class ModelController extends Main{
#FXML
private Label LblStatus;
#FXML
private TextField UserEmail;
#FXML
private TextField UserPassword;
#FXML
private TextField RegisterEmail;
#FXML
private TextField RegisterPassword;
#FXML
private TextField RegisterFirst;
#FXML
private TextField RegisterLast;
#FXML
private TextField RegisterSSN;
#FXML
private TextField RegisterAddress;
#FXML
private TextField RegisterCity;
#FXML
private TextField RegisterState;
#FXML
private TextField RegisterZip;
#FXML
private Button LoginRegister;
public void Login(ActionEvent event) throws Exception {
if(UserEmail.getText().equals("User") && UserPassword.getText().equals("pass")) {
LblStatus.setText("Login Success");
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
else {
LblStatus.setText("Login Failed");
}
}
public void handleButtonAction(ActionEvent event) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Register.FXML"));
Parent root1 = (Parent) fxmlLoader.load();
Scene Register = new Scene(root1);
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(Register);
window.show();
}
catch(Exception e) {
}
}
public void CompleteRegistration(ActionEvent event) {
}
Main:
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/Login.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

The 2nd scene was not pulling in the controller.

Related

How can I make JavaFX re-initialize when Scene is swapped?

I want to make a very simple program in JavaFX. It goes like this:
The user inputs something into a TextField
The program displays the input on a label but on a different Scene
Here is my code:
Controller.java
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 javax.xml.soap.Text;
import java.io.IOException;
public class Controller {
//textField in sample.fxml
#FXML
TextField textField;
//label in display.fxml
#FXML
Label label;
String text;
#FXML
public void enter() throws IOException {
text = textField.getText();
Stage stage = (Stage) (textField).getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getResource("display.fxml"));
stage.setResizable(false);
stage.setTitle("Display");
stage.setScene(new Scene(root, 300, 275));
label.setText(text);
}
}
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);
}
}
There are 2 other FXML files containing either a single textField or a single Label.
But whenever I run this code there is a NullPointerException signaling that label is null, because it hasn't been initialized. How do I fix this?
I believe you should make antoher controller for the display.fxml file (the other scene). Than in this new controller you can prepare a function to set label value:
DisplayController.java
public class DisplayController {
//label in display.fxml
#FXML
Label label;
public void setLabelText(String s){
label.setText(s);
}
}
and in Controller.java edit enter() function by calling a new DisplayController.java instance:
#FXML
public void enter() throws IOException {
text = textField.getText();
Stage stage = (Stage) (textField).getScene().getWindow();
FXMLLoader loader = new FXMLLoader.load(getClass().getResource("display.fxml"));
Parent root = loader.load();
DisplayController dc = loader.getController();
//here call function to set label text
dc.setLabelText(text);
stage.setResizable(false);
stage.setTitle("Display");
stage.setScene(new Scene(root, 300, 275));
}

How to open another screen in JavaFX (not FXML file)

I'm very new to JavaFX. I want to communication between two window in my program, but I don't know how to do it. For example: after login successfully, another screen will pop up (but I don't want to load the file FXML because I used the loop to generate the toggle buttons in this window instead of dragging-dropping in FXML file). I don't know whether it makes sense or not. If not please forgive me :(
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{
Parentroot=FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Login");
primaryStage.setScene(new Scene(root, 400,300));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller.java
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;
public class Controller {
#FXML
private TextField usernameField;
#FXML
private TextField passwordField;
#FXML
public void login(){
if(usernameField.getText().equals("admin") && passwordField.getText().equals("123")){
successLogin();
}
}
public void successLogin(){
try {
Parent root = FXMLLoader.load(getClass().getResource("anotherscene.fxml"));
Stage stage = new Stage(StageStyle.DECORATED);
stage.show();
}catch (IOException e){
e.printStackTrace();
}
}
}
AnotherScene.java
package sample;
import javafx.application.Application;
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.ToggleButton;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class AnotherScene implements Initializable {
ToggleButton[][] matrix;
private static final int SIZE = 10;
private static int length = SIZE;
private static int width = SIZE;
#FXML
private BorderPane borderPane;
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
Parent root = null;
try{
root = FXMLLoader.load(getClass().getResource("anotherscene.fxml"));
}catch (IOException e){
e.getStackTrace();
}
Stage stage = new Stage();
VBox vBox = new VBox();
vBox.getChildren().add(new Label("Seat Number: "));
borderPane.setLeft(vBox);
GridPane gridPane = new GridPane();
gridPane.setHgap(10);
gridPane.setVgap(10);
borderPane.setCenter(gridPane);
matrix = new ToggleButton[length][width];
for(int row = 0; row < length; row++){
for(int col = 0; col < width; col++){
matrix[row][col] = new ToggleButton();
matrix[row][col].setText(row + "" +col);
matrix[row][col].setPrefWidth(40);
gridPane.add(matrix[row][col], row, col);
}
}
// Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
stage.setTitle("Hello World");
Scene scene = new Scene(root, 700, 500);
stage.setScene(scene);
stage.show();
}
}

Getting syntax error while binding label to StringProperty

I am just starting to learn javafx.I am trying to make a simple application where you enter into a textfield and simultaniously give it out via the label.
Heres the main class
package testing;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class testing extends Application {
private Stage primaryStage;
private BorderPane RootLayout;
#Override
public void start(Stage primaryStage) {
this.primaryStage=primaryStage;
this.primaryStage.setTitle("First Application");
initRootLayout();
initProjectTest();
}
public static void main(String[] args) {
launch(args);
}
public Stage getPrimaryStage() {
return primaryStage;
}
public void initRootLayout() {
try{ FXMLLoader loader=new FXMLLoader();
loader.setLocation(testing.class.getResource("view/RootLayout.fxml"));
RootLayout=(BorderPane)loader.load();
primaryStage.setScene(new Scene(RootLayout));
primaryStage.show();
}
catch(IOException e) {
e.printStackTrace();
}
}
public void initProjectTest() {
try {
FXMLLoader loader=new FXMLLoader();
loader.setLocation(testing.class.getResource("view/ProjectTest.fxml"));
AnchorPane ProjectTest=(AnchorPane)loader.load();
RootLayout.setCenter(ProjectTest);
}
catch(IOException e) {
e.printStackTrace();
}
}
}
Here is the controller class
package testing.view;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class ProjectTestController {
#FXML
private Label label1=new Label();
#FXML
private TextField textfield1=new TextField();
label1.textProperty().bind(textfield1.textProperty());
}
If I try to implement it via a button where the text is updated everytime I click the button(via a onAction function) the code works fine. However I get syntax error when I try to bind. I tried to search for answers but none of were having syntax errors.

JavaFX 8 on Button Click show other Scene

Hello I am currently working on a PIN Generator for my work and as I am completely new to Java I am having a few difficulties, especially when using JavaFX.
I want to make the programm show another .fxml file when clicking on one of each buttons.
Here is my code:
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 {
public static void main(String[] args) {
Application.launch(main.class, args);
}
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));
stage.setTitle("PIN-Generator");
stage.setScene(new Scene(root, 600, 400));
stage.show();
}
}
I have created a controller Class for each button on the scene.
Controller Class Code:
package view;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
public class MainController {
#FXML
private Label dpmadirektpropingenerator;
#FXML
private Button unlockPinButton;
#FXML
private Button confirmationPinButton;
}
This code should works.
Modify your main class like this:
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 {
public static void main(String[] args) {
Application.launch(main.class, args);
}
static Stage stg;
#Override
public void start(Stage stage) throws Exception {
this.stg = stage;
Parent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));
stage.setTitle("PIN-Generator");
stage.setScene(new Scene(root, 600, 400));
stage.show();
}
}
and here is your pressButton function:
public void pressButton(ActionEvent event){
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sedondFXML.fxml"));
Parent root = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
main.stg.close();
} catch(Exception e) {
e.printStackTrace();
}
}

NullPointerException when I'm changing the scene in JAVAFX

I want to change scenes on my stage or close stage when I push the startgame button and exitgame button, but its just show me first scene, and when I'm trying to push one of this buttons compiler close in case of NullPointerException.
I also have main class and two fxml files, but I think it's don't necessary to put it here, it contains just two anchor panes, primitive labels and buttons.
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable{
private Stage stage = new Stage();
private FXMLLoader loader = new FXMLLoader();
private AnchorPane anchorPane = new AnchorPane();
private Scene scene = new Scene(new AnchorPane());
#FXML private Button startGameButton;
#FXML private Button endGameButton;
#FXML private Button buttonAnswer1;
#FXML private Button buttonAnswer2;
#FXML private Button buttonAnswer3;
#FXML private Button buttonAnswer4;
public void createScene(int typeOfScene) throws Exception
{
if (typeOfScene == 1)
{
loader = new FXMLLoader(getClass().getResource("/sample/sample.fxml"));
anchorPane = loader.load();
scene = new Scene(anchorPane);
stage.setScene(scene);
stage.show();
}
if (typeOfScene == 2)
{
scene = null;
loader = new FXMLLoader(getClass().getResource("/sample/episodesFXML.fxml"));
anchorPane = loader.load();
scene = new Scene(anchorPane);
stage.setScene(scene);
stage.show();
}
}
public void getPrimaryStage(Stage stage) throws Exception
{
this.stage = stage;
createScene(1);
}
#Override
public void initialize(URL location, ResourceBundle resources) {
startGameButton.setOnAction(event -> {
try {
createScene(2);
} catch (Exception e)
{
e.printStackTrace();
}
});
endGameButton.setOnAction(event -> {
stage.close();
});
}
}
I remember a problem like that, fxml file is linked to a controller file by the fx:controller attribute. You try to load a fxml in a controller that it's not handled/loaded. Create another form and showing/hiding forms seems simpler.

Categories

Resources