根据另一个 tableCell (ComboBox) JavaFx 中的值编辑 TableCell 的内容

发布于 2025-01-12 08:50:50 字数 2041 浏览 1 评论 0原文

我有一个 TableView(如下),我希望用户从 ComboBox 中选择一个项目,然后使用数据库中的值填充接下来的两个单元格(描述和项目成本)。尽管进行了大量的谷歌搜索和头脑风暴,我还没有弄清楚如何访问特定单元格来更改它们的值/文本。当然,invoiceClassDescription.setText() 只是更改列标题,这不是我想要的。

我尝试过 setCellSelectionEnabled(true) 和 invoiceTable.getSelectionModel().getSelectedCells(); 来获取组合框单元格的索引,并可能推断接下来两个单元格的索引就这样,但一切很快就变得非常复杂。

TableView

这是 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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文