如何删除 ListView 中的复选框?
How to delete a CheckBox inside a ListView?
我正在尝试刷新 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?
编辑:
不太确定你在这里想说什么:
a JFXCheckBoxe of the same name can't be added again. Why is this happening and how can i add a new JFXCheckBoxe of the same name using the code provided?
如果在调用 delete 方法时再次删除,请确保包含要删除的项目的列表已更新,以便在下一次调用时不再删除它们。
旧post:
我真的不能说出你的意思或这是什么上下文,但据我猜测,你可以执行以下操作:为复选框分配一个 ID:
yourCheckBox.setID("yourIdHere");
然后您可以通过搜索该 ID 将其从列表中删除:
listView.getItems().removeIf(checkBox -> "yourIdHere".equals(checkBox.getId()));
或者,您可以使用相同的方法检查 CheckBox
es 的其他属性,但请记住,如果多个项目匹配,它们将全部被删除。
如果您只有一个(或几个)CheckBox
(es) 需要删除,您可以将引用存储在某处的字段中,然后以这种方式删除它:
CheckBox yourCheckBox = new CheckBox();
// ...
listView.getItems().remove(yourCheckBox);
此外,indexOf
必须与列表中的对象同时使用。如果您声明您的列表使用复选框:
ListView<CheckBox> listView = new ListView<>();
然后 indexOf
、remove
和其他采用 Object
的方法必须传递 CheckBox
.
使用 here 中的代码并假设 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");
});
Complete Code:
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");
});
}
}
This is an example using your incorrect approach.
Controller:
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>
我正在尝试刷新 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?
编辑:
不太确定你在这里想说什么:
a JFXCheckBoxe of the same name can't be added again. Why is this happening and how can i add a new JFXCheckBoxe of the same name using the code provided?
如果在调用 delete 方法时再次删除,请确保包含要删除的项目的列表已更新,以便在下一次调用时不再删除它们。
旧post:
我真的不能说出你的意思或这是什么上下文,但据我猜测,你可以执行以下操作:为复选框分配一个 ID:
yourCheckBox.setID("yourIdHere");
然后您可以通过搜索该 ID 将其从列表中删除:
listView.getItems().removeIf(checkBox -> "yourIdHere".equals(checkBox.getId()));
或者,您可以使用相同的方法检查 CheckBox
es 的其他属性,但请记住,如果多个项目匹配,它们将全部被删除。
如果您只有一个(或几个)CheckBox
(es) 需要删除,您可以将引用存储在某处的字段中,然后以这种方式删除它:
CheckBox yourCheckBox = new CheckBox();
// ...
listView.getItems().remove(yourCheckBox);
此外,indexOf
必须与列表中的对象同时使用。如果您声明您的列表使用复选框:
ListView<CheckBox> listView = new ListView<>();
然后 indexOf
、remove
和其他采用 Object
的方法必须传递 CheckBox
.
使用 here 中的代码并假设 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");
});
Complete Code:
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");
});
}
}
This is an example using your incorrect approach.
Controller:
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>