gpt4 book ai didi

Javafx:如何将 ComboBox 项目动态绑定(bind)到从数据库更新自身的可观察列表

转载 作者:行者123 更新时间:2023-12-02 10:05:13 25 4
gpt4 key购买 nike

我正在尝试将我的 javafx 组合框项目绑定(bind)到可观察列表;当列表更新时,组合框项目也会更新(添加、删除或修改)。我尝试将监听器添加到组合框项目,但仍然收到“不在 FX 应用程序线程上”异常。这是我的代码:

型号

{

private ObservableList<String> programList = FXCollections.observableArrayList();


some code initialize programList from database


private ListProperty<String> programListProperty = new SimpleListProperty<>(programList);


some code update programList periodically


}

Controller

{
@FXML ComboBox programComboBox;


model.programListProperty().addListener((v, oldValue, newValue) ->
Platform.runLater(() -> {
programComboBox.getItems().clear();
programComboBo.getItems().add(every item in model.programList);
}));
}

我也尝试过这种方法,但都行不通

{
@FXML ComboBox programComboBox;

programComboBox.itemsproperty().bind(model.programListProperty());

}

最佳答案

Note: This solution makes some assumptions about your implementation, as you have not provided a Minimal, Complete, and Verifiable Example of your code.

<小时/>

您不需要为此使用绑定(bind)。 ComboBox 使用 ObservableList 来填充其项目。这里的关键字是可观察。这意味着当基础 List 更改时,ComboBox 将“看到”更改并自动更新其显示的项目。

使用 setItems() 方法初始化 ComboBox,并将 ObservableList 作为参数传递给它:

comboBox.setItems(programList);

从那里开始,每当 programList 更新(添加、删除项目等)时,ComboBox 都会反射(reflect)更改,而无需您进一步编写任何代码。

<小时/>

查看以下完整示例:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class ComboBoxListenerExample extends Application {

// This is our ObservableList that will hold our ComboBox items
private ObservableList<String> items = FXCollections.observableArrayList();

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {

// Simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);

// Simple ComboBox
ComboBox<String> comboBox = new ComboBox<>();

// Let's "permanently" set our ComboBox items to the "items" ObservableList. This causes the
// ComboBox to "observe" the list for changes
comboBox.setItems(items);

// Create a button that will reload data from our "database"
Button button = new Button("Refresh Data");

// The items.setAll()` method replaces all items in the list with our new list of items
button.setOnAction(event -> items.setAll(reloadDatabase()));

root.getChildren().addAll(comboBox, button);

// Show the Stage
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}

// Sample method to simulate reloading the database information
private List<String> reloadDatabase() {

List<String> items = new ArrayList<>();

for (int i = 0; i < 5; i++) {
items.add(getRandomWord());
}
return items;

}

// Just a helper method specific for this example; it simply returns a random word.
// This is used to simulate loading new data from the database
private String getRandomWord() {

List<String> words = Arrays.asList("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Red", "Blue", "Green", "Yellow", "Left", "Right", "Top", "Bottom");
Random random = new Random();
return words.get(random.nextInt(words.size()));
}
}
<小时/>

运行示例后,ComboBox 为空。每次单击“刷新数据”按钮时,基础 ObservableListitems 都会更新为新的随机项目列表; ComboBox 发生更改以立即反射(reflect)这些更改。

<小时/>

不在 JavaFX 应用程序线程上:

现在,那个错误消息怎么样? StackOverflow 上有很多问题和答案已经解决了这个问题,但这里有一个简短的解释:

我假设您正在后台线程上从数据库加载数据,这很好!但是,您无法从该线程对 UI 进行任何更改或更新。所有 UI 更新都需要在 JavaFX 应用程序线程上完成。

这很容易实现。当您在调用数据库方法时更新 List 时,请将该更新包装在 Platform.runLater() 调用中:

Platform.runLater(() -> programList.setAll(yourNewList));

这会安排在 JFX 应用程序线程上发生列表更新。问题解决了!

关于Javafx:如何将 ComboBox 项目动态绑定(bind)到从数据库更新自身的可观察列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55377889/

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