Tuesday, May 22, 2012

JavaFX 2 UI Controls: ListView

This is JavaFX tutorial about JavaFX 2 ListView. The ListView class represents a scrollable list of items. Below is a example how our ListView will look in the end of this tutorial. In this tutorial, I am going to create JavaFX 2 ListView and implement OnMouseClicked Event Handler.
-->

It's simple example with just four list items. Also, I wanted here to implement and handle some basic action. As can be seen from the code I create method initActions(), where I detect on which ListView value is the mouse clicked. When user click on ListView, I set text to a label. This is a pretty basic stuff, because our goal isn't to handle some complex actions for this example. It's just important to understand how to handle actions with  ListView.
Below is the code of our example:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author zoranpavlovic.blogspot.com
*/
public class ListViewMain extends Application {
/**
* @param args the command line arguments
*/
VBox vb = new VBox();
Label lbl = new Label();
ListView<String> list = new ListView<>();
ObservableList<String> data =FXCollections.observableArrayList (
"List index 0",
"List index 1",
"List index 2",
"List index 3");
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX 2.0 ListView");
//Adding data to ListView
list.setItems(data);
//Actions on clicked list item
initActions();
//Adding BorderPane to the scene
vb.getChildren().addAll(list, lbl);
Scene scene = new Scene(vb,300,200);
primaryStage.setScene(scene);
primaryStage.show();
}
public void initActions(){
//Detecting mouse clicked
list.setOnMouseClicked(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent arg0) {
//Check wich list index is selected then set txtContent value for that index
if(list.getSelectionModel().getSelectedIndex() == 0){
lbl.setText("Selected index: 0");
}
else if(list.getSelectionModel().getSelectedIndex() == 1){
lbl.setText("Selected index: 1");
}
else if(list.getSelectionModel().getSelectedIndex() == 2){
lbl.setText("Selected index: 2");
}
else if(list.getSelectionModel().getSelectedIndex() == 3){
lbl.setText("Selected index: 3");
}
}
});
}
}
If you have any commentsquestions, and anything like that, please feel free to  leave  a comment below, or at least if you like this tutorial you can SHARE it with your friends.
-->



6 comments:

  1. Hi, Good work for all the blogs. It would be more helpful for JavaFX freshers if you have shown the Horizantal list view example also.

    Thanks & Regards,
    Sai Pradeep Dandem.

    ReplyDelete
  2. Thanks for the idea, I'll do tutorial about it. :)

    ReplyDelete
  3. That bunch of if-else-if in the click event looks awful. Change it to use binding or something like that

    ReplyDelete
  4. the example is nice can you give me an example for create a list by selecting an item in the list the data should be coming from database updated in the tableview.

    ReplyDelete
  5. I hope that I will have time to do tutorial for that. ;)

    ReplyDelete