简体   繁体   English

如何删除事件处理程序上的所有标签,请单击

[英]How to remove all labels on event handler click

I have an event handler which I would like to clear all labels of result before printing the new labels. 我有一个事件处理程序,在打印新标签之前,我想清除所有结果标签。 I would like to keep the other labels that are not of type result. 我想保留其他不是类型结果的标签。 The code is currently only clearing the last instance of result. 该代码当前仅清除结果的最后一个实例。 What loop do I need to add? 我需要添加什么循环?

@Override
public void handle(ActionEvent event) { 
    listLayout.getChildren().removeAll(result);
    Collections.sort(listOfCars, ListYears.yearCom);

    for (int i = 0; i < listOfCars.size(); i++) {
        newCarsListings = listOfCars.get(i).toString();
        result = new Label(newCarsListings);
        result.setTranslateX(20);
        result.setTranslateY(-40);
        listLayout.getChildren().addAll(result);
    }
}

The problem is that you create the label "result" for every car you have in your listOfCars . 问题是您为listOfCars每辆汽车都创建了标签“结果”。 So your first line listLayout.getChildren().removeAll(result) will only remove the last created Label. 因此,第一行listLayout.getChildren().removeAll(result)将仅删除最后创建的Label。 If you have 10 cars in your list, you will create 10 Label objects, but only the last one can be referenced via result . 如果列表中有10辆汽车,则将创建10个Label对象,但只能通过result引用最后一个。

If you want to remove all your created Labels (and no other controls), I suggest you introduce a subclass of Label : 如果要删除所有已创建的Label(并且没有其他控件),建议您引入Label的子类:

class CarLabel extends Label {
    CarLabel(String str) {
        super(str);
    }
}

And then in your handle method: 然后在您的handle方法中:

public void handle(ActionEvent event) {
    listLayout.getChildren().removeIf(CarLabel.class::isInstance);
    Collections.sort(listOfCars, ListYears.yearCom);
    for(Object o : listOfCars) {
        result = new CarLabel(o.toString());
        result.setTranslateX(20);
        result.setTranslateY(-40);
        listLayout.getChildren().add(result);
    }
}

removeAll(result) will only remove the elements in listLayout.getChildren() that are matching an element from result . removeAll(result)将只删除listLayout.getChildren()中与result元素匹配的元素。

Have a look at the official documention for removeAll 看看removeAll的官方文档

If you want to remove every element the list is containing use the clear() method 如果要删除列表中包含的每个元素,请使用clear() 方法

            @Override
            public void handle(ActionEvent event) { 

                listLayout.getChildren().clear()
                // the list should be empty now
                Collections.sort(listOfCars, ListYears.yearCom);

                for (int i = 0; i < listOfCars.size(); i++) {

                newCarsListings = listOfCars.get(i).toString();

                result = new Label(newCarsListings);
                result.setTranslateX(20);
                result.setTranslateY(-40);

                listLayout.getChildren().addAll(result);

                }
            }
          });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM