I have an application in JavaFX FXML by using Model View Controller. I want to move some sliders with keyboard. To do this in the main class I put a KeyEvent to hear what happens on the keyboard. In the controller class FXMLDocumentController where are FXML variables. These variables are passed to a third class. Where there sliders are changed when you click any.

The problem is that when I pass the variables of the sliders in the third class are perfectly stored but when you run the code to modify sliders coming past the main class when clicked are FXML variables are null.

Here you have the code:

Main class:

public class OpenPilot extends Application {

    Movements Movements = new Movements();

    @Override
    public void start(Stage stage) throws Exception {

        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));


        Scene scene = new Scene(root);

        scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent Key) {
               Movements.GetKeys(Key);
            }
        });

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

Controller:

public class FXMLDocumentController implements Initializable {

    Movements Movements = new Movements();

    @FXML public Slider SpeedSlider;
    @FXML public Slider TurnsSlider;





    @Override
    public void initialize(URL url, ResourceBundle rb) {

        //Send GUI Information
        Movements.GetSliders(SpeedSlider, TurnsSlider);
    }    

}

Movements:

public class Movements {

    //Define Data Variables
    public double SpeedValue;
    public double TurnsValue;

    //Define GUI Variables
    private Slider SpeedSlider;
    private Slider TurnsSlider;

    public void GetSliders(Slider SpeedSlider, Slider TurnsSlider) {
        this.SpeedSlider = SpeedSlider;
        this.TurnsSlider = TurnsSlider;
}

    //Get Sliders

    public void GetKeys(KeyEvent Key) {
        System.out.println(Key.getCode());
        System.out.println(SpeedSlider);

        Platform.runLater(new Runnable() {
                @Override public void run() {
                    TurnsSlider.setValue(10);
                }
        });
    }

}
有帮助吗?

解决方案

You use 2 different instances of the Movements class (Movements Movements = new Movements(); in OpenPilot and FXMLDocumentController). Since you don't use static fields / methods, you won't get any data from one instance to the other. You have to get the Movements object from the controller:

Controller:

public class FXMLDocumentController implements Initializable {

    Movements movements = new Movements();

    public Movements getMovements() {
        return movements;
    }

    @FXML public Slider SpeedSlider;
    @FXML public Slider TurnsSlider;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        //Send GUI Information
        movements.GetSliders(SpeedSlider, TurnsSlider);
    }    

}

Use a instance of the FXMLLoader instead of the static method to get the Movements field from the controller:

Main class:

public class OpenPilot extends Application {

    Movements movements;

    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader fxmlLoader = new FXMLLoader();
        // use non-static load method here
        Parent root = fxmlLoader.load(getClass().getResource("FXMLDocument.fxml").openStream());

        // get movements from via controller
        FXMLDocumentController controller = fxmlLoader.<FXMLDocumentController>getController();
        movements = controller.getMovements();

        Scene scene = new Scene(root);

        scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent Key) {
               movements.GetKeys(Key);
            }
        });

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top