0

I'm working on a simple object database app that displays various attributes of the entries in a class in a table and allows the user to add a new entry through a secondary input window.

Main:

public class Main extends Application {

        Stage theStage;
        Scene mainWindowController;

        @Override
        public void start(Stage primaryStage) throws Exception{
            theStage = primaryStage;

            Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
            primaryStage.setTitle("Steam Database");

            mainWindowController = new Scene(root, 800, 550);

            primaryStage.setScene(mainWindowController);
            primaryStage.show();
        }

        public static void main(String[] args) {
            launch(args);
        }
    }

A controller for one of the secondary windows:

import static sample.MainWindowController.*;

public class CreateDeveloperWindowController {
/*
*/
@FXML
private TextField newDeveloperNameTextField;

@FXML
private TextField newDeveloperPassTextField;

    private void handleCreateDeveloperButton() {
        String proposedNewDevName = newDeveloperNameTextField.getText();
        String proposedNewDevPass = newDeveloperPassTextField.getText();
        if (proposedNewDevName.equals("") || proposedNewDevPass.equals("")) {
            mainWindowController.textMessageDisplay.setText("Name and password must not be empty!");
        } else {
            allDevelopers.add(new Developer(proposedNewDevName, proposedNewDevPass));
        }

    }
/*
*/
}

The problem is in the controller, in the line

mainWindowController.textMessageDisplay.setText("Name and password must not be empty!");

I'm getting a "Cannot resolve symbol" error on it, but I can't discern why. One solution is to add "Main." in front of it and declare the variable static, but that creates problems for other functionality that I want to add. So my questions are:

  1. Why is this even showing up? The mainWindowController variable is declared within the Main class, so it should be visible from anywhere in the application, so far as I'm aware.

  2. How do I solve this; how do I get that line to work?

fabian
  • 80,457
  • 12
  • 86
  • 114
Sargon1
  • 854
  • 5
  • 17
  • 48
  • There is no variable called `mainWindowController` defined in `CreateDeveloperWindowController`, so you get the error. – James_D Apr 07 '16 at 12:37
  • But shouldn't CreateDeveloperWindowController see it in the Main class? Even if I import it, it gives the "non-static variable cannot be referenced from a static context" error, which can only be solved by declaring the variable static, which causes the problem I mentioned in the question - it can't be static because it gets in the way of other stuff I'm going to add – Sargon1 Apr 07 '16 at 12:49
  • *"But shouldn't CreateDeveloperWindowController see it in the Main class?"* No, why would it see it? That's simply not how Java (or indeed any other language I'm aware of) works. – James_D Apr 07 '16 at 13:00
  • As for your second question: "how do I get that line to work", it's not really clear what you're trying to do. Even if the compiler could magically infer which object `mainWindowController` belonged to, and resolve the variable, you declare it in `Main` to be of type `Scene`, and `Scene` does not have a public field called `textMessageDisplay`. It just seems like you have some major misconceptions about what classes and objects are, and how they work. You probably need to step back and learn some basics before trying to get into JavaFX. – James_D Apr 07 '16 at 13:17
  • You're right, I got things mixed up there. Still, it should see it if I reference the variable as "Main.mainWindowController...", no? – Sargon1 Apr 07 '16 at 13:18
  • That would reference a (public) `static` variable. `Main` is a class name, not an object reference. – James_D Apr 07 '16 at 13:18
  • 1
    **`static`** imports import **`static`** members of a class. To reference a non-`static` field of a class you need the instance containing it... How this instance could be passed to the `CreateDeveloperWindowController` is thoroughly described in the answers here http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – fabian Apr 07 '16 at 13:53

1 Answers1

0

You are really wrong my friend, you're so confused with instance variables and static variables. Anyway, you need to get a reference to the first class, here is a nice example https://blogs.oracle.com/acaicedo/entry/managing_multiple_screens_in_javafx1

Rene Enriquez
  • 1,418
  • 1
  • 13
  • 33