JavaFX, closing error window using Button - java

I have a problem with my basic encrypter application. I want to generate an error window if someone type a string in the keyTextField. And also an event to close the error window using OK Button(Window graphic is loading from fxml file)
I've tried making it as shown below, but without success, i was also using close() method. What are best methods to deal with application control? I am using only MainController and i think it is not good idea. Thank you in advance
`package pl.gumisok.cipherController;
import java.io.IOException;
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.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import pl.gumisok.cipherMain.CipherManager;
public class MainController implements Initializable {
CipherManager cipher;
#FXML
private ContentPaneController contentPaneController;
#FXML
private ControlPaneController controlPaneController;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
System.out.println(contentPaneController);
System.out.println(controlPaneController);
Button encryptButton = controlPaneController.getEncryptButton();
Button decryptButton = controlPaneController.getDecryptButton();
Button okButton = controlPaneController.getOkButton();
TextArea cleanTextArea = contentPaneController.getCleanTextArea();
TextArea cryptTextArea = contentPaneController.getCryptTextArea();
TextField keyTextField = controlPaneController.getKeyTextField();
encryptButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
String wiadomosc = cleanTextArea.getText();
System.out.println(wiadomosc);
try {
int key = Integer.parseInt(keyTextField.getText());
System.out.println(key);
} catch (NumberFormatException e) {
System.out.println(e);
FXMLLoader fxmlLoader = new FXMLLoader(getClass()
.getClassLoader().getResource(
"pl/gumisok/cipherView/Error.fxml"));
Parent root;
try {
root = fxmlLoader.load();
Stage sstage = new Stage();
sstage.setOpacity(1);
sstage.setTitle("Error");
sstage.setScene(new Scene(root));
sstage.show();
okButton.setOnAction(x->sstage.hide());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
}
}`

your application layer is not good.
you need bind the button action in fxml file to a controller.
Maybe something like this:
Error.fxml:
<AnchorPane xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controllers.ErrorController">
<children>
<Label text="ERROR!" />
<Button text="close" onAction="#hide" layoutY="15"/>
</children>
</AnchorPane>
ErrorController.java:
public class ErrorController {
private static Stage stage;
private static Parent root;
public ErrorController(){}
public ErrorController(Window owner) throws IOException {
if (root == null)
root = FXMLLoader.load(ClassLoader
.getSystemResource("views/Error.fxml"));
if (stage == null)
stage = new Stage();
//stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(owner);
stage.setTitle("Error");
stage.setScene(new Scene(root));
}
public void show() {
stage.show();
}
public #FXML void hide() {
stage.hide();
}
}
And then use it
...
error = new ErrorController(node.getScene().getWindow());
...
try {
int key = Integer.parseInt(keyTextField.getText());
System.out.println(key);
} catch (NumberFormatException e) {
error.show();
}

I hope, I understand the question correct,
here is an example how to create an alert dialog
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText(null);
alert.setContentText("I have a great message for you!");
alert.showAndWait();

Related

Information not being updated when transferring information across scenes in Java and JavaFX

I have a login page and I'm trying to get a users email that they entered to display on the next page which is their homepage. This is that code:
FXMLLoader loaderNew = new FXMLLoader(getClass().getResource("/sample/view/Dashboard.fxml"));
try {
loaderNew.load();
} catch (IOException e) {
e.printStackTrace();
}
DashboardController dashboardController = loaderNew.getController();
dashboardController.setName(loginEmail.getText().trim());
And this is the method I'm trying to call:
public void setName(String name){
nameLbl.setText(name);
}
I'm getting no errors, it's just not updated the nameLbl and I have no idea why. The logincontroller will go the dashboard successfully, it just won't update the nameLbl.
Here are both full codes.
Login Controller:
package sample.controller;
import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
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.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import sample.database.DBConnection;
import sample.model.User;
public class LoginController {
#FXML
private ResourceBundle resources;
#FXML
private URL location;
#FXML
private Button loginButton;
#FXML
private TextField loginEmail;
#FXML
private Button createAccountSwitchButton;
#FXML
private Label errorLabel;
#FXML
private PasswordField loginPassword;
private DBConnection dbConnection;
#FXML
void initialize() {
dbConnection = new DBConnection();
loginButton.setOnAction(event-> {
String loginEmailText = loginEmail.getText().trim();
String loginPasswordText = loginPassword.getText().trim();
User user = new User();
user.setEmail(loginEmailText);
user.setPassword(loginPasswordText);
ResultSet userRow = dbConnection.checkForUser(user);
int counter = 0;
try{
while (userRow.next()){
counter++;
}
if (counter==1){
//-------------------Below this line to the next line seems to do nothing-------------------------------------------------------------------
FXMLLoader loaderNew = new FXMLLoader(getClass().getResource("/sample/view/Dashboard.fxml"));
try {
loaderNew.load();
} catch (IOException e) {
e.printStackTrace();
}
DashboardController dashboardController = loaderNew.getController();
dashboardController.setName(loginEmail.getText().trim());
//---------------------------------------------------------------------------------------
loginButton.getScene().getWindow().hide();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/sample/view/Dashboard.fxml"));
try {
loader.load();
} catch (IOException e) {
e.printStackTrace();
}
Parent root = loader.getRoot();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.showAndWait();
}else{
errorLabel.setText("Email or Password Incorrect");
}
}catch(SQLException e){
e.printStackTrace();
}
});
}
}
Dashboard controller:
package sample.controller;
//imports here same as above
public class DashboardController {
#FXML
private Label nameLbl;
public void setName(String name){
nameLbl.setText(name);
}
#FXML
void initialize() {
}
}
It seems like my method in Dashboard controller is never being called, why is that? Thanks in advance for any help.
I updated the small segment such that it's only loading one FXML Loader thing and it works now.
loginButton.getScene().getWindow().hide();
FXMLLoader loader = new FXMLLoader(getClass().getResource("/sample/view/Dashboard.fxml"));
try {
loader.load();
} catch (IOException e) {
e.printStackTrace();
}
Parent root = loader.getRoot();
DashboardController dashboardController = loader.getController();
dashboardController.setName(loginEmail.getText().trim());
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();

LinkedList erased in Action Handelers methods in JavaFX

I am working on an App that simulates an online shop for my Data Structures course, I am currently using JavaFX in the Eclipse IDE and Scene Builder for the GUI design, however I am trying to use a Double LinkedList to store the Accounts in the Sign In UI window, but for some reason every time that I try to insert a new Account object (in the Action Event method) into my List it doesn't inserts it, at least if I try to print it´s toString gives me a Null pointer Exception, but if the insert and the println is in the start method works. Last year a did an MP3 player using JavaFX but in Netbeans and I didn't use Scene Builder and the List did work, some one has any idea why is this happening?
package com.javafx.quvbi.controller;
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.TextField;
import javafx.stage.Stage;
import javafx.scene.input.MouseEvent;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class interfazController extends Application {
#FXML
private Button pausa, CreateAccBttn, LoginBttn;
#FXML
private TextField NameTF, AccountTF, PasswordTF, CardTF;
//Data
ListaDobleEnlazada<Account> accList;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
//NameTF = new TextField();
//AccountTF = new TextField();
//PasswordTF= new TextField();
//CardTF = new TextField();
accList = new ListaDobleEnlazada<Account>();
//accList.insertarInicio(new Account("Diego","Qubi","22f24rf2",123321));
Parent root = FXMLLoader.load(getClass().getResource("/view/OptionUI.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
System.out.println(accList.toString());
}
public ListaDobleEnlazada<Account> getAccounts() {
return accList;
}
#FXML
public void controlMusica(ActionEvent event) {
System.out.println("musica");
System.out.println(getAccounts().getInicio().toString());
}
public void CreateAccClick(MouseEvent event) throws IOException {
if(!NameTF.getText().isEmpty() && !AccountTF.getText().isEmpty() && !PasswordTF.getText().isEmpty() && !CardTF.getText().isEmpty()) {
accList.insertarInicio(new Account(NameTF.getText(), AccountTF.getText(), PasswordTF.getText(), Integer.parseInt(CardTF.getText())));
}
}
#SuppressWarnings("restriction")
public void createAccount(ActionEvent event) throws IOException {
if(!NameTF.getText().isEmpty() && !AccountTF.getText().isEmpty() && !PasswordTF.getText().isEmpty() && !CardTF.getText().isEmpty()) {
Parent loginUI = FXMLLoader.load(getClass().getResource("/view/GUI2.fxml"));
Scene logInScene = new Scene(loginUI);
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(logInScene);
window.show();
System.out.println(NameTF.getText());
System.out.println(AccountTF.getText());
System.out.println(PasswordTF.getText());
System.out.println(CardTF.getText());
//this.accList.insertarInicio(new Account(NameTF.getText(), AccountTF.getText(), PasswordTF.getText(), Integer.parseInt(CardTF.getText())));
}
}
public void loginAccount(ActionEvent event) throws IOException {
Parent loginUI = FXMLLoader.load(getClass().getResource("/view/GUI2.fxml"));
Scene logInScene = new Scene(loginUI);
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(logInScene);
window.show();
}
}

JavaFX: Load String from File and set to Label

Not only have I lost 5 hours but also almost my mind because of this problem.
I load a String from a Txt File and want to set it to the label sourceURL. Before I check it in System.out.println("Firstline is : " + brTest.readLine()); and it shows the String correctli. But then the UI pops up and the label is empty.
It looks like it couldnt load it and needs to refresh. Do you know anything here?
DashBoardController
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.text.Font;
import javafx.stage.DirectoryChooser;
public class DashboardController {
#FXML
private Label userFeedback;
#FXML
private Font x1;
#FXML
private Label sourceURL;
#FXML
private Label targetURL;
#FXML
public void initialize(){
BufferedReader brTest;
try {
brTest = new BufferedReader(new FileReader(new File("settings/sourceurl.txt")));
System.out.println("Firstline is : " + brTest.readLine());
String temp = brTest.readLine();
sourceURL.setText(brTest.readLine());
sourceURL.setText(temp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#FXML
void convert(ActionEvent event) {
}
#FXML
void openFolder(ActionEvent event) {
}
#FXML
void selectSource(ActionEvent event) throws FileNotFoundException {
//DirectoryChooser initialisieren
DirectoryChooser directoryChooser = new DirectoryChooser();
directoryChooser.setTitle("Quellordner wählen");
//URL speichern
File selectedDirectory = directoryChooser.showDialog(null);
sourceURL.setText(selectedDirectory.getAbsolutePath());
//Ordner erstellen
File settings = new File("settings");
settings.mkdir();
//Datei erstellen und URL abspeichern
PrintWriter printWriter = new PrintWriter("settings/sourceurl.txt");
printWriter.println(selectedDirectory.getAbsolutePath());
printWriter.close();
}
#FXML
void selectTarget(ActionEvent event) {
sourceURL.setText("sdfkjasdöfl");
}
#FXML
void showInfo(ActionEvent event) {
}
#FXML
void showSupport(ActionEvent event) {
}
}
Dashboard
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Dashboard extends Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
// TODO Auto-generated method stub
//Pane laden
Pane pane = (Pane) FXMLLoader.load(Dashboard.class.getResource("Dashboard.fxml"));
//Stage einrichten
stage.setScene(new Scene(pane));
stage.setTitle("");
stage.setResizable(false);
stage.show();
}
}
If you want to set the text of a TextField during loading, you have to get the ID by using the lookup() function(in JavaFX):
initialize(){
scene = Pane.getScene();
TextField sourceURL= (TextField) scene .lookup("#sourceURL");
sourceURL.setText("your text");
}

JavaFX use parsed variables during initialize

I know how to parse variables to controllers in JavaFX with fxml. But i need to use them in the initialize method of my controller. Is there a ways to do this? The background is, that i have a interface, where you can define different settings. Now you can safe them and have to be able to reopen them. So now when i open a rule, i need to set the values in the new option view. I know, that it works on text fields (UI-Elements) to set Text during initialize but not for variables. I tried different approaches. Like binding with properties (works for visibility property of button (UI-Element) but not for variables to set. Do you know a way or maybe an other approach?
Here is my example:
Controller1:
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
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.Label;
import javafx.stage.Stage;
/**
*
* #author Sandro
*/
public class FXMLDocumentController implements Initializable {
#FXML
private Button btn_openWindow;
#FXML
private void handleButtonAction(ActionEvent event) {
try {
Stage stage = new Stage();
FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(getClass().getResource("fxml_second.fxml").openStream());
Fxml_secondController cont = (Fxml_secondController)loader.getController();
cont.setFlag(0x00000002);
cont.setIsChange(false);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
btn_openWindow.setOnAction(this::handleButtonAction);
}
Controller 2:
/**
* FXML Controller class
*
* #author Sandro
*/
public class Fxml_secondController implements Initializable {
#FXML private Button btn_printOut;
private boolean isChange = true;
private int flag = 0x00000001;
private void printOut(ActionEvent event){
System.out.println("isChange: "+isChange);
System.out.println("flag: "+flag);
}
public boolean isIsChange() {
return isChange;
}
public void setIsChange(boolean isChange) {
this.isChange = isChange;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
#Override
public void initialize(URL url, ResourceBundle rb) {
btn_printOut.setOnAction(this::printOut);
System.out.println(flag);
}
In controller 2 you see the problem. The console-output in initialize shows 1 but it need to show 2. If i klick on printOut (Button) it prints out the right values which i have set in Controller 1.
Set the controller in the Java code, instead of in FXML.
Remove the fx:controller attribute from fxml_second.fxml, and change the code in FXMLDocumentController as follows:
#FXML
private void handleButtonAction(ActionEvent event) {
try {
Stage stage = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("fxml_second.fxml"));
Fxml_secondController cont = new Fxml_secondController();
cont.setFlag(0x00000002);
cont.setIsChange(false);
loader.setController(cont);
Parent root = loader.load();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
Another option would be to use a custom component approach for the second fxml.

Pointer exception in JavaFx when trying to recplace scene content

I just started Java and getting some GUI with Fx but I get an null pointer exception when trying to replace my scenecontent.
In the main the Login.fxml is loaded without any problem but when I click to the button for switching to another fxml I get the error.
The main:
package application;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
private Stage stage;
#Override
public void start(Stage primaryStage) {
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("Login.fxml"));
} catch (IOException e) {
e.printStackTrace();
return;
}
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private void gotoMainView() {
try {
MainViewController profile = (MainViewController) replaceSceneContent("MainView.fxml");
profile.setApp(this);
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
void Userlogin()
{
gotoMainView();
}
private void gotoLogin() {
try {
LoginController login = (LoginController) replaceSceneContent("Login.fxml");
login.setApp(this);
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
private Initializable replaceSceneContent(String fxml) throws Exception {
FXMLLoader loader = new FXMLLoader();
InputStream in = Main.class.getResourceAsStream(fxml);
loader.setBuilderFactory(new JavaFXBuilderFactory());
loader.setLocation(Main.class.getResource(fxml));
AnchorPane page;
try {
page = (AnchorPane) loader.load(in);
} finally {
in.close();
}
Scene scene = new Scene(page, 800, 600);
stage.setScene(scene);
stage.sizeToScene();
return (Initializable) loader.getController();
}
}
The Logincontroller class:
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
public class LoginController extends AnchorPane implements Initializable {
#FXML
static private Button btntext;
#FXML
static private TextField loginusr;
#FXML
static private PasswordField loginpwd;
private Main application;
public void setApp(Main application){
this.application = application;
}
#Override
public void initialize(URL location, ResourceBundle resources) {
btntext.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
System.out.println(loginusr.getText());
System.out.println(loginpwd.getText());
application.Userlogin();
}
});
}
The error occurs at:
application.Userlogin();
Many thanks in advance
The initialize() method is called as soon as the FXML is loaded. Have a look here
Your initialize() is called before setApp(Main application). At this point of time, the application object has not been initialized and is null !
This means the initialize() method is called at
MainViewController profile = (MainViewController) replaceSceneContent("MainView.fxml");
whereas application object which is used inside the initialize() is instantiated after this line, when you are using
profile.setApp(this);
So when you are trying the code application.Userlogin(); inside initalize(), at the time of execution, application is null !

Categories