Loading new scene in JavaFX - javafx

I am attempting to switch scenes from a login screen to the main screen of my program but whenever I try to switch scenes after clicking login, I get the following error.
"Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException"
I've messed around with my code and try to change some things to get different results but no dice. This is my first time doing a GUI so any help would be appreciated.
package pwmanager;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventType;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
/**
*
* #author 176878
*/
public class FXMLDocumentController implements Initializable {
#FXML
private Button loginButton;
#FXML
Stage prevStage;
Stage currentStage;
public void setPrevStage(Stage stage){
this.prevStage = stage;
}
#FXML
public void getPrevStage(Stage stage){
this.currentStage = prevStage;
}
#FXML
public void loginButtonAction(ActionEvent event) throws IOException {
System.out.println("You clicked me, logging in!");
setPrevStage(prevStage);
Stage stage = new Stage();
try{
FXMLLoader loader = new FXMLLoader(getClass().getResource("MainScreen.fxml"));
GridPane mainScreen = (GridPane) loader.load();
Scene scene = new Scene(mainScreen);
stage.setScene(scene);
stage.setTitle("Password Manager");
stage.show();
prevStage.hide();
}
catch(IOException e){
System.out.println("Did not load right");
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}

Related

JavaFx: Check if Username and Password in Registration is matched with Login

I want to check if usernames and passwords in Regisration.java are matched with usernames and passwords in Login.java. For example, if I type in "Ali" as username, and "123" as password in Registration.java and saved it, which I have already done. Then, when I go to Login.java and type in for example "Ross" as username and "1010" as password in Login.java, it will print "Username or Password is wrong"
Users.java:
import java.util.ArrayList;
/**
*
* #author ammar
*/
public class Users {
public static ArrayList<String> usernames = new ArrayList<String>();
public static ArrayList<String> passwords = new ArrayList<String>();
public void addusers(String u, String p){
usernames.add(u);
passwords.add(p);
}
public ArrayList getUserNames()
{
return usernames;
}
public ArrayList getPasswords()
{
return passwords;
}
}
Login.java
package javaapplication6;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
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.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
/**
* FXML Controller class
*
* #author ammar
*/
public class LoginController implements Initializable {
#FXML
private Label Titlelbl;
#FXML
private Label UserNamelbl;
#FXML
private Label Passwordlbl;
#FXML
private Button Registerbtn;
#FXML
private Button Loginbtn;
#FXML
private Label Forgetbtn;
private Label Outputlbl;
#FXML
private TextField UserNametxt;
#FXML
private TextField Passwordtxt;
#FXML
private Label Outputlbl1;
#FXML
private Label Outputlbl2;
#FXML
private ImageView Img;
#FXML
private Button Viewbtn;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
Image image = new Image(getClass().getResourceAsStream("/javaapplication6/icons/uqu.png"));
Img.setImage(image);
}
#FXML
private void Login(ActionEvent event) {
var valid = true;
// Validate the username field
if (UserNametxt.getText().isEmpty() ) {
valid = false;
Outputlbl1.setText("Please Enter User Name ");
} else if (UserNametxt.getText().equals("Ali")) {
Outputlbl1.setText("Welcome");
} else {
Outputlbl1.setText("In");
}
if (Passwordtxt.getText().isEmpty()) {
valid = false;
Outputlbl2.setText("Please Enter Password");
} else {
Outputlbl2.setText("");
}
}
#FXML
private void Registerbtn(ActionEvent event) {
try {
((Node)event.getSource()).getScene().getWindow().hide();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("SecondWindow.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.show();
} catch (Exception e) {
System.out.println("Cant load new window");
}
}
#FXML
private void view(ActionEvent event) {
Users u = new Users();
ArrayList<String> uname = new ArrayList<String>();
ArrayList<String> pass = new ArrayList<String>();
uname = u.getUserNames();
pass = u.getPasswords();
System.out.println("The user Names-"+uname);
}
}
Registration.java (I named it SecondWindowController.java):
package javaapplication6;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
/**
* FXML Controller class
*
* #author ammar
*/
public class SecondWindowController implements Initializable {
#FXML
private Button Backbtn;
#FXML
private Button Savebtn;
#FXML
private TextField UserNameReg;
#FXML
private TextField PasswordReg;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
#FXML
private void back(ActionEvent event) throws IOException {
((Node)event.getSource()).getScene().getWindow().hide();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Login.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.show();
}
#FXML
private void save(ActionEvent event) {
Users user = new Users();
user.addusers(UserNameReg.getText(), PasswordReg.getText());
}
}
FXMain:
package javaapplication6;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* #author ammar
*/
public class FXMain extends Application {
#Override
public void start(Stage stage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("/javaapplication6/Login.fxml"));
Scene scene = new Scene(root, 600, 400);
stage.setScene(scene);
stage.setTitle("UQU");
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

Java fx event handler

Why my code is not running? How am I going to display an image on the interface when clicked "add image"?
import javafx.stage.Stage;
import javafx.scene.*;
import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
public class GUIProject extends Application{
/**
* #param args the command line arguments
*/
public void start (Stage stage) throws Exception
{
Pane bPane = new Pane();
Button btOK = new Button("OK");
OKAction OKact = new OKAction();
btOK.setOnAction(OKact);
Button addImage = new Button("Add Image");
isAdd addimage = new isAdd();
addImage.setOnAction(addimage);
//bPane.getChildren().add(btOK);
bPane.getChildren().add(addImage);
Scene scene = new Scene(bPane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
// TODO code application logic here
Application.launch(args);
}
}
import java.io.FileInputStream;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
public class Pane extends StackPane {
public void addImage() throws Exception
{
Image logo = new Image(new FileInputStream("logo.jpg"));
ImageView logoView = new ImageView(logo);
this.getChildren().add(logoView);
}
}
import java.io.FileInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
class isAdd implements EventHandler<ActionEvent>{
public void handle(ActionEvent event)
{
try {
Pane.displayImage();
}
catch (Exception ex) {
System.out.println("Error");;
}
}
}
It keep showing static method cannot with non-static method, how can I solve that?
Is there any website can learn javafx event handler?

how to dynamically set new TreeItems in a TreeView in JavaFX [duplicate]

I am begging with JavaFx, and I realized that I need some help to update a TreeView with some TreeItems in runtime, and it should be updated in the main window.
Here, you can see a screenshot of the two windows:
The bigger is the main window and it calls (by clicking in File >> New Project), new smaller. In the smaller window, I could get the String that is typed and than the enter button is clicked.
The trouble is: How can I show the new items created by the "new project window" (the smaller window in the pic) in the TreeView in the main window(the bigger)?
The treeview is in the left side of the main window.
I hope I was clear.
Here is the code of the controllers of these windows:
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeItem.TreeModificationEvent;
import javafx.scene.control.TreeView;
import javafx.stage.Modality;
import javafx.stage.Stage;
/**
* this class handles with the main window of our LDF Tool
* #author Vinicius
* #version 1.0
*/
public class MainController implements Initializable{
#FXML
TreeView<String> treeView;
#FXML
MenuItem newProject;
private boolean flag = false;
private NewProjectWindowController npwc;
#Override
public void initialize(URL location, ResourceBundle resources) {
}
#FXML
public void newProjectClicked(ActionEvent event){
try{
flag = true;
FXMLLoader fxml = new FXMLLoader(getClass().getResource("newProjectWindow.fxml"));
Parent root = (Parent) fxml.load();
Stage newWindow = new Stage();
newWindow.setTitle("New Project");
newWindow.initModality(Modality.APPLICATION_MODAL);
newWindow.setScene(new Scene(root));
newWindow.show();
} catch (Exception e) {
System.out.println("caiu na exceção");
}
}
/**
* to this method, choose the project's name as argument, and it will be put on the
* tree with the archives that should be created together
* #param projectName
*/
public void doTree(String projectName){
TreeItem<String> root = new TreeItem<>("projectName");
root.setExpanded(true);
//TreeItem<String> folha1 = new TreeItem<String>(projectName + " arquivo 1");
//root.getChildren().add(folha1);
treeView.setRoot(root);
}
The other controller class:
package application;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class NewProjectWindowController implements Initializable{
#Override
public void initialize(URL location, ResourceBundle resources) {
}
#FXML
Button cancelButton;
#FXML
Button enterButton;
#FXML
TextField textInput;
private String input;
public String getInput(){
return this.input;
}
#FXML
public void cancelButtonClicked(ActionEvent event) {
Stage window = (Stage) this.cancelButton.getParent().getScene().getWindow();
window.close();
}
#FXML
public void enterButtonClicked(ActionEvent event) {
input = hasString();
Stage window = (Stage) this.enterButton.getParent().getScene().getWindow();
window.close();
}
private String hasString(){
if (this.textInput.getText().isEmpty())
return null;
return this.textInput.getText();
}
}
Please, assume that I mapped everything ok in the FXML file.
thanks
#FXML
public void newProjectClicked(ActionEvent event){
try{
flag = true;
FXMLLoader fxml = new FXMLLoader(getClass().getResource("newProjectWindow.fxml"));
Parent root = (Parent) fxml.load();
Stage newWindow = new Stage();
newWindow.setTitle("New Project");
newWindow.initModality(Modality.APPLICATION_MODAL);
newWindow.setScene(new Scene(root));
// showAndWait blocks execution until the window closes:
newWindow.showAndWait();
NewProjectWindowController controller = fxml.getController();
String input = controller.getInput();
if (input != null) {
TreeItem<String> currentItem = treeView.getSelectionModel().getSelectedItem();
if (currentItem == null) currentItem = treeView.getRoot();
currentItem.getChildren().add(new TreeItem<>(input));
}
} catch (Exception e) {
System.out.println("caiu na exceção");
}
}

Change the scene (load another FXML file)

I launch my application using this Main.java file:
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 {
Parent root;
#Override
public void start(Stage stage) throws Exception {
root = FXMLLoader.load(getClass().getResource("../fxml/mainpage.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Smart City - Main Page");
stage.setScene(scene);
stage.setMaximized(true);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I also created a class named ScreenChanger.java:
package utils;
import java.io.IOException;
import javafx.event.Event;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class ScreenChanger {
public static void screenChanger(Event event, String path, String title) throws IOException{
Parent root = FXMLLoader.load(getClass().getResource(path));
Scene scene = new Scene(root);
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.setScene(scene);
stage.setTitle(title);
stage.setMaximized(false);
stage.setMaximized(true);
stage.show();
}
}
The problem is that sometimes I haven't that Event in my code when I want to switch between scenes. I would like to use something else instead of Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); in order to change the screen.

NullPointerException in changing javaFX scene

I have two scenes in my javaFx project .. the first one Language.fxml has a button which on click changes the scene to allDevices.fxml .. but it throws NullPointerException saying "Location is required" although both of the fxml files are in the same path !!
that's my LanguageController.java
package astrolabe;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
/**
*
* #author Ahmed Fawzy
*/
public class LanguageController implements Initializable {
#FXML
private Button arabic ;
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
arabic.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
// TODO Auto-generated method stub
try{
Node node=(Node) event.getSource();
Stage stage=(Stage) node.getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("allDevices.fxml"));/* Exception */
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
}
The problem solved by adding the package name before the fxml file name !

Resources