-1

我正在尝试刷新 JFXCheckBoxes 的 JFXListView。我使用的代码适用于每个更新案例,即更新需要从 JFXListView 中删除 JFXCheckBoxe 的情况。这就是我为删除部分所做的:

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

seup 包含应该删除的 JFXCheckBoxes 的名称,问题是li_se.getItems().removeIf((t) -> {return ((JFXCheckBox) t).getText().equals(g);});当 JFXCheckBoxe 被删除时只能工作一次,不能再次添加同名的 JFXCheckBoxe。为什么会发生这种情况,如何使用提供的代码添加新的同名 JFXCheckBoxe?

4

2 回答 2

0

编辑:

不完全确定你的意思是在这里说:

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

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

旧帖:

我无法真正说出您的意思或这是什么上下文,但据我猜测,您可以执行以下操作:为复选框分配一个 ID:

yourCheckBox.setID("yourIdHere");

然后您可以通过搜索该 ID 将其从列表中删除:

listView.getItems().removeIf(checkBox -> "yourIdHere".equals(checkBox.getId()));

或者,您可以使用相同的方法检查CheckBoxes 的其他属性,但请记住,如果多个项目匹配,它们将全部被删除。

如果您只有一个(或几个)CheckBox需要删除,您可以将引用存储在某个字段中,然后以这种方式删除它:

CheckBox yourCheckBox = new CheckBox();

// ...

listView.getItems().remove(yourCheckBox);

此外,indexOf必须与列表中的对象同时使用。如果您声明您的列表使用复选框:

ListView<CheckBox> listView = new ListView<>();

然后indexOfremove和其他采用 an 的方法Object必须通过 a CheckBox

于 2018-05-22T12:50:21.593 回答
0

使用此处的代码并假设 JFoniex 像常规一样工作ListView,此示例显示了删除单元格的两种方法ListView

一种方法是观察Item's“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);
    }
});

另一种是 buy 遍历物品并删除符合某些条件的物品。在这种情况下,我没有迭代我正在使用removeIf()的 .

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

FXML:

<?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>
于 2018-05-22T15:38:24.767 回答