Created
March 9, 2016 09:35
-
-
Save bugabinga/0cc365227a235c3fb794 to your computer and use it in GitHub Desktop.
Demonstrating ChoiceBox focus behavior in JavaFx. https://stackoverflow.com/questions/35869386/javafx-automatically-expand-choicebox-when-on-focus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.isp.stackoverflow; | |
import java.util.Arrays; | |
import javafx.application.Application; | |
import javafx.beans.value.ChangeListener; | |
import javafx.scene.Scene; | |
import javafx.scene.control.ChoiceBox; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.layout.GridPane; | |
import javafx.scene.layout.StackPane; | |
import javafx.stage.Stage; | |
/** | |
* https://stackoverflow.com/questions/35869386/javafx-automatically-expand-choicebox-when-on-focus | |
* | |
* @author okr | |
* @date 09.03.2016 | |
* | |
*/ | |
public class ChoiceBoxFocusApp extends Application | |
{ | |
/** | |
* @param args ignored. | |
*/ | |
public static void main( final String[] args ) | |
{ | |
launch( args ); | |
} | |
@Override | |
public void start( final Stage primaryStage ) | |
{ | |
final StackPane root = new StackPane(); | |
final GridPane pane = new GridPane(); | |
final ChoiceBox<String> box1 = new ChoiceBox<>(); | |
box1.getItems().addAll( "1", "2", "3" ); | |
final ChoiceBox<String> box2 = new ChoiceBox<>(); | |
box2.getItems().addAll( "a", "b", "c" ); | |
for ( final ChoiceBox<String> choiceBox : Arrays.asList( box1, box2 ) ) | |
{ | |
final ChangeListener<? super Boolean> showHideBox = ( __, ___, isFocused ) -> | |
{ | |
if ( isFocused.booleanValue() ) | |
{ | |
choiceBox.show(); | |
} | |
else | |
{ | |
choiceBox.hide(); | |
} | |
}; | |
choiceBox.focusedProperty().addListener( showHideBox ); | |
choiceBox.addEventFilter( MouseEvent.MOUSE_RELEASED, release -> | |
{ | |
release.consume(); | |
choiceBox.requestFocus(); | |
} ); | |
} | |
pane.add( box1, 0, 0 ); | |
pane.add( box2, 1, 0 ); | |
root.getChildren().add( pane ); | |
primaryStage.setScene( new Scene( root ) ); | |
primaryStage.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment