【问题标题】:Search Result in new Window (JavaFx)在新窗口中搜索结果 (JavaFx)
【发布时间】:2017-11-29 13:12:11
【问题描述】:

我目前正在开发一个软件模块,该模块包括使用 JavaFx 在数据库中进行搜索。 一切都按预期工作。 但唯一的问题是,在我的结果表中,我只显示了很少的搜索细节(来自 UX 问题:我有太多细节和长文本)。 我想做的是显示一个新窗口,其中包含 TextFields 和 TextArea 中单击行的所有详细信息。 我查看了几个教程和答案,但不幸的是没有任何效果。 任何帮助,将不胜感激! 谢谢。

SearchResult.setOnMouseClicked(new EventHandler<MouseEvent>() {
        Gemo temp;
        @Override
        public void handle(MouseEvent event) {
            Gemo row = SearchResult.getSelectionModel().getSelectedItem();
            if(row == null) {
                System.out.print("I am not in");
                return ;
            }
            else {
                temp = row;

                String id = String.format(temp.getRef());
                System.out.println(id);
                FXMLLoader loader=new FXMLLoader();
                loader.setLocation(getClass().getResource("DetailsWindow.fxml"));
                ControllerDetails gemodetails=loader.getController();
                ControllerDetails gd=new ControllerDetails();
                gd.SearchById(id);
                try{
                    loader.load();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Parent p= loader.getRoot();
                Stage stage=new Stage();
                stage.setTitle("More Details");
                stage.setScene(new Scene(p));
                stage.show();




            }


        }
    });




public class ControllerDetails {
    @FXML
    private TextField fn_patient;

    @FXML
    private TextField ln_patient;

    @FXML
    private TextField db_patient;

    @FXML
    private TextField id_patient;

    @FXML
    private TextField id_visit;

    @FXML
    private TextField date_visit;

    @FXML
    private TextField fn_user;

    @FXML
    private TextField ln_user;

    @FXML
    private TextField status;
    @FXML
    private TextArea com;

    @FXML
    public void initialize(){

    }

    public void SearchById(String id) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {

            connection = ConnectionConfiguration.getConnection();
            statement = connection.prepareStatement("my_sql_query");
            rs = statement.executeQuery();
            while (rs.next()) {


                id_visit.setText(rs.getString(1));
                id_patient.setText(rs.getString(2));
                date_visit.setText(rs.getString(3));
                com.setText(rs.getString(4));
                fn_patient.setText(rs.getString(5));
                ln_patient.setText(rs.getString(6));
                db_patient.setText(rs.getString(7));
                fn_user.setText(rs.getString(8));
                ln_user.setText(rs.getString(9));
                status.setText(rs.getString(10));



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

【问题讨论】:

标签: mysql javafx tableview textfield


【解决方案1】:

尝试使用 listView 创建一个简单的 fxml 文件。把你感兴趣的数据放到这个listView中。如果你更喜欢 listView 以外的东西,那也没关系。 要在新窗口中打开此类 fxml,请尝试以下操作:

Stage s = new Stage();
try {
    s.setScene(new Scene(new FXMLLoader(getClass().getResource("your_fxml_name")).load()));
    s.show();
} catch (IOException e) {
    e.printStackTrace();
}

【讨论】:

  • 我做到了,但这不是我的问题。我想知道他们是否是一种从表中获取数据的方法
  • @GoSoft 目前还不清楚是什么阻止了你这样做。
【解决方案2】:

您有两个操作选项:提取所有数据并将其粘贴到表格中,或者提取您放入表格中的一小部分,然后按需提取额外的数据(显示详细信息)。

我给出的示例并没有对这种方法上瘾——它只是告诉了如何将表数据(选定的行)传输到对话框(其控制器)

public class Controller {

    @FXML
    private TableView<MySearchResult> tableView;

    @FXML
    private void initialize() {
        tableView.setItems(new Database().serach("query", "query"));

        tableView.setOnMouseClicked(event -> {
            if(event.getClickCount() == 2) {
                showDetailsDialog();
            }
        });
    }

    private void showDetailsDialog() {
        MySearchResult result = tableView.getSelectionModel().getSelectedItem();
        if(result == null) {
            return;
        }

        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("details_dilaog.fxml"));

            Dialog dlg = new Dialog();
            dlg.initOwner(tableView.getScene().getWindow());
            dlg.initModality(Modality.APPLICATION_MODAL);

            dlg.getDialogPane().setContent((Parent) loader.load());
            dlg.getDialogPane().getButtonTypes().setAll(ButtonType.OK);

            DetailsDialogControler ddc = loader.getController();
            ddc.showDetailsFor(result);

            dlg.showAndWait();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我的目标是展示showDetailsDialog() 函数中的逻辑。

【讨论】:

  • 实际上它对应于我所做的,只是我提取了访问的ID,并以这个ID作为参数调用了第二个控制器的函数。当我双击该行时,一切顺利并得到我的窗口,但我的文本字段没有被数据库中的数据填充。我真的不明白为什么。
  • @GoSoft 因为你没有调用SearchById。此外,您无法正常使用对话框控制器。仔细查看我示例中的操作顺序。
  • 感谢您的快速回答,我会检查所有这些
  • @GoSoft 1. 创建FXMLLoader 的实例。 2.调用load()函数创建Parent实例。 3.调用getController()获取与生成的Parent对象关联的控制器实例。 4. 调用我们收到的实例所需的控制器设置器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
  • 2011-01-19
  • 2020-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多