So I've tried to follow this as closely as I could but when I run my program it just displays an empty listview, instead of my custom listcells; and without showing any error.
Big light grey rectangle in the middle is the listview.
Here is my code:-
Main FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="1045.0" prefWidth="1770.0" style="-fx-background-color: #2E2E2E;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.BuyerVendors">
<children>
<AnchorPane layoutX="70.0" layoutY="40.0" prefHeight="900.0" prefWidth="1630.0" style="-fx-background-color: #404040;">
<opaqueInsets>
<Insets />
</opaqueInsets>
<children>
<ListView fx:id="listview" layoutX="125.0" layoutY="92.0" prefHeight="900.0" prefWidth="1630.0" style="-fx-background-color: #404040;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</children>
</AnchorPane>
Listcell FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<fx:root maxHeight="200.0" maxWidth="1630.0" minHeight="200.0" minWidth="1630.0" prefHeight="200.0" prefWidth="1630.0" style="-fx-background-color: #404040;" type="AnchorPane" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="name" layoutX="200.0" layoutY="35.0" maxHeight="600.0" prefWidth="600.0" text="Abc Xyz Fresh Greens" textFill="WHITE" underline="true">
<font>
<Font size="30.0" />
</font>
</Label>
<Label fx:id="address" alignment="TOP_LEFT" layoutX="200.0" layoutY="100.0" maxHeight="63.0" maxWidth="600.0" prefHeight="63.0" prefWidth="600.0" text="East of Trodden, 910 019." textFill="WHITE" wrapText="true">
<font>
<Font size="18.0" />
</font>
</Label>
<ImageView fitHeight="36.0" fitWidth="36.0" layoutX="1000.0" layoutY="35.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../icons/star.png" />
</image>
</ImageView>
<Label fx:id="stars" layoutX="1050.0" layoutY="45.0" text="4.9/5" textFill="WHITE">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="reviews" layoutX="1100.0" layoutY="45.0" text="(200 reviews)" textFill="WHITE">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="email" layoutX="1000.0" layoutY="103.0" text="Contact: abc.xyz@gmail.com" textFill="WHITE" />
</children>
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
</fx:root>
Main controller class along with object 'Vendors' and listcell controller class:
package application;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.util.Callback;
//Controller class for Vendors listview
public class BuyerVendors implements Initializable {
//Getters, setters and constructor for list/listview object
public static class Vendors {
String email, name, address, no_of_reviews, rating;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
public String getNo_of_reviews() {
return no_of_reviews;
}
public void setNo_of_reviews(String no_of_reviews) {
this.no_of_reviews = no_of_reviews;
}
public Vendors(String email, String name, String address, String rating, String no_of_reviews) {
super();
this.email = email;
this.name = name;
this.address = address;
this.rating = rating;
this.no_of_reviews = no_of_reviews;
}
}
ObservableList<Vendors> list;
//Controller class for Vendors listcell
public class VendorsCell extends ListCell<Vendors> {
@FXML
private Label name;
@FXML
private Label address;
@FXML
private Label stars;
@FXML
private Label reviews;
@FXML
private Label email;
public VendorsCell() {
loadFXML();
}
private void loadFXML() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("../FXML files/VendorsCell.fxml"));
loader.setController(this);
loader.setRoot(this);
loader.load();
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
protected void updateItem(Vendors item, boolean empty) {
super.updateItem(item, empty);
if(empty) {
setText(null);
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
else {
name.setText(item.getName());
address.setText(item.getAddress());
if(item.getRating() == null) {
stars.setText("No ratings yet");
reviews.setText("");
}
else {
stars.setText(item.getRating());
reviews.setText(item.getNo_of_reviews());
}
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
}
}
//CellFactory
public class VendorsCellFactory implements Callback<ListView<Vendors>, ListCell<Vendors>> {
@Override
public ListCell<Vendors> call(ListView<Vendors> param) {
return new VendorsCell();
}
}
@FXML
private ListView<Vendors> listview;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
try {
list = UserDB.displayVendors(); // Function that returns observable list from database
} catch (SQLException e) {
e.printStackTrace();
}
listview = new ListView<Vendors>(list);
listview.setCellFactory(new VendorsCellFactory());
}
}
PS: Please excuse me if there are some silly errors; I don't have much experience using javaFX or java itself. :(