java从控制器外部操纵FXML节点和应用程序类
我需要操作控制器类中的一个元素。然而,我找到的每一个例子都是通过扩展应用程序的类来进行这种操作的。
我需要在类Main中操作来自VBoxSampleController的文本字段inputText。
操作如下:
inputText.setText(“TEST”);
所需的结果是,一旦FXML应用程序启动,inputText文本字段就会预先填充文本“TEST”。
我的困惑源于如何在任何地方操纵这个领域,但:
- VBoxSampleController
- UIManager
将VBoxsampleController设置为静态,以便我可以使用类名访问它:
UIManager.vbsc.inputText.setText("TEST");
什么都不做。启动的FXML窗口中的元素不会更改。它仍然是空的
这个项目取自https://examples.javacodegeeks.com/desktop-java/javafx/javafx-applications-efxclipse/,因为我现在无法创建新的FXML应用程序。
谢谢
完整代码:
/* Main */
public class Main {
public static void main(String[] args) {
UIManager.main(args);
// Does nothing
// UIManager.vbsc.inputText.setText("TEST");
// I want to change inputText here
}
}
/* UIManager */
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.fxml.FXMLLoader;
public class UIManager extends Application {
public static VBoxSampleController vbsc;
public FXMLLoader loader;
@Override
public void start(Stage primaryStage) {
try {
loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("VBoxSample.fxml"));
VBox root = loader.load();
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
vbsc = loader.getController();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
/* VBoxController */
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
public class VBoxSampleController {
@FXML
// The reference of inputText will be injected by the FXML loader
public TextField inputText;
// The reference of outputText will be injected by the FXML loader
@FXML
private TextArea outputText;
// location and resources will be automatically injected by the FXML loader
@FXML
private URL location;
@FXML
private TabPane mainPane;
@FXML
private ResourceBundle resources;
// Add a public no-args constructor
public VBoxSampleController() {
}
@FXML
private void initialize() {
}
@FXML
private void printOutput() {
outputText.setText(inputText.getText());
}
}
<!-- FXML -->
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="VBoxSampleController">
<children>
<Label alignment="CENTER_LEFT" cache="true" cacheHint="SCALE" prefHeight="30.0" prefWidth="200.0" text="Please insert Your Input here:" textAlignment="LEFT" />
<TextField fx:id="inputText" prefWidth="100.0" />
<Button alignment="CENTER_RIGHT" contentDisplay="CENTER" mnemonicParsing="false" onAction="#printOutput" text="OK" textAlignment="CENTER" />
<Label alignment="CENTER_LEFT" cache="true" cacheHint="SCALE" prefHeight="30.0" prefWidth="200.0" text="Your Input:" textAlignment="LEFT" />
<TextArea fx:id="outputText" prefHeight="100.0" prefWidth="200.0" wrapText="true" />
</children>
</VBox>
# 1 楼答案
在HomeController类的初始化方法中,编写:
在StageControl类的initialize方法中:
# 2 楼答案
我认为发生这种情况是因为您试图从非静态方法修改静态变量:
如果我猜对了,那么您正在尝试在应用程序启动时动态更新FXML字段。因此,您可以通过使用控制器的initialize()方法简单地实现这一点:
# 3 楼答案
您的问题似乎源于这样一个事实:您在主线程上进行更新,而不是在平台线程上。这里有两个问题
UIManager.vbsc.inputText.setText("TEST");
李>试试看