JavaFX 从 KeyCombination 正确创建新场景
JavaFX creating a new scene from KeyCombination correctly
我不喜欢菜单栏,所以我将我的应用程序限制为 KeyCombinations 以产生事件。我可以使用组合键,但我感觉我没有正确使用控制器。我需要在我的控制器中编辑新场景,但我发现自己是在 KeyCombination 事件的 try/catch 中这样做的。
我想对 class 中的场景进行所有更改 SettingsController.java
我在哪里创建一个新的 scene/view
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();
}
}
您可以从 FXMLLoader
:
获取对视图控制器 class 的引用
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();
}
}
}
});
我不喜欢菜单栏,所以我将我的应用程序限制为 KeyCombinations 以产生事件。我可以使用组合键,但我感觉我没有正确使用控制器。我需要在我的控制器中编辑新场景,但我发现自己是在 KeyCombination 事件的 try/catch 中这样做的。
我想对 class 中的场景进行所有更改 SettingsController.java
我在哪里创建一个新的 scene/view
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();
}
}
您可以从 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();
}
}
}
});