-1

I am currently working on a software module consisting of searching in a database using JavaFx. Everything is working as expected. But the only problem is that in my result table I am showing only few details of search (from UX issues: I have too much details and long texts). What I would like to do is to show a new window with all details of a clicked row in TextFields and TextArea. I looked at several tutorials and answers, but unfortunately nothing is working. Any help would be appreciated! Thank you.

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();
        }
     }
 }
GoSoft
  • 1
  • 2
  • 2
    You need to put more effort in your question. Show what you have done so far, logs, code, etc. https://stackoverflow.com/help/how-to-ask – Rolo Nov 29 '17 at 13:19
  • Why don't you use one of the techniques described in https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml? – James_D Nov 29 '17 at 16:54

2 Answers2

0

Try to create a simple fxml file with a listView. Place the data you are interested in into this listView. If you prefer something else than listView, that's ok too. To open such fxml in a new window try something like that:

Stage s = new Stage();
try {
    s.setScene(new Scene(new FXMLLoader(getClass().getResource("your_fxml_name")).load()));
    s.show();
} catch (IOException e) {
    e.printStackTrace();
}
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
P.P.
  • 11
  • 3
  • I did it but it's not really my issue. I would just like to know if their is a way to get from a table where few data from database is extracted to show all data in another window with full data – GoSoft Nov 29 '17 at 13:59
  • @GoSoft It is unclear what is preventing you from doing that. – James_D Nov 29 '17 at 15:58
0

You have two action options: either extract all the data and paste it into a table, or extract a small part that you put in a table, and extract the extra on demand (display details).

The example I give is not addicted to the approach - it simply tells how to transfer table data (a selected row) to a dialog (its controller)

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();
        }
    }
}

My goal is to show the logic in the showDetailsDialog() function.

mr mcwolf
  • 2,574
  • 2
  • 14
  • 27
  • Actually it corresponds to what I did except that I extracted the ID of a visit and I called the function of the second controller with this ID as parameter. Everything goes well and get my window when I double-click on the row but my textfields do not get filled with the data from the database. I really don't understand why. – GoSoft Nov 29 '17 at 16:16
  • @GoSoft because you do not call `SearchById`. Also, you do not work properly with the dialog box controller. review carefully the action sequence in my example. – mr mcwolf Nov 29 '17 at 16:40
  • Thank you for your quick answer, I will check all this – GoSoft Nov 29 '17 at 16:42
  • @GoSoft 1. create an instance of `FXMLLoader`. 2. Call the `load()` function to create a `Parent` instance. 3. Call `getController()` to get an instance of the controller associated with the generated `Parent` object. 4. Call the desired controller setter whose instance we received. – mr mcwolf Nov 29 '17 at 16:50