javafxjavafx-8accelerator

JavaFX Accelerator execute twice


I'm using Accelerator in my application, and when I hit the F1 key it should open a dialog just once, but when I close the dialog it appears again. The runnable is been called twice.

Does anyone know what it could be? Is it a bug? I'm using a Macbook to develop.

This is my controller:

public class Screen1Controller { 

    @FXML
    private TextField txtCode;

    @FXML
    private TableView tblProducts;

    @FXML
    private Button buttonAskUserDecision;

    @FXML 
    public void initialize () {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                initializeKeyCombinations();
            }
        });
    }

    @FXML
    private void executePrint (ActionEvent event) {
        System.out.println("ExecutePrint");
    }

    public void initializeKeyCombinations() {
        buttonAskUserDecision.getScene().getAccelerators().clear();

        buttonAskUserDecision.getScene().getAccelerators().put(
            new KeyCodeCombination(KeyCode.F1),
            new Runnable() {
                @Override
                public void run() {
                    System.out.println("Executed F1");
                }
            }
        );
    }
}

This is my FXML:

<StackPane prefHeight="600.0" prefWidth="800.0" stylesheets="@../../../css/main.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Screen1Controller">
<children>
    <BorderPane prefHeight="200.0" prefWidth="200.0">
        <center>
            <ScrollPane fitToHeight="true" fitToWidth="true" BorderPane.alignment="CENTER">
                <content>
                    <VBox alignment="TOP_CENTER">
                        <children>

                            <GridPane hgap="10.0">
                                <columnConstraints>
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="20.0" />
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="20.0" />
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="30.0" />
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="30.0" />
                                </columnConstraints>
                                <rowConstraints>
                                    <RowConstraints fillHeight="false" minHeight="30.0" vgrow="NEVER" />
                                </rowConstraints>
                                <children>
                                    <TextField fx:id="txtCode" GridPane.columnSpan="2" />
                                </children>
                                <VBox.margin>
                                    <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                                </VBox.margin>
                            </GridPane>
                            <TableView fx:id="tblProducts" prefHeight="200.0" prefWidth="200.0">
                                <columns>
                                    <TableColumn prefWidth="75.0" text="C1" />
                                    <TableColumn prefWidth="75.0" text="C2" />
                                </columns>
                            </TableView>
                        </children>
                    </VBox>
                </content>
            </ScrollPane>
        </center>
        <bottom>
            <ToolBar maxHeight="80.0" minHeight="80.0" prefHeight="80.0" styleClass="default-skin-pdv-toolbar" stylesheets="@../../../css/main.css">
                <items>
                    <Button fx:id="buttonAskUserDecision" layoutX="10.0" layoutY="20.0" mnemonicParsing="false" onAction="#executePrint" prefHeight="60.0" prefWidth="69.0" styleClass="default-skin-pdv-button">
                    </Button>

                </items>
                <padding>
                    <Insets left="10.0" />
                </padding>
            </ToolBar>
        </bottom>
    </BorderPane>
</children>

Inside this screen I have a textfield, a tableview and a toolbar with a button.

I did some new tests. Every time I give focus on my tableview and hit the F1 button, the execution is incremented by 1. For instance, if I click on the tableview and hit F1, it prints "Executed F1" just once on the console and the focus go back to the textfield, if i give focus to the tableview again and hit F1, It prints two times the text on the console and give focus back to the textfield, and if I give focus to the tableview for the third time and press the F1 again it prints three more lines on the console, and so on.

It only happens when I use the F1 button.. When I click the button with the mouse it prints only once.

I removed the TableView from the screen and it stopped duplicating the executions.

The problem is: I need this tableView in my screen. What could be happening here?

That's my Java version details:

java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

Solution

  • The problem was in a component (Dialog) from ControlsFX library.

    I created a new Project and included a textfield, viewTable and a button. The duplication didn't happened.

    When I included the ControlsFX Dialog, the started to happen. When I click the button, with the mouse, the dialog opens just once. But when I use the accelerator to fire the button, the dialog opens twice.

    Edited: I created my own dialog, but it happened again. Instead of using the stage.showAndWait() I tried the stage.show() and it stopped to open twice. I searched the stackoverflow and found this answer https://stackoverflow.com/a/22463857/916804 ... When I tried to wrap the call to the Dialog with the Platform.runLater the execution happened only once.