Edit contents of TableCell based on value in another tableCell (ComboBox) JavaFx

127 Views Asked by At

I have a TableView (below) where I would like the user to select an item from a ComboBox which then populates the next two cells (Description and item cost) with values from a Database. In spite of much googling and headbanging, I have not figured out how to access the specific cells to change their values/text. Of Course invoiceClassDescription.setText() just changes the column heading which is not what I want.

I have tried setCellSelectionEnabled(true) and invoiceTable.getSelectionModel().getSelectedCells(); to then get index of the combobox cell and perhaps infer the index of the next two cells that way but it all got very convoluted very quickly.

TableView

Here is the code for the ClassID ComboBox:

invoiceClassID.setCellValueFactory(new PropertyValueFactory<>("courseID"));
    invoiceClassID.setCellFactory(column -> new TableCell<InvoiceItem, ObservableList<String>>(){
        private final ComboBox<String> combo = new ComboBox<>(courseIDs);
        {
            combo.valueProperty().addListener((obs, oldValue, newValue) -> {
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    Connection conn = DriverManager.getConnection (DB_Updater.DB_URL,DB_Updater.USER,DB_Updater.PASS);
                    String sql = ("SELECT ClassName, TotalCost from Classes where CourseID = " + '"' + newValue + '"');
                    
                    ResultSet result = conn.createStatement().executeQuery(sql);
                    while(result.next()) {
                        
                        // set class description and cost to values in DB
                    }
                    conn.close();
                }
                catch (Exception e) {
                    System.out.println(e);
                }
            });
        }
        @Override
        protected void updateItem(ObservableList<String> items, boolean empty) {
            super.updateItem(courseIDs, empty);
            if(empty) {
                setGraphic(null);
            } else {
                combo.setItems(courseIDs);
                setGraphic(combo);
            }
        }
    });

I guess my question is, how does one gain access to a specific cell to edit its contents?

0

There are 0 best solutions below