I want to make a customize list view in javafx. Here I need to bind multiple component in list cell as follow, like one label, one textfield, one button under one HBox and two button, one hyperlink, one label in another HBox and these HBox comes under one VBox and this VBox comes under the single list cell and it will repeat and make a list View.
The code is
<ListView fx:id="ListView" layoutX="0" layoutY="30" prefWidth="600" prefHeight="300">
<HBox fx:id="listBox" alignment="CENTER_LEFT">
<padding><Insets top="5" bottom="5" left="5"></Insets> </padding>
<HBox alignment="CENTER_LEFT" prefWidth="170" minWidth="88">
<Label fx:id="surveyName" text="Field A" styleClass="Name"></Label>
</HBox>
<VBox styleClass="Description" prefWidth="155" minWidth="86">
<HBox>
<HBox styleClass="surveyDesIcon" prefWidth="20" prefHeight="16"></HBox>
<Label fx:id="surveyCode" text="PRW3456HJ"></Label>
</HBox>
<HBox>
<HBox styleClass="DateIcon" prefWidth="20" prefHeight="16"></HBox>
<Label fx:id="Date" text="PRW3456HJ"></Label>
</HBox>
</VBox>
<HBox fx:id="Status" prefWidth="160" minWidth="80">
<Label fx:id="StatusLabel" text="Checking Files.."/>
</HBox>
<HBox fx:id="StatusIcon1" prefWidth="50" prefHeight="50" alignment="CENTER">
<Label styleClass="StatusIcon1" prefWidth="24" prefHeight="24" alignment="CENTER"/>
</HBox>
<HBox fx:id="StatusIcon2" prefWidth="50" prefHeight="50" styleClass="StatusIconBox" alignment="CENTER">
<Hyperlink styleClass="StatusIcon2" prefWidth="24" maxHeight="24" alignment="CENTER"/>
</HBox>
</HBox>
</ListView>
The answer by Anvay for some reason didnt work for me, what i had to do to fix it was just some very small tweaks:
I also had a main class (intellij auto generated).
I know this was probably obvious for most of the experts here, but these issues perplexed me for hours while i was debugging.
The example above by @Anvay needs a couple of tweaks to work. These are simple things to set on-track.
The ListViewController below works with these changes. I changed "stringSet" to a list, "stringList". The controller is pretty much the sample controller provided by Scene Builder 2
The JavaFX platform needs to be started in the main() method from a JavaFX Application. Netbeans conviently provides most of this structure from the Maven JavaFX application template.
I understand your question. There are mainly two ways to set items in a
Listview
:1. Create the
ObservableList
and set the items of theListView
with theObservableList
(listView.setItems(observableList)
).2. Use the
setCellFactory()
method of theListView
class.You would prefer to use the
setCellFactory()
method, because this approach simplies the process as well as it helps to separate out the business logic and the UI (FXML).Here is a more detailed explaination:
1. Create a new FXML file with the name
listview.fxml
to contain theListView
, and set theListViewController
class as its controller:File: listview.fxml:
2. Create the controller and name it
ListViewController
.The controller can load the
listview.fxml
file and access thelistview
.File: ListViewController.java:
3. First you need to set the value of the
ObservableList
. This is very important.Then, set the items of list using the
ObservableList
and call thesetCellFactory()
method on theListView
. In the given example I just take theString
values an add them to theString
set (theSet<String> stringSet
).4. When the
setCellFactory()
method is called on theListView
, it will return theListCell
. So for sake of simplicity, I added a class which extends theListCell
, and thesetGraphic()
method is present for theListCell()
and will set the items of theListCell
.File: ListViewCell.java:
5. I just added a class which will load the
listCellItem.fxml
and return theHBox
, which will contain the other components as children.The
HBox
is then set to theListCell
.File: listCellItem.fxml:
File: Data.java:
Using this way, you can use the
setCellFactory()
method to separate the things that are business logic and FXML.Hope this is helpful.