javafx组合框列表问题 [英] javafx combobox items list issue

查看:140
本文介绍了javafx组合框列表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JavaFX ComboBox 用作具有历史记录的搜索字段。
这是我的代码示例。

I am trying to use a JavaFX ComboBox as a search field with history. This is a sample of my code.

ObservableList<String> patternHistory = FXCollections.observableArrayList();
searchComboBox.setItems(patternHistory);

searchComboBox.valueProperty().addListener((observable, oldValue, newValue) -> {

    ObservableList<String> history = searchComboBox.getItems();

    // This works fine
    // history.add(newValue);

    // This does not work
    history.add(0, newValue);
});

如果我执行 history.add(newValue) ComboBox 的行为符合我的预期。
历史记录中的最后一个条目将添加到列表的末尾。
但是,我希望首先显示最后一个条目(我还想删除重复项并限制历史记录大小,但在本例中我保持简单)。

If I do the history.add(newValue) the ComboBox behaves as I expect. The last entry in the history is added to the end of the list. However, I would like the last entry to be shown first (I also want to remove duplicates and limit the history size but I keep it simple in this example).

所以我决定只用 history.add(0,newValue)在列表前添加新值。
然而,当我这样做时,组合框开始以奇怪的方式运行,代码不再起作用。似乎只要我在列表的末尾添加/删除项目就可以正常工作,但如果我在开头或中间做同样的事情就不再有效了。

So I decided to simply add the new value in front of the list with history.add(0, newValue). However, when I do that the combobox start to behaves in strange ways and the code does not work anymore. It seems that as long I add/delete items at the end of the list it works fine but if I do the same at the beginning or in the middle it does not work anymore.

这里我使用lambda表达式语法,但我尝试使用匿名类表示法,它是相同的。我也试图实现我自己的可观察列表,也有相同的结果。

Here I'm using the lambda expression syntax but I have tried with the anonymous class notation and it's the same. I also tried to implement my own observable list and there also it's the same result.

我目前正在使用JDK 1.8.0_60,但我遇到了早期版本的问题。
有人可以告诉我是否有办法解决这个问题或者我做错了什么?

I'm currently using JDK 1.8.0_60 but I had the problem with earlier version. Can somebody tell me if there is a way to fix this issue or if I'm doing something wrong?

推荐答案

解决方法似乎是替换列表中的所有项目。以下似乎工作正常:

A workaround seems to be to replace all the items in the list. The following seems to work OK:

import java.util.ArrayList;
import java.util.List;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class HistoryListComboBox extends Application {

    @Override
    public void start(Stage primaryStage) {
        ComboBox<String> combo = new ComboBox<>();
        combo.getItems().addAll("One", "Two", "Three");
        combo.setEditable(true);
        combo.valueProperty().addListener((obs, oldValue, newValue) -> {
            if (! combo.getItems().contains(newValue)) {
                List<String> newItems = new ArrayList<>();
                newItems.add(newValue);
                newItems.addAll(combo.getItems());
                combo.getItems().setAll(newItems);
            }
        });
        StackPane root = new StackPane(combo);
        primaryStage.setScene(new Scene(root, 350, 120));
        primaryStage.show();
    }

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

这篇关于javafx组合框列表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆