0

So I have this button. It can be pressed with the enter key, and can be selected through tabbing to it, but it cannot be clicked on, and doesn't appear to exist as far as the cursor is concerned. I don't believe anything is covering it, but it most be covered some how, as a button created with identical code is clickable.

public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
    primaryStage.setTitle("Mortgage Calculator");

    GridPane pane = new GridPane();
    pane.setAlignment(Pos.CENTER);
    pane.setHgap(10);
    pane.setVgap(10);
    pane.setPadding(new Insets(25, 25, 25, 25));
    Scene scene = new Scene(pane, 350, 325);

    Text sceneTitle = new Text("Mortgage Calculator");
    sceneTitle.setFont(Font.font("Arial", FontWeight.NORMAL, 20));
    pane.add(sceneTitle, 0, 0, 2, 1);
    Label total = new Label("Value of your Mortgage:");
    pane.add(total, 0, 1);
    final TextField totalField = new TextField();
    pane.add(totalField, 1, 1);
    Label percent = new Label("% Interest:");
    pane.add(percent, 0, 2);
    final TextField percentField = new TextField();
    pane.add(percentField, 1, 2);
    Label time = new Label("Length of mortgage:");
    pane.add(time, 0, 3);
    final TextField timeField = new TextField();
    pane.add(timeField, 1, 3);
    final Text amountOwed = new Text();
    pane.add(amountOwed, 1, 7);
    final Text payment = new Text();
    pane.add(payment, 1, 8);

    Button calculateButton = new Button("Calculate");
    HBox hbox1 = new HBox(10);
    hbox1.setAlignment(Pos.BOTTOM_RIGHT);
    hbox1.getChildren().add(calculateButton);
    calculateButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            double principal = Double.parseDouble(totalField.getText());
            double interest = (Double.parseDouble(percentField.getText()) / 100) / 12;
            int time = Integer.parseInt(timeField.getText());
            int numPayment = time * 12;

            double monthlyPayment = (principal * (interest * (Math.pow((interest + 1), numPayment)))) / (Math.pow((1 + interest), numPayment) - 1);
            //double totalPayment = ;
            //amountOwed.setText("Amount owed is: " + totalPayment);
            payment.setText("Monthly payment is: $" +  (int)Math.ceil(monthlyPayment));
        }
    });
    pane.add(hbox1, 1, 4);

    Button clearButton = new Button("Clear");
    HBox hbox = new HBox(10);
    hbox.setAlignment(Pos.BOTTOM_LEFT);
    hbox.getChildren().add(clearButton);
    pane.add(hbox, 1, 4);
    EventHandler<ActionEvent> handler1 = new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            totalField.setText("");
            percentField.setText("");
            timeField.setText("");
            payment.setText("");
        }
    };
    clearButton.setOnAction(handler1);
    primaryStage.setScene(scene);
    primaryStage.show();
}

}

1 Answer 1

7

mouseTransparent Issue

You put the "Calculate" button in a HBox and then you call setMouseTransparent(true) on the HBox. Such a call will disable mouse input on the HBox and all it's children. This behavior is documented in the linked Javadoc:

If true, this node (together with all its children) is completely transparent to mouse events. When choosing target for mouse event, nodes with mouseTransparent set to true and their subtrees won't be taken into account.

You don't need the setMouseTransparent(true) call; just remove it, and the "Calculate" button will be clickable as you expect.

calc

Overlapping Components Issue

You also need to ensure that you don't overlap some components with others otherwise only the top components will be clickable. In your case, the HBox containing the "Clear" button is overlapping the "Calculate" button.

So change:

pane.add(hbox, 1, 4);

To:

pane.add(clearButton, 1, 4);

Debugging Assistance

You can debug layouts either by calling layout.setStyle("-fx-background-color: green;") to show the extent of the layout, or by using ScenicView.

Sign up to request clarification or add additional context in comments.

3 Comments

I removed that setMouseTransparent, but for some reason the button still isn't clickable. the clearButton works fine, but calculate doesn't.
You second HBox (containing the clear button) is also over-lapping the Calculate button. After removing that and directly adding the clear button to your grid everything work fine. I attached a screenshot: OS X 10.9, Java8u60.
Thanks so much bro, works great. Thanks a lot for the debugging tips too, those are great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.