0

I have a program, and I'm trying to use Event Handlers in JavaFX to make the Buttons do things, for example the button labeled Exit to end the program. I think my code is correct, but it isn't working. Any idea on what to do? Here's the code:

import edu.seminolestate.studentanalysis.ActionEvent; import edu.seminolestate.studentanalysis.EventHandler; import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.FlowPane; import javafx.scene.layout.GridPane; import javafx.stage.Stage; public class ProcessPayrollApplication extends Application { HourlyEmployee hE = new HourlyEmployee(newName, hoursWorked, hourlyRate); @Override public void start(Stage primaryStage) throws Exception { Label lblName = new Label("Employee name: "); Label lblHours = new Label("Hours worked: "); Label lblHourlyPay = new Label("Hourly pay rate: "); Label lblGrossPay = new Label("Gross pay:" + hE.computeGrossPay()); Label lblTaxes = new Label("Taxes: " + hE.computeTaxAmount()); Label lblNetPay = new Label("Net pay: " + hE.computeNetPay()); TextField txtName = new TextField(); TextField txtHours = new TextField(); TextField txtPay = new TextField(); Button btnCalculate = new Button ("Calculate"); Button btnClear = new Button ("Clear"); Button btnExit = new Button ("Exit"); BorderPane bp = new BorderPane(); GridPane gp = new GridPane(); gp.setVgap(15); gp.setHgap(15); FlowPane fp = new FlowPane(); fp.setHgap(10); fp.setAlignment(Pos.CENTER); gp.add(lblName, 0, 0); gp.add(lblHours, 0, 1); gp.add(lblHourlyPay, 0, 2); gp.add(lblGrossPay, 0, 3); gp.add(lblTaxes, 0, 4); gp.add(lblNetPay, 0, 5); gp.add(txtName, 1, 0, 2, 1); gp.add(txtHours, 1, 1, 2, 1); gp.add(txtPay, 1, 2, 2, 1); BorderPane.setAlignment(gp, Pos.CENTER); bp.setCenter(gp); BorderPane.setAlignment(fp, Pos.CENTER); bp.setBottom(fp); //Add event handlers btnExit.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.exit(0); } }); Scene scene = new Scene(bp, 350, 250); primaryStage.setScene(scene); primaryStage.show(); primaryStage.setTitle("Payroll"); } public static void main(String[] args) { Application.launch(args); } } 
4
  • You didn't add those buttons to the scene, do you see those buttons when you launch the application? About catching button click you are doing it right! Commented Apr 24, 2017 at 2:12
  • @Flood2d I added them to the FlowPane which is added to the BorderPane which is added to the scene. IS that not okay to do? Edit: Wait, I didn't add them... I'm dense. Commented Apr 24, 2017 at 2:13
  • It's ok, but in the code you have posted I don't see where you add them to the FlowPane, you create them, add the listener to btnExit but you don't add them to the FlowPane. If you can see them graphically maybe you omitted some part of the code? Commented Apr 24, 2017 at 2:16
  • @Flood2d You are right, i didn't add them, don't know how I missed that, thank you very much dude. Commented Apr 24, 2017 at 2:16

1 Answer 1

2

Replace

import edu.seminolestate.studentanalysis.ActionEvent; import edu.seminolestate.studentanalysis.EventHandler; 

with

import javafx.event.ActionEvent; import javafx.event.EventHandler; 

then do not forget to add the buttons to the pane.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.