JavaFX ListView create from ObservableList

Description

JavaFX ListView create from ObservableList



// Demonstrate a list view. 
 
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.MultipleSelectionModel;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage; 
 
public class Main extends Application { 
   private Label response; 
 
  public static void main(String[] args) { 
 
    launch(args);   /* www .  j a va2 s  .co m*/
  } 
  public void start(Stage myStage) { 
    myStage.setTitle("ListView Demo"); 
    FlowPane rootNode = new FlowPane(10, 10); 
    rootNode.setAlignment(Pos.CENTER); 
    Scene myScene = new Scene(rootNode, 200, 120); 
    myStage.setScene(myScene); 
    response = new Label("Select Type"); 
 
    ObservableList<String> dataList = 
      FXCollections.observableArrayList( "HTML", "CSS", "Java" );  
 
    // Create the list view. 
    ListView<String> listView = new ListView<String>(dataList); 
 
    // Set the preferred height and width. 
    listView.setPrefSize(80, 80); 
 
    // Get the list view selection model. 
    MultipleSelectionModel<String> lvSelModel = 
                                     listView.getSelectionModel(); 
 
    lvSelModel.selectedItemProperty().addListener( 
                                      new ChangeListener<String>() { 
      public void changed(ObservableValue<? extends String> changed, 
                          String oldVal, String newVal) { 
 
        // Display the selection. 
        response.setText("selected is " + newVal); 
      } 
     }); 
 
    // Add the label and list view to the scene graph. 
    rootNode.getChildren().addAll(listView, response); 
 
    // Show the stage and its scene. 
    myStage.show(); 
  } 
}



PreviousNext

Related