JavaFX 8,ListView with Checkboxes scrollpane问题 [英] JavaFX 8, ListView with Checkboxes scrollpane issue

查看:141
本文介绍了JavaFX 8,ListView with Checkboxes scrollpane问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用单元格工厂列表视图,其中包含以下复选框:

I am using cell factory for listview with checkboxes like:

  listView.setCellFactory(CheckBoxListCell.forListView(new Callback < Bean, ObservableValue < Boolean >> () {
  @Override
  public ObservableValue < Boolean > call(Bean item) {
    BooleanProperty observable = new SimpleBooleanProperty();
    observable.addListener((obs, wasSelected, isNowSelected) -> {
      if (isNowSelected) {
        if (!beanChoices.contains(item.toString())) {
          beanChoices.add(item.toString());
          observable.setValue(true);
          //listView.scrollTo(listView.getItems().size() - 1);
        }
      } else if (wasSelected) {
        if (beanChoices.contains(item.toString())) {
          beanChoices.remove(item.toString());
          observable.setValue(false);
        }
      }
    });
  /* [Code] which compares values with bean item string value and select observable to true for that for edit mode
    but here the observer not called for beanItem that are under scrollpane of listview. But on scroll it gets called. */
    return observable;
  }
}));

它工作正常,但并非适用于所有情况。
案例:当我说超过10个条目时,滚动窗格会出现。假设我有要检查的beanChoices是8或9索引(你必须滚动才能查看它们。)
不会为不可见的项目(在滚动窗格下)调用监听器。
在Debug上,我发现当我向下滚动时会调用监听器。

It works fine but not for all cases. Case: When I have say more than 10 entries, the scrollpane comes. Say I have beanChoices to be checked that are at 8 or 9 index(you have to scroll to view them).
The listener is not called for the items not visible(that are under scrollpane).
On Debug, I found that listener is called when I scroll down.


问题:当我从beanChoices检查上面的值时case,它返回空。

细节:我有beanChoices,我需要检查listview项目(编辑模式)。当我更新而不改变任何东西。 (假设将选择列表视图滚动窗口下的值并添加到beanChoices)

Problem: when I get checked values from beanChoices for above case, it return empty.
Detail: I have beanChoices which I need to make checked for listview items (edit mode). When I update without changing anything. (Assume that the value which is under the scrollpane of listview will be selected and added to beanChoices)


推荐答案

Callback 用于在项目与单元格关联时检索已检查状态的属性。可以从单元格中删除该项目,并随时将其放入新项目中。这就是 ListView (以及类似的控件,如 TableView )的工作原理。 CheckBoxListCell 每次新项目与单元格关联时,只需获取已检查的州属性。

The Callback is used to retrieve the property for the checked state when the item is associated with a cell. The item may be removed from a cell and put in a new one at any time. This is how ListView (and similar controls like TableView) works. CheckBoxListCell simply gets the checked state property every time a new item is associated with the cell.

返回值为也用于设置 CheckBox 的初始状态。由于没有使用正确的值正确初始化属性,因此不会保留初始状态。

The return value is also used to set the initial state of the CheckBox. Since you do not properly initialize the property with the correct value the initial state is not preserved.

另请注意,将属性的值更新为更改侦听器中的新值。无论如何它都会发生。

Also note that it makes little sense to update the value of the property to the new value in the change listener. It happens anyway.

因为 BooleanProperty 是原始 boolean 可能的值是 true false ;只有!Objects.equals(oldValue,newValue)才能调用 ChangeListener ,你可以确定 isNowSelected =!wasSelected

Since BooleanProperty is a wrapper for primitive boolean the possible values are true and false; the ChangeListener only gets called when !Objects.equals(oldValue, newValue) you can be sure that isNowSelected = !wasSelected.

当然你还需要返回值:

@Override
public ObservableValue < Boolean > call(Bean item) {
    final String value = item.toString();
    BooleanProperty observable = new SimpleBooleanProperty(beanChoices.contains(value));
    observable.addListener((obs, wasSelected, isNowSelected) -> {
        if (isNowSelected) {
            beanChoices.add(value);
        } else {
            beanChoices.remove(value);
        }
    });
    return observable;
}

我还建议使用 Collection of Bean 而不是依赖于对象的字符串表示。 toString 许多不会产生唯一结果,而 Beans.equals 将是比较对象的更好选择。

I also recommend using a Collection of Beans instead of relying on the string representation of the objects. toString many not produce unique results and Beans.equals would be the better choice to compare the objects.

这篇关于JavaFX 8,ListView with Checkboxes scrollpane问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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