Issue
I've got a simple password generator in JavaFX. Whenever I click the button, a password is generated but also when I click somewhere else in the application area, the button also reacts. How can I reduce the click area?
Main
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/sample/main.fxml"));
primaryStage.setTitle("Password Generator");
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.setMaxWidth(610.0);
primaryStage.setMaxHeight(450.0);
primaryStage.setMinWidth(600.0);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Main.fxml
<Pane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="416.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onMouseClicked="#generatePassword" prefHeight="417.0" prefWidth="600.0" style="-fx-background-color: #ed801a;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button fx:id="generatePassword" layoutX="206.0" layoutY="216.0" mnemonicParsing="false" onMouseClicked="#generatePassword" prefHeight="59.0" prefWidth="188.0" text="Generate">
<font>
<Font name="System Italic" size="20.0" />
</font>
</Button>
</children>
</AnchorPane>
</children>
</Pane>
Solution
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onMouseClicked="#generatePassword" prefHeight="417.0" prefWidth="600.0" style="-fx-background-color: #ed801a;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
Remove the onMouseClicked="#generatePassword"
from the AnchorPane
declaration.
Answered By - Puce
Answer Checked By - David Goodson (JavaFixing Volunteer)