JavaFX: Setting up list view

This entry is part 36 of 55 in the series JavaFX

List view displays a list of items and there are many controls that we can use to set the display of the list items.

Here we display a list in vertical mode and we can set the max height of the list since we know that there are only 3 items in the list.

package javafxapplication29;
 
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
 
public class JavaFXApplication29 extends Application {
 
    @Override
    public void start(Stage stage) {
 
        stage.setTitle("JavaFX Program!");
 
        ListView<String> lv = new ListView<String>();
 
        lv.getItems().add("Rock");
        lv.getItems().add("Country");
        lv.getItems().add("Rap");
        lv.setMaxHeight(120);
 
        HBox hboxPane = new HBox(10); // spacing = 10       
        hboxPane.setPadding(new Insets(10, 10, 10, 10));
 
        Label lb = new Label("Selection:");
 
        hboxPane.getChildren().addAll(lb, lv);
 
        Scene scene = new Scene(hboxPane, 400, 300);
        stage.setScene(scene);
 
        stage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}

listview

Series Navigation<< JavaFX: Change listener for a Choice Box
JavaFX: Getting selected item from listview >>