Friday 15 June 2018

JavaFX: Create horizontal ListView

By setting the orientation to HORIZONTAL, you can create horizontal list view widget.

Ex
ListView<String> listView = new ListView<>();
listView.setOrientation(Orientation.HORIZONTAL);

Find the below working application.

ListViewApp.java
package com.sample.demos;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ListViewApp extends Application {

 @Override
 public void start(Stage primaryStage) {
  ListView<String> listView = new ListView<>();
  ObservableList<String> items = FXCollections.observableArrayList("Cricket", "Chess", "Kabaddy", "Badminton",
    "Football", "Golf", "CoCo", "car racing");
  listView.setItems(items);
  
  listView.setPrefWidth(400);
  listView.setPrefHeight(50);
  listView.setOrientation(Orientation.HORIZONTAL);

  HBox hBox = new HBox(listView);

  Scene scene = new Scene(hBox, 500, 200);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX ListView app Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(ListViewApp.class, args);
 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment