猿问

ListChangeListener、AddListener 到 ListView 或

我有一个 java 程序,我没有编写我正在反转/更新的代码,以便添加一些急需的功能。


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


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


我当前的任务是在 GUI 中添加一个“选择所有角色”复选框,将所有 GroupEntry 角色项设置为“真”。这成功地完成了。


我的问题是将 onChanged() 侦听器添加到 ListView 或单个复选框项,以便在手动取消选中一个或多个角色的情况下禁用“设置所有角色”切换。


@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,

                           ^

我也尝试过不使用抽象,但编译器返回相同的错误。


这都是javafx。有谁知道问题可能是什么或有任何明确的示例/指南?我已经阅读了无数的文档页面,但似乎无法解决这个简单的错误。我对Java相当陌生,但不是编码。


非常感谢提前!


慕桂英3389331
浏览 138回答 2
2回答

蓝山帝景

经过大量调查和反复试验,下面的代码现在可以正常运行,同时提供正确的视觉反馈,使用 checkbox.setIndeterminate() 并实现与 'item.isSelected().getValue()' 配对的“当前阈值”、“最大阈值”整数变量触发复选框状态的条件。当然有更清洁的方法来实现,但这对我有用。固定为:public void setAllRoles(){thrshld = 0;ObservableList<GroupEntry> groups = this.list2.getItems();for (GroupEntry item : groups) {&nbsp; thrshld--;}thrshldMax = thrshld;if (this.allRoles) {&nbsp; this.allRoles = false;&nbsp; for (GroupEntry item : groups) {&nbsp; &nbsp; item.setSelected(new SimpleBooleanProperty(false));&nbsp; &nbsp; item.isSelected().addListener(new ChangeListener<Boolean>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void changed(ObservableValue<? extends Boolean> ov,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Boolean old_val, Boolean new_val) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BooleanProperty thatCB = item.isSelected();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (thatCB.getValue() == true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkbox2.setIndeterminate(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; thrshld++; // = thrshld + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; thrshld--; // = thrshld - 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (thrshld == thrshldMax) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkbox2.setIndeterminate(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkbox2.setSelected(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (thrshld == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkbox2.setIndeterminate(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkbox2.setSelected(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //status.setText("state: " +thatCB.getValue()+ "\r\nthrshld: " +thrshld+ "Max: " +thrshldMax);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; }&nbsp; this.list2.refresh();} else {&nbsp; this.allRoles = true;&nbsp; thrshld = 0;&nbsp; for (GroupEntry item : groups) {&nbsp; &nbsp; item.setSelected(new SimpleBooleanProperty(true));&nbsp; &nbsp; item.isSelected().addListener(new ChangeListener<Boolean>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void changed(ObservableValue<? extends Boolean> ov,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Boolean old_val, Boolean new_val) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BooleanProperty thisCB = item.isSelected();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (thisCB.getValue() == true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; thrshld++; // = thrshld + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (thrshld == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkbox2.setIndeterminate(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkbox2.setSelected(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkbox2.setIndeterminate(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; thrshld--; // = thrshld - 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (thrshld == thrshldMax) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkbox2.setIndeterminate(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkbox2.setSelected(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //status.setText("state: " +thisCB.getValue()+ "\r\nthrshld: " +thrshld+ "Max: " +thrshldMax);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; }&nbsp; this.list2.refresh();&nbsp;}}

慕妹3146593

这是一个简单的解决方案。您在方法声明中缺少类型。例如void。onChanged应该是方法,而不是构造函数。还要删除&nbsp;abstract限定符。&nbsp;public&nbsp;void&nbsp;onChanged(ObservableValue<?&nbsp;extends&nbsp;GroupEntry>&nbsp;ovEDIT&nbsp;ListChangeListener是一个界面。您需要正确覆盖该onChanged方法。该方法需要与定义中相同的参数:void&nbsp;onChanged(ListChangeListener.Change<?&nbsp;extends&nbsp;E>&nbsp;c)
随时随地看视频慕课网APP

相关分类

Java
我要回答