如何在JavaFX中从主窗口调用弹出窗口?
这也是我的第一篇帖子。这也是我第一个使用JavaFX的项目。我试图通过调用一些键来调用一个小的弹出窗口,比如F9键,它会在主窗口的文本字段中显示一个TableView。现在,我正在尝试从主窗口上的按钮调用弹出窗口。我当然希望将表视图中的值返回到主窗口,但这将在以后进行。我甚至调用我的弹出窗口都有问题 我已经在这里查看了各种示例和解决方案,但未能解决我的问题。我最终会在主窗口中调用3个弹出窗口 我的MVC架构下有以下文件: Main.java-src/sample文件夹 java-src/sample/controller文件夹 StudentDAO.java和sexDAO.java数据访问对象-src/sample/model文件夹 java公共类Student和constructor-src/sample/model文件夹 OJDBC-src/sample/util文件夹的util下的DBUtil 使用场景生成器创建的FXML文件-src/sample/view文件夹 RootLayout.fxml StudentView.fxml主窗口 genderpoup.fxml弹出窗口,带有显示记录的TableView Main.java StudentController.java StudentView.fxml 这是主窗口,其中有一个按钮,onAction正在调用showGenderPopup 因此,在这一阶段,我的学生记录在主窗口上表现良好。但是,当我按下弹出按钮试图调用弹出窗口时,我得到一个错误位置未设置。现在,我知道这不是一个与文件名的错误,它确实发生了类似的错误。我的路径正确,文件名也正确。我猜想,不知何故,作为孩子的弹出窗口无法找到家长。非常感谢您的帮助 错误: 原因:java.lang.IllegalStateException:未设置位置如何在JavaFX中从主窗口调用弹出窗口?,javafx,fxml,Javafx,Fxml,这也是我的第一篇帖子。这也是我第一个使用JavaFX的项目。我试图通过调用一些键来调用一个小的弹出窗口,比如F9键,它会在主窗口的文本字段中显示一个TableView。现在,我正在尝试从主窗口上的按钮调用弹出窗口。我当然希望将表视图中的值返回到主窗口,但这将在以后进行。我甚至调用我的弹出窗口都有问题 我已经在这里查看了各种示例和解决方案,但未能解决我的问题。我最终会在主窗口中调用3个弹出窗口 我的MVC架构下有以下文件: Main.java-src/sample文件夹 java-src/samp
谢谢您的路径不正确。您试图从StudentController类而不是主类获取FXML资源。如您所述,您的项目结构包括: /sample/Main.java /示例/controller/StudentController.java /sample/view/genderpoup.fxml 因为您调用了getClass.getResource。。。在StudentController内部,没有前导/的路径相对于StudentController的位置。这意味着路径最终解决为: /sample/controller/view/genderpoup.fxml 这是不存在的
如果使用StudentController.class查询资源,则需要使用/sample/view/genderpoup.fxml作为路径。感谢Slaw解释为什么它不起作用。这很有道理。这能回答你的问题吗链接在这一次之后发布的副本,因为它更详细
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Sample JavaFX App");
initRootLayout(); // Initialize RootLayout
showStudentView();
}
//Initializes the root layout.
public void initRootLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show(); //Display the primary stage
} catch (IOException e) {
e.printStackTrace();
}
}
//Shows the Patient inside the root layout.
public void showStudentView() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/StudentView.fxml"));
AnchorPane PatientView = (AnchorPane) loader.load();
rootLayout.setCenter(PatientView);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
package sample.controller;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class StudentController {
@FXML private TextField studentIdText;
@FXML private TextField lastNameText;
@FXML private TextField firstNameText;
@FXML private TextField sexText;
@FXML private Button popupButton;
@FXML
private void initialize () {
// I have additional listeners here doing different things that I have excluded which do pretty much similar things as shown below
sexText.focusedProperty().addListener((arg0, oldPropertyValue, newPropertyValue) -> {
if (!newPropertyValue) {
try {
searchSexDescription();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
// Function to Search Sex Description
@FXML
private void searchSexDescription() throws ClassNotFoundException, SQLException {
try {
if (!sexText.getText().isEmpty()) {
// Get Gender Description
Patient sex = SexDAO.searchSex(sexText.getText());
populateAndShowSexDescription(sex);
}
} catch (SQLException e) {
System.out.println("Exception raised in searchSexDescription");
e.printStackTrace();
throw e;
}
}
@FXML
public void showGenderPopup() throws IOException {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("view/GenderPopup.fxml"));
Parent root = (Parent) fxmlLoader.load();
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}