-1

I am trying to refresh a JFXListView of JFXCheckBoxes. The code i am using works for every update case just the case when the update needs to remove a JFXCheckBoxe from the JFXListView. This is what i do for the delete part :

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

The seup cnotains the names of the JFXCheckBoxes that should be removed, the problem is li_se.getItems().removeIf((t) -> {return ((JFXCheckBox) t).getText().equals(g);}); work only once when the JFXCheckBoxe is deleted 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 ?

Sami
  • 165
  • 4
  • 19
  • 3
    `String` is not assignable to `JFXCheckBox`. Why do you expect a `String` object to be found in a `List`? Furthermore in general you don't use `Node`s as item types, since this undoes the benefits of using a virtualizing control almost completely. If you describe a use case for this, we may be able to suggest a suitable alternative. – fabian May 22 '18 at 12:28
  • Please provide a little more code and context. – Chaoz May 22 '18 at 12:32
  • What's interesting is that It appears that they pass `Nodes` directly. [Here](http://www.jfoenix.com/documentation.html#ListView) is sample code from `JFoenix` website. – SedJ601 May 22 '18 at 14:55
  • @Sami what exactly do you mean by "can't be added again"? Also, check if what I added to my answer helps – Chaoz May 23 '18 at 07:16

2 Answers2

0

Edit:

Not exactly sure what you meant to say here:

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?

If the case that it's removed again upon calling the delete method, make sure that the lists that contain items to be deleted are updated, so that on the next call they would no longer be deleted.

Old post:

I can't really tell what you mean or what context this is in for, but from what I can guess, you can do the following: assign the checkbox an ID:

yourCheckBox.setID("yourIdHere");

And then you can remove it from the list by searching for that ID:

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

Alternatively you can use the same method to check for other properties of the CheckBoxes but keep in mind that if multiple items match they will all be removed.

If you only have one (or few) CheckBox(es) that you need to remove, you can store the reference in a field somewhere and then remove it this way:

CheckBox yourCheckBox = new CheckBox();

// ...

listView.getItems().remove(yourCheckBox);

Also, indexOf must be used with objects of the same time as the ones in the list. If you declared your list to use checkboxes:

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

Then indexOf, remove and other methods that take an Object must be passed a CheckBox.

Chaoz
  • 2,119
  • 1
  • 20
  • 39
  • listView.getItems().removeIf(checkBox -> "yourIdHere".equals(checkBox.getId())); is working but as i said. How to solve this ? – Sami May 22 '18 at 22:16
0

Using the code from here and assuming JFoniex works like the regular ListView, this example shows two ways to remove a ListView cell.

One way is by observing the Item's "on" property.

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

The other is buy Iterating through the items and removing the item that meets certain criteria. In this case, I am not iterating I am using 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>
SedJ601
  • 12,173
  • 3
  • 41
  • 59
  • I am not using the method setCellFactory(), i only add JFXCheckBoxex not items. How can i proceed in this case ? – Sami May 22 '18 at 16:17
  • 1
    As @fabian pointed out in his comment, you should not do that. – SedJ601 May 22 '18 at 16:18
  • But, if you chose to continue your way, your solution should be very similar to the second approach. Which `Item` replaced by `JFXListView `. – SedJ601 May 22 '18 at 16:20
  • Sorry, `Item` replaced by `JFXCheckBox`. return ((JFXCheckBox) t) – SedJ601 May 22 '18 at 16:29
  • @Sami you should read the [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) again. That was a terrible attempt. I updated this answer. – SedJ601 May 23 '18 at 00:49
  • 1
    Thnak you the li_se.getItems().removeIf((t) -> { return ((JFXCheckBox) t).getText().equals(g); }); did the trick. – Sami May 23 '18 at 22:47