【问题标题】:ListChangeListener, AddListener to ListView or Boolean Checkboxes in ListViewListChangeListener, AddListener 到 ListView 或 ListView 中的布尔复选框
【发布时间】:2019-07-23 11:33:43
【问题描述】:

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

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

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

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

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

@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相当陌生,但不是编码。

提前非常感谢!

【问题讨论】:

  • 不相关:永远不要使用 listView.refresh(或任何其他虚拟化控件的刷新) - 如果似乎需要它,那么您的设置很可能有问题!
  • 当然 ;) 重复:如果你需要它,设置中有问题(不管是谁写的;)

标签: java listview javafx checkbox changelistener


【解决方案1】:

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

 public void onChanged(ObservableValue<? extends GroupEntry> ov

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

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

【讨论】:

  • “代码目前看起来像这样”请编辑问题并更新。
  • 重复。您的错误是因为您没有正确覆盖 onChanged。你的方法签名是错误的
【解决方案2】:

经过大量调查和反复试验,下面的代码现在可以正常运行,同时提供正确的视觉反馈,使用 checkbox.setIndeterminate() 并实现 'current threshhold'、'max threshhold' 整数变量与 '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();
 }
}

感谢那些付出 2 美分的人! 小心 stackoverflow!

【讨论】:

  • 永远不要使用 list.refresh - 如果它似乎是必要的,那么你的设置有问题
  • 是的,当然——你似乎没有读过它,因为你也重复了使用刷新的错误;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2011-02-10
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
  • 1970-01-01
相关资源
最近更新 更多