Java 如何删除ListView中的复选框?

Java 如何删除ListView中的复选框?,java,listview,javafx,checkbox,jfoenix,Java,Listview,Javafx,Checkbox,Jfoenix,我正在尝试刷新JFX复选框的JFXListView。 我使用的代码适用于每种更新情况,仅适用于更新需要从JFXListView中删除JFXCheckBox的情况。 以下是我对删除部分所做的操作: ArrayList<String> ServiceToDelete = R.OffrirService(); List<String> seup = se.stream().filter(elem -> !ServiceToDelete.contains(elem)).co

我正在尝试刷新JFX复选框的JFXListView。 我使用的代码适用于每种更新情况,仅适用于更新需要从JFXListView中删除JFXCheckBox的情况。 以下是我对删除部分所做的操作:

ArrayList<String> ServiceToDelete = R.OffrirService();
List<String> seup = se.stream().filter(elem -> !ServiceToDelete.contains(elem)).collect(Collectors.toList());
                for (int x = 0; x < seup.size(); x++) {
                    String g = seup.get(x);
                    li_se.getItems().removeIf((t) -> {
                        return ((JFXCheckBox) t).getText().equals(g);
                    });
                    //System.out.println(li_se.getItems().indexOf(ServiceToDelete.get(x)));
                }
arraylistservicetodelet=R.OffrirService();
List seup=se.stream().filter(elem->!servicetodelet.contains(elem)).collect(Collectors.toList());
对于(int x=0;x{
return((JFXCheckBox)t).getText()等于(g);
});
//System.out.println(li_-se.getItems().indexOf(serviceTodelet.get(x));
}
seup cno包含应该删除的jfx复选框的名称,问题是
li_se.getItems().removeIf((t)->{return((JFXCheckBox)t.getText().equals(g);})删除JFXCheckBox时仅工作一次无法再次添加同名JFXCheckBox。为什么会发生这种情况?如何使用提供的代码添加同名的新JFXCheckBox

编辑:

不太清楚你在这里想说什么:

无法再次添加同名的JFXCheckBox。为什么会发生这种情况?如何使用提供的代码添加同名的新JFXCheckBox

如果在调用delete方法时再次删除,请确保更新包含要删除的项目的列表,以便下次调用时不再删除这些项目

旧帖子:

我真的不知道你是什么意思,也不知道这是什么上下文,但根据我的猜测,你可以做以下操作:为复选框指定一个ID:

yourCheckBox.setID("yourIdHere");
然后,您可以通过搜索该ID将其从列表中删除:

listView.getItems().removeIf(checkBox -> "yourIdHere".equals(checkBox.getId()));
或者,您可以使用相同的方法检查
复选框
es的其他属性,但请记住,如果多个项目匹配,它们都将被删除

如果只有一个(或几个)需要删除的
复选框
,可以将引用存储在某个字段中,然后通过以下方式删除:

CheckBox yourCheckBox = new CheckBox();

// ...

listView.getItems().remove(yourCheckBox);
另外,
indexOf
必须与列表中的对象同时使用。如果您声明要使用列表复选框:

ListView<CheckBox> listView = new ListView<>();
ListView ListView=新建ListView();
然后,必须使用JFoniex中的代码,并假设JFoniex的工作方式与常规的
列表视图
类似,则必须将
复选框
传递给
indexOf
移除
和其他获取
对象的方法,此示例显示了移除
列表视图
单元格的两种方法

一种方法是观察
项的
“on”属性

// observe item's on property and display message if it changes:
item.onProperty().addListener((obs, wasOn, isNowOn) -> {
    System.out.println(item.getName() + " changed on state from " + wasOn + " to " + isNowOn);
    if (isNowOn) {
        listView.getItems().remove(item);
    }
});
另一种方法是迭代项目并删除满足特定条件的项目。在本例中,我使用的不是迭代而是
removeIf()

完整代码:

导入javafx.application.application;
导入javafx.beans.property.BooleanProperty;
导入javafx.beans.property.SimpleBoleAnProperty;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.property.StringProperty;
导入javafx.scene.scene;
导入javafx.scene.control.ListView;
导入javafx.scene.control.cell.CheckBoxListCell;
导入javafx.scene.layout.BorderPane;
导入javafx.stage.stage;
公共类ListViewWithCheckBox扩展应用程序
{
@凌驾
公共无效开始(阶段primaryStage)
{
ListView ListView=新建ListView();

对于(int i=1;i
String
不可分配给
JFXCheckBox
。为什么希望在
列表中找到
String
对象?此外,一般情况下,您不使用
节点
作为项类型,因为这几乎完全取消了使用虚拟化控件的好处。如果您描述这一点,我们可以建议一个合适的替代方案。请提供更多的代码和上下文。有趣的是,它们似乎直接通过
节点
。示例代码来自
JFoenix
网站。@Sami“不能再添加”到底是什么意思?另外,检查我添加到答案帮助中的内容是否未使用setCellFactory()方法,我只添加JFXCheckBoxex而不是项目。在这种情况下,我如何继续?正如@fabian在他的评论中指出的,你不应该这样做。但是,如果你选择继续你的方式,你的解决方案应该与第二种方法非常相似。第二种方法将
Item
替换为
JFXListView
。对不起,
Item
替换为
JFXCheckBox
.return((JFXCheckBox)t)t)t)t告诉你li_se.getItems().removeIf((t)->{return((JFXCheckBox)t.getText().equals(g);});完成了这个技巧。listView.getItems().removeIf(checkBox->“yourIdHere.equals(checkBox.getId());正在工作,但正如我所说的。如何解决这个问题?
listView.getItems().removeIf((t) -> {
    return ((Item) t).getName().equals("Item 9");
});
import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.CheckBoxListCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class ListViewWithCheckBox extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        ListView<Item> listView = new ListView<>();
        for (int i = 1; i <= 20; i++) {
            Item item = new Item("Item " + i, false);

            // observe item's on property and display message if it changes:
            item.onProperty().addListener((obs, wasOn, isNowOn) -> {
                System.out.println(item.getName() + " changed on state from " + wasOn + " to " + isNowOn);
                if (isNowOn) {
                    listView.getItems().remove(item);
                }
            });

            listView.getItems().add(item);
        }

        listView.setCellFactory(CheckBoxListCell.forListView((Item item) -> item.onProperty()));

        removeItem(listView);

        BorderPane root = new BorderPane(listView);
        Scene scene = new Scene(root, 250, 400);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static class Item
    {

        private final StringProperty name = new SimpleStringProperty();
        private final BooleanProperty on = new SimpleBooleanProperty();

        public Item(String name, boolean on)
        {
            setName(name);
            setOn(on);
        }

        public final StringProperty nameProperty()
        {
            return this.name;
        }

        public final String getName()
        {
            return this.nameProperty().get();
        }

        public final void setName(final String name)
        {
            this.nameProperty().set(name);
        }

        public final BooleanProperty onProperty()
        {
            return this.on;
        }

        public final boolean isOn()
        {
            return this.onProperty().get();
        }

        public final void setOn(final boolean on)
        {
            this.onProperty().set(on);
        }

        @Override
        public String toString()
        {
            return getName();
        }

    }

    public static void main(String[] args)
    {
        launch(args);
    }

    public void removeItem(ListView listView)
    {
        listView.getItems().removeIf((t) -> {
            return ((Item) t).getName().equals("Item 9");
        });
    }
}
import com.jfoenix.controls.JFXCheckBox;
import com.jfoenix.controls.JFXListView;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;

/**
 *
 * @author sedri
 */
public class FXMLDocumentController implements Initializable {

    @FXML
    JFXListView listview;
    @FXML ListView<String> lvAddNew;
    @FXML Button btnAddNew;    


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        for(int i = 1; i < 100; i++)
        {
           JFXCheckBox tempJFXCheckBox = new JFXCheckBox("item " + i);
           tempJFXCheckBox.selectedProperty().addListener((obs, oldValue, newValue)->{
               System.out.println(newValue);
               if(newValue)
               {
                   listview.getItems().remove(tempJFXCheckBox);
                   lvAddNew.getItems().add(tempJFXCheckBox.getText());
               }
           });

           listview.getItems().add(tempJFXCheckBox);           
        }     
    }   

    @FXML private void handleBtnAddNew(ActionEvent event)
    {
        //Remove selected item
        String tempList = lvAddNew.getSelectionModel().getSelectedItem();
        lvAddNew.getItems().remove(tempList);
        listview.getItems().add(new JFXCheckBox(tempList));        
    }
}
<?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXListView?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane id="AnchorPane" prefHeight="700.0" prefWidth="320" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication2.FXMLDocumentController">
   <children>
      <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <JFXListView fx:id="listview" />
            <ListView fx:id="lvAddNew" prefHeight="200.0" prefWidth="200.0" />
            <Button fx:id="btnAddNew" mnemonicParsing="false" onAction="#handleBtnAddNew" text="Add New" />
         </children>
      </VBox>
   </children>
</AnchorPane>