ListChangeListener、AddListener 到 ListView 或 ListView 中的布尔复选框

ListChangeListener, AddListener to ListView or Boolean Checkboxes in ListView

我有一个 java 程序,我没有编码我是 reversing/updating 以添加一些急需的功能。

此程序根据在 /input 中找到的一些 csv 文件生成两个 ListView,list1:NameEntry 和 list2:GroupEntry。

NameEntry 包含带有相应复选框的用户名列表。 GroupEntry 包含一长串带有复选框的用户角色。

我当前的任务是向 GUI 添加一个 'Select All Roles' 复选框,将所有 GroupEntry 角色项设置为 'true'。这是成功完成的。

我的问题是向 ListView 或单个复选框项添加 onChanged() 侦听器,以便在手动取消选中一个或多个角色的情况下禁用 'Set All Roles' 切换。

@FXML
  public void setAllRoles()
  {
    ObservableList<GroupEntry> groups = this.list2.getItems();
    if (this.allRoles) {
      this.allRoles = false;
      for (GroupEntry item : groups) {
      item.setSelected(new SimpleBooleanProperty(false));
      this.list2.refresh();
      }
    } else {
      this.allRoles = true;
      for (GroupEntry item : groups) {
      item.setSelected(new SimpleBooleanProperty(true));
      item.addListener(new ListChangeListener<GroupEntry>() {
           public abstract onChanged(ObservableValue<? extends GroupEntry> ov,
             Boolean old_val, Boolean new_val) {
                 //System.out.println(item.isSelected());
                 allRoles = false;
             }
      });
      }
      this.list2.refresh();
    }
  }

当尝试编译控制器 .class 文件时,我收到以下错误:

invalid method declaration; return type required
           public abstract onChanged(ObservableValue<? extends GroupEntry> ov,
                           ^

我也试过没有摘要但是编译器 returns 同样的错误。

这就是全部 javafx。有没有人知道问题可能是什么或有任何明确的 examples/guidelines?我阅读了无数文档页面,但似乎无法理解这个简单的错误。我是 Java 的新手,但不会编码。

非常感谢!

这是一个简单的解决方案。您在方法声明中缺少类型。例如 voidonChanged 应该是方法,而不是构造函数。同时删除 abstract 限定符。

 public void onChanged(ObservableValue<? extends GroupEntry> ov

编辑 ListChangeListener 是一个接口。您需要正确覆盖 onChanged 方法。该方法需要与定义中相同的参数:

void onChanged(ListChangeListener.Change<? extends E> c)

经过大量调查和反复试验,下面的代码现在可以正常运行,同时提供正确的视觉反馈,使用 checkbox.setIndeterminate() 并实现 'current threshhold'、'max threshhold' 与 [= 配对的整数变量17=] 触发复选框状态的条件。当然有更清洁的实施方式,但这对我有用。固定为:

public void setAllRoles()
{
thrshld = 0;
ObservableList<GroupEntry> groups = this.list2.getItems();
for (GroupEntry item : groups) {
  thrshld--;
}
thrshldMax = thrshld;

if (this.allRoles) {
  this.allRoles = false;
  for (GroupEntry item : groups) {
    item.setSelected(new SimpleBooleanProperty(false));
    item.isSelected().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> ov,
          Boolean old_val, Boolean new_val) {
            BooleanProperty thatCB = item.isSelected();
            if (thatCB.getValue() == true) {
              checkbox2.setIndeterminate(true);
              thrshld++; // = thrshld + 1;
            } else {
              thrshld--; // = thrshld - 1;
            } 
            if (thrshld == thrshldMax) {
              checkbox2.setIndeterminate(false);
              checkbox2.setSelected(false);
            }
            if (thrshld == 0) {
              checkbox2.setIndeterminate(false);
              checkbox2.setSelected(true);
            }
            //status.setText("state: " +thatCB.getValue()+ "\r\nthrshld: " +thrshld+ "Max: " +thrshldMax);
          }
    });
  }
  this.list2.refresh();
} else {
  this.allRoles = true;
  thrshld = 0;
  for (GroupEntry item : groups) {
    item.setSelected(new SimpleBooleanProperty(true));
    item.isSelected().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> ov,
          Boolean old_val, Boolean new_val) {
            BooleanProperty thisCB = item.isSelected();
            if (thisCB.getValue() == true) {
              thrshld++; // = thrshld + 1;
              if (thrshld == 0) {
                checkbox2.setIndeterminate(false);
                checkbox2.setSelected(true);
              }
            } else {
              checkbox2.setIndeterminate(true);
              thrshld--; // = thrshld - 1;
              if (thrshld == thrshldMax) {
                checkbox2.setIndeterminate(false);
                checkbox2.setSelected(false);
              }
            }
            //status.setText("state: " +thisCB.getValue()+ "\r\nthrshld: " +thrshld+ "Max: " +thrshldMax);
          }
    });
  }
  this.list2.refresh();
 }
}

感谢那些捐出 2 美分的人! 小心 Whosebug!