JavaFX ListView

0

JavaFX ListView tutorial:

JavaFX ListView displays a Vertical or Horizontal list of items.

Constructors of List view:

  • ListView()
    Creates a default ListView which will display contents stacked vertically.
  • ListView(ObservableList<T> items)
    Creates a default ListView which will stack the contents retrieved from the provided ObservableList vertically.

Creating ListView :

ListView<Type> listView = new ListView<Type>(names);
Type – This is type of objects which is stored in the listview.
For example here is Type = String. So
ListView<String> listView = new ListView<String>(names);

And List is observable list of type String AS
ObservableList<String> names = FXCollections.observableArrayList(
"Ram", "Shyam", "Ramu", "Sachin", "Amit", "Prem", "Anjli");

ListView Focus And Selection :

By default single selection model is enabled to enable multiple selection modle we use the SelectionModle Class AS

listView.getSelectionModel().setSelectionMode(
SelectionMode.MULTIPLE);

setSelectionMode() is used to set the selection mode.

To Returns the selected item from listview
getSelectionModel().getSelectedItem();

To Returns the focused item of listview
getFocusModel().getFocusedItem()

Example using ListView:


package javafxtuts;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
 *
 * @author javafxtuts
 */
public class Javafxtuts extends Application {

  @Override
  public void start(Stage primaryStage) {
      
// setting title of application
      primaryStage.setTitle("javafxtuts.com");
//To Creating a Observable arraylist of String Type 
      ObservableList names = FXCollections.observableArrayList("Ram", "Shyam", "Ramu", "Sachin", "Amit", "Prem", "Anjli");
//To create a ListView

   ListView listView = new ListView(names);

 //To set multiple selection model     listView.getSelectionModel()
 			.setSelectionMode(
            	SelectionMode.MULTIPLE);

	//set items using setItems() method 
       listView.setItems(names);
    

    StackPane root = new StackPane();
    root.getChildren().add(listView);
     
      

      //to create a scane using root
      Scene scene = new Scene(root, 250, 300);
      primaryStage.setScene(scene);
      primaryStage.show();
  }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}
Output of the ListView example :

Explanation of list view example:

  • Creating a Observable arraylist of String Type AS
    ObservableList names = FXCollections.observableArrayList(
    "Ram", "Shyam", "Ramu", "Sachin", "Amit", "Prem", "Anjli");
  • To create a ListView
    ListView listView = new ListView(names);
  • To set multiple selection model
    listView.getSelectionModel()
    			.setSelectionMode(
    			SelectionMode.MULTIPLE);
  • And set items using setItems() method AS
    listView.setItems(names);
Share.

Leave A Reply