Попытка динамически добавлять панели. Не уверен, почему они не появляются - PullRequest
0 голосов
/ 16 апреля 2019

Я пытаюсь динамически добавить несколько панелей в hbox.У окон есть свой класс.На каждой панели есть кнопка, изображение и метка.Я создал цикл for, который берет 5 продуктов и добавляет один на панель, а затем так далее.У меня была проблема: я дважды вызывал метод @FXML private void initialize().Это добавило 5 продуктов к 5 панелям, не выводило их, затем снова запустило, добавив еще 5 - так что теперь их 10 - и вставив эти 10 в hbox.Я удалил лишнюю initialize(), но теперь ничего не печатает.

домашняя страница магазина

public class StoreHomePageController {

private MainController mainController;


private HBox scrollableHBox = new HBox();
private String fileName;
private Product waterProduct = new Product("", 999.00, 1,
        "/0.png");
private Product pollutedWater = new Product("", 4.99, 25,
        "/1.png");
private Product rawStreamWater= new Product("", 24.99, 50, "/2.png");
private Product rawStreamWater2= new Product("", 24.99, 50, "/3.png");
private Product rawStreamWater1= new Product("", 24.99, 50, "/4.png");
private List<Product> forSaleHBoxProducts = new ArrayList<>();]

public StoreHomePageController(MainController mainController) {

    this.mainController = mainController;
    this.fileName = waterProduct.getFileName();

    HBox hb = new HBox();
    borderPane.setTop(hb);
    hb.getStyleClass().add("hb");
    addToForSalesPane();
}
public void addToForSalesPane(){
    forSaleHBoxProducts.add(waterProduct);
    forSaleHBoxProducts.add(rawStreamWater);
    forSaleHBoxProducts.add(rawStreamWater1);
    forSaleHBoxProducts.add(pollutedWater);
    forSaleHBoxProducts.add(rawStreamWater2);
    for (Product p: forSaleHBoxProducts){
        scrollableHBox.getChildren().add(new ProductPaneHomePage(p));
    }
}

@FXML
private void initialize(){
//        addToForSalesPane();
// putting this here causes the method to run twice. it adds 5 
products, doesn't add them
// then adds 5 more and adds those 10.
    salesScrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
    salesScrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
    }
}

панель, которую я пытаюсь добавить

public class ProductPaneHomePage extends Pane {

private ImageView quickLookImageView = new ImageView();
private Label quickLookLabel = new Label();
private Button quickLookBtn = new Button("Buy Now!");
private Product product;
private File file;
private Image image;
private String fileName;

public ProductPaneHomePage(Product product){

    this.product = product;
    this.fileName = this.product.getFileName();
    file = new File(this.fileName);

    setPrefHeight(218);
    setPrefWidth(200);

    getChildren().add(quickLookImageView);
    setQuickLookImageViewDimensions();

    getChildren().add(quickLookLabel);
    setQuickLookLabelDimensions();

    getChildren().add(quickLookBtn);
    setText();
 //   initialize();
}
//  private void initialize(){
//     setQuickLookLabelDimensions();
//      setQuickLookImageViewDimensions();
//      setText();
//  }
private void setQuickLookImageViewDimensions(){
    quickLookImageView.setFitHeight(196);
    quickLookImageView.setFitWidth(200);
    quickLookImageView.setLayoutX(0);
    quickLookImageView.setLayoutY(14);
    quickLookImageView.setImage(getImage());
}
private void setQuickLookLabelDimensions(){
    quickLookLabel.setAlignment(Pos.CENTER);
    quickLookLabel.setLayoutX(55);
    quickLookLabel.setLayoutY(220);
    quickLookLabel.setPrefHeight(17);
    quickLookLabel.setPrefWidth(10);
    quickLookLabel.setTextAlignment(TextAlignment.CENTER);
}
public Image getImage(){age = this.product.getImage();
    return image;
}
public void setText(){
    quickLookLabel.setText(this.product.getName());
}
}

Класс продукта

public class Product {

private String name;
private String companyName;
private double price;
private int quantity;
private String description;
private ImageView imageView;
private String fileName;
private String info;
private Image image;

public Product(String name, double price, int quantity, String fileName)  {
    this.name = name;
    this.companyName = companyName;
    this.price = price;
    this.description = description;
    this.imageView = imageView;
    this.quantity = quantity;
    this.fileName = fileName;
    this.info = info;
    this.image = new Image(this.fileName);
}
public String getName(){
    return this.name;
}
public String getCompanyName(){
    return this.companyName;
}
public double getPrice(){
    return this.price;
}
public Image getImage(){
    return image;
}
public ImageView getImageView(){ return this.imageView; }
public String getFileName(){return this.fileName;}
}

В идеале, пятьпанели выводятся в hbox.В настоящее время нет ни одного.Но если я добавлю избыточный initialize() метод, я могу получить 10 застрявших панелей. Спасибо!

...