JavaFX: create a ContextMenu on a ListView

We need a Person class, to display something on our ListView.

/*
 * Copyright 2014 Patrick Ott .
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.professional_webworkx.blog.contextmenudemo.model;

/**
 *
 * @author Patrick Ott 
 * @version 1.0
 */
public class Person {

    private String firstName;
    private String lastName;
    private String eMail;
    
    public Person(final String firstName, final String lastName, final String eMail) {
        this.firstName  = firstName;
        this.lastName   = lastName;
        this.eMail      = eMail;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String geteMail() {
        return eMail;
    }

    public void seteMail(String eMail) {
        this.eMail = eMail;
    }

    @Override
    public String toString() {
        return firstName + " " + lastName + ", E-Mail: " + eMail; 
    }
    
}

The Main class

package de.professional_webworkx.blog.contextmenudemo;

import de.professional_webworkx.blog.contextmenudemo.model.Person;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.ListView;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author Patrick Ott 
 */
public class App extends Application {
    
    @Override
    public void start(Stage primaryStage) {

        List persons = new ArrayList<>();
        persons.add(new Person("Patrick", "Ott", "[email protected]"));
        persons.add(new Person("Hans", "Meier", "[email protected]"));
        ObservableList observableList = FXCollections.observableList(persons);
        ListView personList = new ListView(observableList);
        
        final ContextMenu contextMenu = new ContextMenu();
        MenuItem print1 = new MenuItem("Print on Laser Printer");
        MenuItem print2 = new MenuItem("Print on Ink Printer");
        
        contextMenu.getItems().addAll(print1, print2);
        MenuItem editItem = new MenuItem("Show E-Mail Address");
        editItem.setOnAction(new EventHandler() {

            @Override
            public void handle(ActionEvent t) {
                Person selectedItem = personList.getSelectionModel().getSelectedItem();
                System.out.println(selectedItem.geteMail());
            }
        });
        
        contextMenu.getItems().add(editItem);
        personList.setContextMenu(contextMenu);
        personList.setOnContextMenuRequested(new EventHandler() {

            @Override
            public void handle(ContextMenuEvent event) {
                contextMenu.show(personList, event.getScreenX(), event.getScreenY());
                event.consume();
            }
            
        });
        
        VBox root = new VBox(personList);
        Scene scene = new Scene(root, 1024, 768);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ContextMenu Demo");
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
}