Dynamically change the background of a cell in a ListView

Thomas Dejagere

I have a problem with my listview. I have some code that changes the background of some cells in a ListView. But when i scroll in that listview the background changes to the wrong cells.

Here you see some code: Change the background of the cell in a listview:

@Override
        public ListCell<String> call(ListView<String> param) {
            ListCell<String> cell = new ListCell<String>() {
                @Override
                protected void updateItem(String t, boolean bln) {
                    super.updateItem(t, bln);
                    if (t != null) {
                        setText(t);

                        if (!controller.checkGerecht(t)) {

                           if (!getStyleClass().contains("mystyleclass")) {
                                getStyleClass().add("mystyleclass");
                                foutieveInput.add(t);
                            } else {      
                               getStyleClass().remove("mystyleclass");
                            }
                        } else {
                            setText(t);

                        }
                    }
                }

The css file:

.mystyleclass{
    -fx-background-color: #ff0000;
}
James_D

You have the logic implemented incorrectly, assuming you want the red background only on cells for which controller.checkGerecht(t) is false. You're trying to remove the style class if it's not present: you want to remove the style class if your condition doesn't hold. (I.e. you have remove in the wrong else clause.)

Additionally, you need to handle the case where the cell is updated to hold a null value (e.g. if it is empty):

public ListCell<String> call(ListView<String> param) {
    ListCell<String> cell = new ListCell<String>() {
        @Override
        protected void updateItem(String t, boolean bln) {
            super.updateItem(t, bln);
            if (t == null) {
                setText(null);
                getStyleClass().remove("mystyleclass");
            } else {
                setText(t);

                if (!controller.checkGerecht(t)) {

                    if (!getStyleClass().contains("mystyleclass")) {
                        getStyleClass().add("mystyleclass");
                        foutieveInput.add(t);
                    } 
                } else {
                    getStyleClass().remove("mystyleclass");
                }
            }

        }
    };
    return cell ;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SSRS Dynamically change the cell background and font color

From Dev

SSRS Dynamically change the cell background and font color

From Dev

WPF: Change DataGrid cell/row background color dynamically at runtime

From Dev

How to change the background of a cell dynamically in a Kendo MVC UI Grid?

From Dev

How do I dynamically change the background of an item in a listview in JavaFX

From Dev

Change Listview cell opacity

From Dev

Change Listview cell opacity

From Dev

Change background colour dynamically

From Dev

Change background of disabled listView

From Dev

Dynamically changing the background of ListView child

From Dev

Change a cell's background color dynamically according to a RGB value stored in other cells

From Dev

Datatables - How do I change background and text color of a cell changed dynamically?

From Dev

How dynamically to change the formula of a cell

From Dev

Dynamically change TableView Cell height

From Dev

Dynamically change cell height programmatically

From Dev

Change Background of UICollectionView Cell on Tap

From Dev

Dynamically change to Listview frm Gridview

From Dev

How to dynamically change the background of a button

From Dev

Dynamically change element background (gradient)

From Dev

Change background color on textview dynamically

From Dev

Dynamically change element background (gradient)

From Dev

Jquery change background dynamically not working

From Dev

Change background color selecteditem Listview

From Dev

Android listview item background change

From Dev

ListView Change background of single item

From Dev

Change button background in ListView and CustomAdapter

From Dev

ListView Change background of single item

From Dev

JavaFx Set Tableview Cell Background Color Dynamically

From Dev

Dynamically change value of a <select> inside cell of a table

Related Related

  1. 1

    SSRS Dynamically change the cell background and font color

  2. 2

    SSRS Dynamically change the cell background and font color

  3. 3

    WPF: Change DataGrid cell/row background color dynamically at runtime

  4. 4

    How to change the background of a cell dynamically in a Kendo MVC UI Grid?

  5. 5

    How do I dynamically change the background of an item in a listview in JavaFX

  6. 6

    Change Listview cell opacity

  7. 7

    Change Listview cell opacity

  8. 8

    Change background colour dynamically

  9. 9

    Change background of disabled listView

  10. 10

    Dynamically changing the background of ListView child

  11. 11

    Change a cell's background color dynamically according to a RGB value stored in other cells

  12. 12

    Datatables - How do I change background and text color of a cell changed dynamically?

  13. 13

    How dynamically to change the formula of a cell

  14. 14

    Dynamically change TableView Cell height

  15. 15

    Dynamically change cell height programmatically

  16. 16

    Change Background of UICollectionView Cell on Tap

  17. 17

    Dynamically change to Listview frm Gridview

  18. 18

    How to dynamically change the background of a button

  19. 19

    Dynamically change element background (gradient)

  20. 20

    Change background color on textview dynamically

  21. 21

    Dynamically change element background (gradient)

  22. 22

    Jquery change background dynamically not working

  23. 23

    Change background color selecteditem Listview

  24. 24

    Android listview item background change

  25. 25

    ListView Change background of single item

  26. 26

    Change button background in ListView and CustomAdapter

  27. 27

    ListView Change background of single item

  28. 28

    JavaFx Set Tableview Cell Background Color Dynamically

  29. 29

    Dynamically change value of a <select> inside cell of a table

HotTag

Archive