Listview_setOnAction1
  • getSelectionModel().selectedIndexProperty().addListener(newChangeListener
 
Code :

package javafxtemplate1;

import java.sql.SQLException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.Label;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;

import javafx.stage.Stage;

public class JavaFXTemplate1 extends Application {
@Override
public void start(Stage primaryStage) {
Group groot = new Group();
String str1 = "Selected ";
//
ObservableList<String> oString1 = FXCollections.<String>observableArrayList(null,"Jan", "FEB", "MAR");
ListView<String> lview1 = new ListView(oString1);
//lview1.getItems().addAll(null,"Jan", "FEB", "MAR");
lview1.setLayoutX(100);lview1.setLayoutY(10);
Button btn2 = new Button("Exit Platform");
btn2.setLayoutX(10);btn2.setLayoutY(80);
Label label1 = new Label("Label");
label1.setLayoutX(10);label1.setLayoutY(40);
//Class Implementing Event handler
btn2.setOnAction(new DemoEventHandler1(){ });
//
//lview1.getSelectionModel().select(index)
lview1.getSelectionModel().selectFirst();
//
lview1.getSelectionModel().selectedIndexProperty().addListener(new
ChangeListener<Number>() {
@Override
public void changed(ObservableValue ov,
Number value, Number new_value) {
label1.setText(lview1.getItems().toString());

label1.setText(str1+ Integer.toString(new_value.intValue()));
//label1.setText(str1+ Integer.toString(new_value.hashCode()));
}
});

//groot.getChildren().addAll(btn2,label1);
groot.getChildren().addAll(btn2,label1, lview1);
Scene scene = new Scene(groot, 350, 150);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFx ListView");
primaryStage.show();
}

public static void main(String[] args) throws SQLException {
launch(args);
}
}
class DemoEventHandler1 implements EventHandler<ActionEvent>{

@Override
public void handle(ActionEvent event) {
Platform.exit();

}
}

 
Runtime views: