0

Alright guys, I have been working a lot with SWING but recently started learning some FX. I have had good progress with it so far, I've been learning a lot. In Swing it was quite easy to declare a global variable and then use it across all your frames without any issues or whatsoever. However in JavaFX its quite a problem.

I have searched for this problem everywhere but I just cant seem to find the right answer.

So I got a few textfields that are taking input from the user, I'd like to save the input and display it on a new textfield in a new scence.

Here is the code

FirstController.java

private void reggy(ActionEvent event) {
      try { 
      String username_text = username.getText().trim();

    String password_text = password.getText().trim();

        Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/store","root","password"); 
        Statement st = (Statement) conn.createStatement(); 
        String sql = "select username, password from users where username ='"+username_text+"' and password ='"+password_text+"'";
        ResultSet rs;
        rs = st.executeQuery(sql);




int count = 0;
   while(rs.next()) {
       count = count +1;
   }
   if(count == 1){
       AnchorPane pane = FXMLLoader.load(getClass().getResource("second.fxml"));
       rootpane.getChildren().setAll(pane);
       System.out.println("User Found, Logged in");

   }

Now the main question is how I can get the variable "String username_text" ready for use in a different FXML document (second.fxml)

Thanks a lot in advance. Really appreciate the help. A beginner here trying to work his way through FX.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Freakz0r
  • 53
  • 1
  • 12

2 Answers2

0

This doesn't really have anything to do with FXML, but Java in general.

In my applications, when a need for this arises, I use a Singleton class to hold global variables:

Global.java:

public final class Global {

    private static String usernameText;

    private Global(){}  // Private constructor to prevent instantiation

    public static String getUsernameText() {
        return usernameText;
    }

    public static void setUsernameText(String usernameText) {
        Global.usernameText = usernameText;
    }
}

From that point on, you can set/get the value from anywhere in your application:

Global.setUsernameText("my_username");
String username = Global.getUsernameText();

This is, obviously, a very simple example. Check out this article to learn more about singletons and some of the disadvantages to be aware of.

Zephyr
  • 9,885
  • 4
  • 28
  • 63
  • This doesn't solve my problem, At least I don't think so. If you read my code, You can see the variable username_text. I want it to be a global variable. How can I do that? – Freakz0r May 26 '18 at 00:45
  • This is exactly a "global variable." From within your first controller, simply call `Global.setUsernameText(username_text)` from within your first controller. This really isn't specific to JavaFX as it's a common practice to accomplish your goal of using the value in another class. That being said, Chaoz's answer below is also a viable solution. It all depends on your needs. – Zephyr May 26 '18 at 00:56
0

Update 1

You should replace this part if your code:

if(count == 1){
    AnchorPane pane = FXMLLoader.load(getClass().getResource("second.fxml"));
    rootpane.getChildren().setAll(pane);
    System.out.println("User Found, Logged in");
}

with this:

if(count == 1){
    FXMLLoader loader = new FXMLLoader(getClass().getResource("second.fxml"));
    AnchorPane pane = loader.load();
    SecondController controller = (SecondController) loader.getController();
    controller.updateUsernameText(username_text);
    rootpane.getChildren().setAll(pane);
    System.out.println("User Found, Logged in");
}

(replace SecondController with the class name for your second controller if needed)

Then all that's left to do is to create this method inside the second controller class:

void updateUsernameText(String username_text) {
    // your code here
}

On a side note, I suggest you use the java naming conventions, search for that on google :)

In a nutshell, variable names should not contain underlines, each word should have first letter capitalized (except first word), like this: usernameText. Same for methods, except they should start with a verb. And classes should have the first letter capitalized too (you've already done that I think, not sure for second controller).

Old post

A different approach to this (better than a global class IMO, since it allows multiple instances and can't be accessed from outside) is to simply pass the text from one class to another using a method.

First, add a method in the second controller to allow passing the text:

void setText(String text) {
    textField.setText(text);
}

Then add this inside the first (main) controller:

FXMLLoader loader = new FXMLLoader(...);
Scene scene = new Scene(loader.load());
SecondController second = (SecondController) loader.getController();
second.setText(someText);
Chaoz
  • 2,119
  • 1
  • 20
  • 39
  • I have a textfield in my first FXML file which takes input from the user, I just want that stored 'input' to be carried to the new scence. I don't see how the code you provided would help with this. I'm sorry if I'm wrong, I'm really new with FX> – Freakz0r May 26 '18 at 01:06
  • @Freakz0r Check my updated answer. – Chaoz May 26 '18 at 01:35
  • Well I tried your new updated code however its giving out an error loader.updateUsernameText(username_text);, Can you explain this line.. This line is giving out an error (cannot find symbol) – Freakz0r May 26 '18 at 13:18
  • @Freak0rz Yeah, sorry for that, wrote it on phone. Updated it, try again and it should work. – Chaoz May 26 '18 at 13:45
  • Oh god. Thanks a ton mate. Really appreciate it. It works like a charm. Thanks again for helping me out, Its a blessing to have a community this experienced out there. – Freakz0r May 26 '18 at 14:09
  • No problem man :) You might wanna check this out: https://docs.oracle.com/javafx/2/get_started/jfxpub-get_started.htm, this helped me get started with javafx. Also keep in mind the thing I said about java convetions, it will help. – Chaoz May 26 '18 at 18:21