This JavaFX code snippet will show you how to perform an action when selecting a item in a list view.


package com.crazyandcoding;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

 * Code snippet from
 * @author Colt
public class ListViewSelection extends Application {

	private ListView<String> m_listView;

	public void start(Stage primaryStage) throws Exception {
		Group root = new Group();

		// a horizontal panel to hold the list view and the label.
		HBox listViewPanel = new HBox();

		// the text to be displayed when clicking on a new item in the list.
		final Text label = new Text("Nothing Selected.");
		label.setFont(Font.font(null, FontWeight.BOLD, 16));

		// create a list of items.
		m_listView = new ListView<String>(FXCollections.observableArrayList(
				"Item 1", "Item 2", "Item 3", "Item 4"));
				.addListener(new ChangeListener<String>() {

					public void changed(
							ObservableValue<? extends String> observable,
							String oldValue, String newValue) {
						// change the label text value to the newly selected
						// item.
						label.setText("You Selected " + newValue);

		listViewPanel.getChildren().addAll(m_listView, label);

		Scene scene = new Scene(root);
		primaryStage.setTitle("List View Selector");


	public static void main(String[] args) {


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>