【发布时间】:2015-05-28 22:01:00
【问题描述】:
我需要根据客户端输入更新我的 GUI。从后台任务调用我的控制器类方法有效。但它不能更新 GUI,因为它不是 JavaFX 应用程序线程..请帮助。
我尝试了许多相关的Q & A,但我仍然感到困惑。
我应该使用 Platform. runLater 还是 Task ?
这是我创建controller 类实例的类
public class FactoryClass {
public static Controller_Gui1 createGUI() {
FXMLLoader fxLoader = new FXMLLoader();
fxLoader.setLocation(MainApp_Gui1.class.getResource("/com/Gui_1.fxml"));
AnchorPane anchorPane = null;
try {
anchorPane = (AnchorPane) fxLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
Controller_Gui1 controller_Gui1 = (Controller_Gui1) fxLoader
.getController();
Scene scene = new Scene(anchorPane);
//System.out.println(scene);
controller_Gui1.setScene(scene);
return controller_Gui1;
}
}
控制器类
@FXML
Button B1 = new Button();
@FXML
public void handleButton1() {
B1.setDisable(true);
}
应用类
public class MainApp_Gui1 extends Application {
Controller_Gui1 cGui;
@Override
public void start(Stage primaryStage) throws Exception {
initScene(primaryStage);
primaryStage.show();
System.out.println("asdasd");
SceneSetting sceneSetting = new SceneSetting();
//handleEvent();
System.out.println("after");
sceneSetting.setSceneAfter();
System.out.println("after2");
}
// creating scene
private void initScene(Stage primaryStage) throws IOException {
primaryStage.setScene(getScene(primaryStage));
}
public Scene getScene(Stage primaryStage) {
Controller_Gui1 cGui;
cGui = FactoryClass.createGUI();
return cGui.getScene();
}
public void ExcessFromOutside() {
Platform.runLater(new Runnable() {
public void run() {
System.out.println(Platform.isFxApplicationThread());
cGui.handleButton1();
}
});
}
public static void main(String[] args) {
launch(args);
}
}
我想从另一个线程调用ExcessFromOutside() 方法。
尝试更新 GUI 时出现空指针异常 这是我的应用程序类
public class MainAppGui1 extends Application {
Controller_Gui1 controller_Gui1;
@Override
public void start(Stage primaryStage) throws Exception {
initScene(primaryStage);
primaryStage.show();
}
// creating scene
public void initScene(Stage primaryStage) throws IOException {
FXMLLoader fxLoader = new FXMLLoader();
fxLoader.setLocation(MainApp_Gui1.class.getResource("/com/Gui_1.fxml"));
AnchorPane anchorPane=new AnchorPane();
anchorPane = (AnchorPane) fxLoader.load();
Controller_Gui1 controller_Gui1 = (Controller_Gui1) fxLoader.getController();
Scene scene = new Scene(anchorPane);
primaryStage.setScene(scene);
}
@FXML
public void ExcessFromOutside()
{
Platform.runLater(new Runnable() {
@Override
public void run() {
System.out.println("called atleast");
controller_Gui1.handleButton1();
}
});
}
public static void main(String[] args) {
launch(args);
}
}
这是我尝试更新 GUI 的类
public class Hudai {
public static void main(String args[]) throws InterruptedException
{
new Thread()
{
public void run()
{
MainAppGui1.main(null);
}
}.start();
Thread.sleep(5000);
MainAppGui1 m = new MainAppGui1();
m.ExcessFromOutside();
}
}
【问题讨论】:
-
您能否添加一些代码来向我们展示究竟是什么不适合您?
-
我应该使用任务吗?这是我的第一篇文章……所以考虑一下……
-
你为什么使用
MainAppGui1.main(null);? -
您可以直接从任何类启动 GUI。请转至Starting JavaFX from Main method of class which doesn't extend Application
-
谢谢。但这不能解决我的问题。仍然得到空指针异常
标签: model-view-controller javafx javafx-8