2012-09-17 30 views

Respuesta

19

No puede hacerlo solo en el archivo FXML.
Definir la vista de lista correspondiente (suponiendo fx:id="myListView" en FXML) en la clase de controlador del archivo FXML:

@FXML 
private ListView<MyDataModel> myListView; 

Añadir oyente en init/Start método que va a escuchar a la vista de lista elemento cambiará:

myListView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<MyDataModel>() { 

    @Override 
    public void changed(ObservableValue<? extends MyDataModel> observable, MyDataModel oldValue, MyDataModel newValue) { 
     // Your action here 
     System.out.println("Selected item: " + newValue); 
    } 
}); 

MyDataModel puede ser su propia clase de modelo de estructura de datos o simplemente un String.
Por ejemplo cuerdas,

@FXML 
private ListView<String> myListView; 

... 
... 

ObservableList<String> data = FXCollections.observableArrayList("chocolate", "blue"); 
myListView.setItems(data); 

myListView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() { 
    @Override 
    public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { 
     // Your action here 
     System.out.println("Selected item: " + newValue); 
    } 
}); 
+0

gracias por la respuesta rápida. Pero tengo problemas con MyDataModel. Lo probé como una cadena ... como String a = "test"; y ObservableList data = FXCollections.observableArrayList ( "chocolate", "azul"); Ambos no funcionaron para mí ... me pide una clase ... ¿Pueden darme un ejemplo, por favor? –

Cuestiones relacionadas