Przeglądaj źródła

Minor change

MFXListCell: fixed bug where the cell background was not updated if the item was selected programmatically.
Signed-off-by: PAlex404 <alessandro.parisi406@gmail.com>
PAlex404 4 lat temu
rodzic
commit
548ad7521a

+ 18 - 4
demo/src/main/resources/io/github/palexdev/materialfx/demo/combo_boxes_demo.fxml

@@ -1,12 +1,11 @@
 <?xml version="1.0" encoding="UTF-8"?>
 
 <?import io.github.palexdev.materialfx.controls.legacy.*?>
-<?import io.github.palexdev.materialfx.controls.MFXCheckbox?>
-<?import io.github.palexdev.materialfx.controls.MFXComboBox?>
+<?import io.github.palexdev.materialfx.controls.*?>
 <?import javafx.geometry.*?>
 <?import javafx.scene.control.Label?>
 <?import javafx.scene.layout.*?>
-<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" stylesheets="@css/combo_boxes_demo.css" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="io.github.palexdev.materialfx.demo.controllers.ComboBoxesDemoController">
+<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="450.0" prefWidth="600.0" stylesheets="@css/combo_boxes_demo.css" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="io.github.palexdev.materialfx.demo.controllers.ComboBoxesDemoController">
    <Label id="customLabel" alignment="CENTER" prefHeight="26.0" prefWidth="266.0" text="Legacy Combo Boxes" StackPane.alignment="TOP_CENTER">
       <StackPane.margin>
          <Insets top="20.0" />
@@ -54,7 +53,7 @@
    </MFXLegacyComboBox>
    <MFXCheckbox fx:id="checkbox" text="Validation!" StackPane.alignment="BOTTOM_RIGHT">
       <StackPane.margin>
-         <Insets bottom="160.0" left="10.0" right="10.0" top="10.0" />
+         <Insets bottom="200.0" left="10.0" right="10.0" top="10.0" />
       </StackPane.margin>
    </MFXCheckbox>
    <Label id="customLabel" alignment="CENTER" prefHeight="26.0" prefWidth="266.0" text="New Combo Boxes">
@@ -77,4 +76,19 @@
          <Insets left="250.0" top="185.0" />
       </StackPane.margin>
    </MFXComboBox>
+   <Label id="customLabel" alignment="CENTER" prefHeight="26.0" prefWidth="266.0" text="Filter Combo Box" StackPane.alignment="BOTTOM_CENTER">
+      <StackPane.margin>
+         <Insets bottom="70.0" />
+      </StackPane.margin>
+   </Label>
+   <MFXFilterComboBox fx:id="filters1" StackPane.alignment="BOTTOM_CENTER">
+      <StackPane.margin>
+         <Insets bottom="25.0" right="150.0" />
+      </StackPane.margin>
+   </MFXFilterComboBox>
+   <MFXFilterComboBox fx:id="filters2" comboStyle="STYLE2" StackPane.alignment="BOTTOM_CENTER">
+      <StackPane.margin>
+         <Insets bottom="25.0" left="150.0" />
+      </StackPane.margin>
+   </MFXFilterComboBox>
 </StackPane>

+ 16 - 0
materialfx/src/main/java/io/github/palexdev/materialfx/controls/cell/MFXListCell.java

@@ -34,6 +34,7 @@ import javafx.scene.paint.Paint;
 import javafx.util.Duration;
 
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * This is the implementation of a ListCell restyled to comply with modern standards.
@@ -95,6 +96,12 @@ public class MFXListCell<T> extends ListCell<T> {
             }
         });
 
+        backgroundProperty().addListener((observable, oldValue, newValue) -> {
+            if (newValue != null && isSelected() && !containsFill(newValue.getFills(), getSelectedColor())) {
+                NodeUtils.updateBackground(this, getSelectedColor(), new CornerRadii(getCornerRadius()), new Insets(getBackgroundInsets()));
+            }
+        });
+
         hoverProperty().addListener((observable, oldValue, newValue) -> {
             if (isSelected() || isEmpty()) {
                 return;
@@ -127,6 +134,15 @@ public class MFXListCell<T> extends ListCell<T> {
             rippleGenerator.createRipple();
         });
     }
+
+    private boolean containsFill(List<BackgroundFill> backgroundFills, Paint fill) {
+        List<Paint> paints = backgroundFills.stream()
+                .map(BackgroundFill::getFill)
+                .collect(Collectors.toList());
+
+        return paints.contains(fill);
+    }
+
     //================================================================================
     // Styleable Properties
     //================================================================================