JavaFX正确地从KeyCombination创建一个新场景

时间:2016-04-18 19:00:41

标签: model-view-controller javafx scene

我不喜欢菜单栏,所以我将我的应用程序限制为KeyCombinations以生成事件。我有键组合工作,但我有一种感觉我没有正确使用控制器。我需要在我的控制器中编辑新场景,但我发现自己在KeyCombination事件的try / catch中这样做。

我想在类SettingsController.java中对场景进行所有更改


我在哪里创建新的场景/视图

final KeyCombination settingsCMD = new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN);
scene.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>()
{
    @Override
    public void handle(KeyEvent event)
    {
        if (settingsCMD.match(event))
        {
            System.out.println("CTRL + S was pressed on " + name + " display\n" +
                    "Opening Settings Scene");
            /*
             *  This is where we need to launch a scene for settings
             */
            try
            {
                Parent root = FXMLLoader.load(getClass().getResource("/sample/view/settingsscreen.fxml"));
                Stage settingsStage = new Stage();
                settingsStage.setTitle("Settings");
                settingsStage.setScene(new Scene(root, 500 , 400));
                settingsStage.show();

                // This really needs to be done in the controller. How do I do this?
                JSON jsonTools = new JSON();
                jsonTools.readJSONSettings();
                jsonTools.writeJSONSettings();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
});


SettingsController.java

public class SettingsController
{

    @FXML private TextField hostname;


    public String getText()
    {
        String textProp = textProperty().get();
        System.out.println("testProperty is " + textProp + "\n");
        return textProp;
    }

    public void setText(String value)
    {
        textProperty().set(value);
    }

    private StringProperty textProperty()
    {
        return hostname.textProperty();
    }

}

1 个答案:

答案 0 :(得分:1)

您可以从FXMLLoader

获取对视图控制器类的引用
    FXMLLoader loader = new FXMLLoader(getClass().getResource(resource));
    Scene scene = new Scene(loader.load());
    parentStage.setScene(scene);

    Controller controller = loader.getController();

在此参考文献中,您可以调用相应的方法

final KeyCombination settingsCMD = new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN);
scene.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>()
{
    @Override
    public void handle(KeyEvent event)
    {
        if (settingsCMD.match(event))
        {
            System.out.println("CTRL + S was pressed on " + name + " display\n" +
                    "Opening Settings Scene");
            /*
             *  This is where we need to launch a scene for settings
             */
            try
            {
                FXMLLoader loader = new FXMLLoader.load(getClass().getResource("/sample/view/settingsscreen.fxml"));
                Parent root = loader.load();
                SettingsController controller = loader.getController();

                Stage settingsStage = new Stage();
                settingsStage.setTitle("Settings");
                settingsStage.setScene(new Scene(root, 500 , 400));
                settingsStage.show();

                controller.yourMethod();

            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
});