import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Skin;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.util.stream.Collectors;
import java.util.stream.IntStream;


public class ControlDiscardsSkinOfSameClassWithoutDisposing extends Application {

   public static void main( final String[] args ) {
      launch( args );
   }

   @Override
   public void start( final Stage primaryStage ) throws Exception {
      final ComboBox<Integer> comboBox = new ComboBox<>(
            FXCollections.observableList( IntStream.rangeClosed( 0, 20 )
                                                   .boxed().collect( Collectors.toList() ) )
      );
      comboBox.setEditable( true );
      final StackPane root = new StackPane( comboBox );
      root.setStyle( "-fx-min-width: 30em; -fx-min-height: 30em;" );
      final Scene scene = new Scene( root );
      scene.getStylesheets().add( getClass()
            .getResource( "ControlDiscardsSkinOfSameClassWithoutDisposing.css" )
            .toExternalForm() );
      primaryStage.setScene( scene );
	  primaryStage.setTitle(System.getProperty("javafx.version"));
      primaryStage.sizeToScene();
      primaryStage.centerOnScreen();
      primaryStage.show();
      // Now, the ComboBox popup ListView has an initial ListViewSkin installed on it,
      // but Control.skin.setSkin(Skin) discarded a second ListViewSkin and failed to call dispose() on it.
      // If you open the ComboBox popup and use the down arrow key to navigate past the bottom item in the viewport,
      // then you will see that the ListView fails to scroll to follow the selected item.
      // That is because the second (discarded) ListViewSkin's ListViewBehavior's InputMap KeyMappings are still
      // observing the ListView and consuming events to make calls on the second (discarded) ListViewSkin's
      // VirtualFlow, preventing the first (kept) ListViewSkin's ListViewBehavior's InputMap KeyMappings from
      // receiving the events and making calls on the VirtualFlow that is actually displayed.
   }
}
