为了账号安全,请及时绑定邮箱和手机立即绑定

ListChangeListener、AddListener 到 ListView 或

ListChangeListener、AddListener 到 ListView 或

慕桂英3389331 2022-07-14 10:42:12
我有一个 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相当陌生,但不是编码。非常感谢提前!
查看完整描述

2 回答

?
蓝山帝景

TA贡献1843条经验 获得超7个赞

经过大量调查和反复试验,下面的代码现在可以正常运行,同时提供正确的视觉反馈,使用 checkbox.setIndeterminate() 并实现与 'item.isSelected().getValue()' 配对的“当前阈值”、“最大阈值”整数变量触发复选框状态的条件。当然有更清洁的方法来实现,但这对我有用。固定为:


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();

 }

}



查看完整回答
反对 回复 2022-07-14
?
慕妹3146593

TA贡献1820条经验 获得超9个赞

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

 public void onChanged(ObservableValue<? extends GroupEntry> ov

EDIT ListChangeListener是一个界面。您需要正确覆盖该onChanged方法。该方法需要与定义中相同的参数:

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


查看完整回答
反对 回复 2022-07-14
  • 2 回答
  • 0 关注
  • 105 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信