gpt4 book ai didi

JavaFX tableview 按组合框所选项目过滤

转载 作者:太空宇宙 更新时间:2023-11-04 10:54:13 26 4
gpt4 key购买 nike

我是 javafx 和场景构建器方面的菜鸟。我想通过从组合框中选择一项来填充表格 View 。有可能的?

我尝试使用 String val = Combobox.getValue() 并将字符串放入 SQL 查询中的 preparedStatement 中以直接排序,但应用程序停止在空字符串值处,并且 TableView 未更新。

谢谢大家!

最佳答案

甚至在 ComboBox 获取输入之前,字符串也可能已用 ComboBox 值进行初始化。在这种情况下,ComboBox 将返回空值。

您应该为 ComboBox 添加一个 onAction 事件,该事件将更新字符串。您可以使用以下代码段来执行此操作

comboBox.setOnAction((event) -> {
val = comboBox.getValue();
//Any other action you want to carry out when an item of the combo box is selected
});

或者,如果您使用 FXML 文件并希望在 Controller 中添加 onAction 事件,则可以使用此方法。

public void comboBoxEvent(ActionEvent event){
val = comboBox.getValue();
} // Use this code when working with FXML files

这两个示例都假设 String var 是全局定义的。为了安全起见,当您将 var 与另一个值进行比较或将其存储在其他位置时,您应该将其放在 if 条件下

if(var != null)
//Code segment here

关于JavaFX tableview 按组合框所选项目过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47464545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com