使用此处的代码并假设 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>