Javafx - ListView - 当我点击一个项目时,fxml文件中定义的节点文本消失

时间:2018-04-25 16:01:38

标签: java listview javafx

你好我有一个必须显示产品的列表视图,但当我点击一个项目时,fxml文件中定义的节点文本消失了,(我们可以看到链接的轮廓,但文本消失了)。有什么问题吗?

结果:

Normal.png WhenIClick.png

CellProduct.java

import javafx.fxml.FXMLLoader;
import javafx.scene.control.ListCell;
import javafx.scene.layout.HBox;

public class CellProduct extends ListCell<Product> {

private HBox cellView;
private ProductCellController productCellController;

public CellProduct() {
    super();
    FXMLLoader PRODUCT_CELL_FXML_LOADER = LoaderFile.getFXMLLoader("ProductCellView");
    cellView = (HBox) LoaderFile.loadView(PRODUCT_CELL_FXML_LOADER);
    productCellController = PRODUCT_CELL_FXML_LOADER.getController();
    System.out.println(productCellController);
}

@Override
protected void updateItem(Product product, boolean empty) {
    super.updateItem(product, empty);

    if (product == null || empty) {
        setGraphic(null);
    } else {
        String urlImage = product.getUrlImage();
        double price = product.getPrice();
        String name = product.getName();
        String shortDescription = product.getShortDescription();
        String rating = String.valueOf(product.getRating());
        String numberOpinion = String.valueOf(product.getNumberOpinion());
        StateProduct state = product.getState();
        productCellController.init(urlImage, name, shortDescription, rating, numberOpinion, state);

        setGraphic(cellView);
        setOnMouseClicked(event -> System.out.println("Click"));
      }
   } 
}

LoaderFile.java

import javafx.fxml.FXMLLoader;
import javafx.scene.Node;

public class LoaderFile {
private class ClassForGetClass {}
public static FXMLLoader getFXMLLoader(String nameView) {
    return new FXMLLoader(ClassForGetClass.class.getResource(nameView + ".fxml"));
}

public static Node loadView(FXMLLoader fXMLLoader) {
    try {
        return fXMLLoader.load();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
       }
    }
}

Main.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Stage;

public class Main extends Application{

    public static void main (String[] args ) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        ListView<Product> listView = new ListView<Product>();

        Scene scene = new Scene(listView, 900, 500);
        Product product = new Product(1,"http://media.ldlc.com/ld/products/00/04/10/90/LD0004109014_2_0004109423_0004109488.jpg", false, 2000.0, "MSI GS63VR 7RF-262FR Stealth Pro 4K", "PC PORTABLE", "",2, 2.5, StateProduct.IN_STOCK, "", 1, "", 1);
        for (int index = 0; index < 30; index++) {
            listView.getItems().add(product);
        }

        listView.setCellFactory(param -> {
            System.out.println("Création d'une cellule");
            return new CellProduct();
        });

        primaryStage.setScene(scene);
        primaryStage.show();

    }
}

Product.java

    public class Product {
    private int id;
    private String urlImage;
    private boolean isSelected;
    private double price;
    private String name;
    private String shortDescription;
    private String longDescription;
    private int numberOpinion;
    private double rating;
    private StateProduct state;
    private String domain;
    private int domainID;
    private String category;
    private int categoryID;

    public Product(int id, String urlImage, boolean isSelected, double price, String name, String shortDescription, String longDescription, int numberOpinion, double rating, StateProduct state, String domain, int domainID, String category, int categoryID) {
        this.id = id;
        this.urlImage = urlImage;
        this.isSelected = isSelected;
        this.price = price;
        this.name = name;
        this.shortDescription = shortDescription;
        this.longDescription = longDescription;
        this.numberOpinion = numberOpinion;
        this.rating = rating;
        this.state = state;
        this.domain = domain;
        this.domainID = domainID;
        this.category = category;
        this.categoryID = categoryID;
    }

    public int getId() {
        return id;
    }

    public String getUrlImage() {
        return urlImage;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public double getPrice() {
        return price;
    }

    public String getName() {
        return name;
    }

    public String getShortDescription() {
        return shortDescription;
    }

    public String getLongDescription() {
        return longDescription;
    }

    public int getNumberOpinion() {
        return numberOpinion;
    }

    public double getRating() {
        return rating;
    }

    public StateProduct getState() {
        return state;
    }

    public String getDomain() {
        return domain;
    }

    public int getDomainID() {
        return domainID;
    }

    public String getCategory() {
        return category;
    }

    public int getCategoryID() {
        return categoryID;
    }
}

ProductCellController.java

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;

public class ProductCellController {
    @FXML private Hyperlink nameProductHyperLink;
    @FXML private CheckBox checkBoxProductCompare;
    @FXML private Label priceLabel;
    @FXML private ImageView productImageView;
    @FXML private Label descriptionProductLabel;
    @FXML private Label numberOpinionLabel;
    @FXML private VBox stateProductVBox;
    @FXML private Button addBasketButton;

    public void init(String urlImageProduct, String nameProduct, String descriptionProduct, String ratingProduct, String numberOpinion, StateProduct stateProduct) {
        // productImageView.setImage(new Image(urlImageProduct));
        productImageView.setImage(new Image("Laptop.png"));
        nameProductHyperLink.setText(nameProduct);
        descriptionProductLabel.setText(descriptionProduct);
        numberOpinionLabel.setText(numberOpinion + " Avis");
        priceLabel.setText("2000.00 $");

        String color = stateProduct.getColor();

        stateProductVBox.getChildren().clear(); // reset else old labels are not deleted, it is normal ?
        for (String string: stateProduct.getPartiesName()) {
            Label label = new Label(string);
            String style = "-fx-text-fill : " + color + "; -fx-font-weight: bold; -fx-font-size: 14px;"; // -fx-text-fill ne fonctionne pas avec les { }
            label.setStyle(style);
            stateProductVBox.getChildren().add(label);
        }

        addBasketButton.setGraphic(new ImageView(new Image("AddShoppingCart.png")));
        addBasketButton.setOnMouseClicked(event -> System.out.println("click"));
    }
}

StateProduct.java

    public enum StateProduct {
    IN_STOCK("EN-STOCK", "#15AA15"),
    BREAKING("RUPTURE", "#737373"),
    AVAILABLE_SUPPLIER("DISPO-FOURNISSEUR", "#062D8F"),
    WITHIN_7_DAYS("SOUS-7 JOURS", "#FFB502"),
    WITHIN_7_AND_15__DAYS("ENTRE-7/15 JOURS", "#FF6400"),
    MORE_THAN_15_DAYS("+ DE-15 JOURS", "#FF0606");

    private String[] partiesName;
    private String color;

    StateProduct(String partiesName, String color) {
        this.partiesName = partiesName.split("-");
        this.color = color;
    }

    public String[] getPartiesName() {
        return partiesName;
    }

    public String getColor() {
        return color;
    }

    @Override
    public String toString() {
        return String.join(" ", partiesName);
    }
}

Laptop.png AddShoppingCart.png

0 个答案:

没有答案