java - Closing stage on key release -
i have created vbox in javafx comes pop on application based on hot key combination alt + j. want close vbox when release key combination alt + j. piece of code
final stage dialog = new stage(); eventhandler handler = new eventhandler<keyevent>() { public void handle( keyevent event ) { if ( event.isaltdown() && event.getcode() == keycode.j ) { dialog.initstyle( stagestyle.undecorated ); // dialog.initmodality(modality.application_modal); vbox dialogvbox = new vbox( 25 ); dialogvbox.getchildren().add( new text( "abc" ) ); scene dialogscene = new scene( dialogvbox, 300, 200 ); dialog.setscene( dialogscene ); dialog.show(); } else if ( keyevent.key_released.equals( eventrel.isaltdown() && eventrel.getcode() == keycode.j ) ) { dialog.hide(); } } }; scene.addeventhandler( keyevent.key_pressed, handler );
but not close expected. please guide me vbox close on key release
as per comment created separate handler key release : did not close vbox
eventhandler handlerrel = new eventhandler<keyevent>() { public void handle(keyevent eventrel) { //event.consume(); if(keyevent.key_released.equals(eventrel.isaltdown() && eventrel.getcode() == keycode.j)) { system.out.println("inside released"); dialog.hide(); } }}; scene.addeventhandler(keyevent.key_released, handlerrel);
thanks
you attaching 2 handlers same scene. however, once 2nd scene created, namely dialogscene, becomes active , scene receiving events. therefore, fix solution attach key_released event handler dialogscene , not original scene.
dialogscene.addeventhandler(keyevent.key_released, event -> { if (event.isaltdown() && event.getcode() == keycode.j ) { dialog.hide(); } });
Comments
Post a Comment