JavaFX – ListView


JavaFX – ListView



”;


The ListView is a graphical user interface component used for displaying a list of items from which a user can select desired items. Generally, a list refers to a group or collection of items. It helps in organizing, structuring and presenting information in more user-friendly and readable way. The figure below illustrates a list view of cities from which a user can choose one option −


JavaFX ListView

ListView in JavaFX

In JavaFX, the list view is represented by a class named ListView which is a part of javafx.scene.control package. We can create a list view component by instantiating this class. Additionally, we have the option to select its orientation, allowing it to be displayed either vertically or horizontally. List of constructors of the ListView class is as follows −

  • ListView() − It is the default constructor that constructs a vertical list view.

  • ListView(ObservableList<type> listItems) − It constructs a new vertical list view with the specified list items.

Creating ListView in JavaFX

To create a ListView in any JavaFX application, we can use either the default contructor or the parameterized constructor of the ListView class. If the default constructor is used, we should pass the list items explicitly. The parameterized constructor accepts an ArrayList object as a parameter value as shown in the below code block −


//list View for educational qualification
ObservableList<String> names = FXCollections.observableArrayList("Engineering", "MCA", "MBA", "Graduation", "MTECH", "Mphil", "Phd");
ListView<String> listView = new ListView<String>(names);

Once the ListView class is instantiated and its items are added, define any layout pane such VBox or HBox to hold the list view. Next, create a Scene object by passing the layout pane obejct and the dimensions of the Scene to its constructor. Then, set the stage and launch the application to display the result.

Example

In the following example, we are going to demonstrates how to create ListView in JavaFX. Save this code in a file with the name JavafxListview.java.


import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class JavafxListview extends Application {
   public void start(Stage stage) {
      //Label for education
      Label label = new Label("Educational qualification:");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      label.setFont(font);
      //list View for educational qualification
      ObservableList<String> names = FXCollections.observableArrayList("Engineering", "MCA", "MBA", "Graduation", "MTECH", "Mphil", "Phd");
      ListView<String> listView = new ListView<String>(names);
      listView.setMaxSize(200, 160);
      //Creating the layout
      VBox layout = new VBox(10);
      layout.setPadding(new Insets(5, 5, 5, 50));
      layout.getChildren().addAll(label, listView);
      layout.setStyle("-fx-background-color: BEIGE");
      //Setting the stage
      Scene scene = new Scene(layout, 400, 300);
      stage.setTitle("List View Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Compile and execute the saved Java file from the command prompt using the following commands −


javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxListview.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxListview

Output

On executing the above code, it will generate a window displaying a ListView as shown in the below output −


Listview Output

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *