In FX 2.2 when a button has focus and you press the Enter key it animated a press and fired an on action event. This doesn't seem to happen anymore in FX 8.0 !? Only space works now. It doesn't matter if you've set the action in code, button.setOnAction( someAction ); or if you've set it in FXML, onAction="#handleAction", the result is the same - i.e. no action on Enter only Space.
Try it yourself, here's a code snippet to quickly test:
public class ButtonTest extends Application
{
public void start( Stage primaryStage )
{
final Button but = new Button( "Press Me" );
but.setOnAction( new EventHandler<ActionEvent>()
{
private int count = 1;
public void handle( ActionEvent arg0 )
{
but.setText( "Button Pressed: "+ count++ );
}
} );
BorderPane bp = new BorderPane();
bp.setPadding( new Insets( 10 ) );
bp.setCenter( but );
Scene scene = new Scene( bp, 200, 50 );
primaryStage.setScene( scene );
primaryStage.show();
but.requestFocus();
}
public static void main( String[] args )
{
launch( args );
}
}
Apparently this change is by design, see RT-28779 Thankfully this isn't a trainsmash in my app, and where I wanted or needed Enter to work I simply added a key pressed handler, button.setOnKeyPressed( enterAction ); or added the handler into my FXML with onKeyPressed="#handelEnter" where the handle method is implemented as:
public void handle( KeyEvent KE )Note that the isArmed test is needed otherwise in FX 2.2 the action event will be executed twice.
{
if ( KE.getCode() == KeyCode.ENTER )
{
if ( ! button.isArmed() ) button.fire();
}
}
Alternatively you can try button.setDefaultButton( true ) instead of the above. Note however that the button style changes and that it responds to Enter even when not focused.
Next up, two minor TabPane issues .... see part 9.
No comments:
Post a Comment