java - 如何根据表中选定的行在 JavaFX 选择框中设置文本

标签 java events javafx getselection

我正在使用 JavaFx 开发票务系统。当用户在表中选择特定票证并单击“编辑”按钮时,所选行中的数据将加载到下面表单中的相应字段中。然后用户可以进行更改并更新信息。

Image showing the GUI I am referring to

但是,我在弄清楚如何将“状态”和“严重性”选择框中的文本设置为所选行中的文本时遇到问题。这是到目前为止我的编辑按钮的代码:

@FXML
    private void editButtonFired(ActionEvent event) {
        try {
            int value = table.getSelectionModel().getSelectedItem().getTicketNum();

            JdbcRowSet rowset = RowSetProvider.newFactory().createJdbcRowSet();
            rowset.setUrl(url);
            rowset.setUsername(username);
            rowset.setPassword(password);
            rowset.setCommand("SELECT * FROM s_fuse_ticket_table WHERE ticket_id = ?");
            rowset.setInt(1, value);
            rowset.execute();



            while(rowset.next()) {
                ticketNumber.setText(rowset.getString(1));
                summary.setText(rowset.getString(2));
            }
        }catch (SQLException e){

        }
    }

我尝试使用 .setSelectionModel() 方法,但这不起作用。有人可以帮助我吗? 谢谢!

最佳答案

调用choiceBox.setValue()设置选择框的值:

import javafx.scene.control.ChoiceBox;

ChoiceBox cb = new ChoiceBox();
cb.getItems().addAll("item1", "item2", "item3");
cb.setValue("item2");

后续问题的答案

So I have already set the values for the choice box in the fxml

可能不是。可能您已经设置了项目而不是值(这很好)。对于您的用例,您无法在 FXML 中设置该值,因为只有在用户选择主表中的相关行项目后,该值才会得知。

When I try to use the setValue() method to set the value retrieved from the table I get an error saying: incompatible types: String cannot be converted to CAP#1 where CAP#1 is a fresh type-variable: CAP#1 extends Object from capture of

我以前从未遇到过这样的错误消息。对于它的值(value),这里有一些信息:incompatible types and fresh type-variable ,尽管我承认我没有直接看到与您的情况的相关性。我的猜测是,您没有定义 ChoiceBox 的项目类型,或者将它们定义为字符串以外的其他内容。您可以使用以下方式显式设置类型:

ChoiceBox<String> cb = new ChoiceBox<>();

当您使用 FXML 时,选择框定义不会使用 new 关键字,而是类似于以下内容:

@FXML
ChoiceBox<String> cb;

如果您的 ChoiceBox 的类型不是 String,那么您可能需要 set a converter就在上面。

您的问题中有太多未知数,无法提供更具体的答案。

关于java - 如何根据表中选定的行在 JavaFX 选择框中设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40918081/

相关文章:

java - 无法在新场景中移动 TextFlow

JavaFX 同时创建两个阶段

java - 在 vert.x 中运行 verticle 的最佳方式是什么?

java - 从 JOptionPane 中的 JTextArea 获取输入

java - 使用 JPA 将对象上传到数据库时出现 NULL 指针异常

javascript - JQueryUI 菜单选择事件未触发

java - 如何定制测试报告?

javascript - 命名事件处理程序方法 "onEvent"的用例有哪些

c# - 如何跨对象链处理事件?

java - 为什么更改 JavaFX 组中一个形状的平移属性会在更改组的比例后影响其他形状