Retreever
Retreever

Reputation: 23

How to incorporate If Else statement output in Java GUI?

I am struggling with finishing a project I've managed to get my the GUI up and running but I can't get the output to show on the GUI window once I push one of the buttons - right now when I push the button the output shows up on my terminal screen.

Screen Cap of GUI

When I push either of the buttons my output prints to the terminal screen rather than display at on the GUI screen - I'm not sure what I am doing wrong.
Screen Cap of Terminal Output when I push either of the buttons
Command window output

import java.text.NumberFormat;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.text.Text;
import javafx.scene.layout.FlowPane;
import javafx.geometry.Pos;
import javafx.event.ActionEvent;


    public class CarRentalDriverGui extends Application {

        private Button economyButton;
        private Button businessButton;
        private TextField capacityField;


        /**Specified number formatter for currency formatting on cost .*/
        NumberFormat formatter = NumberFormat.getCurrencyInstance();

            public void start (Stage primaryStage) {
                primaryStage.setTitle ("Vehicle Rental Contract Generator");

                Label fieldLabel = new Label ("Enter the capacity of the vehicle required then select Economy or Business button");
                capacityField = new TextField ();
                capacityField.setPrefWidth (50);
                capacityField.setOnAction(this::processRentalRequest);
                economyButton = new Button ("Economy Rental");
                economyButton.setOnAction
                (this::processRentalRequest);
                businessButton = new Button ("Business Rental");
                businessButton.setOnAction
                (this::processRentalRequest);
                FlowPane pane = new FlowPane(fieldLabel, capacityField, economyButton, businessButton);
                pane.setAlignment(Pos.CENTER);
                pane.setHgap (40);
                Scene scene = new Scene (pane, 600, 200);
                primaryStage.setScene (scene);
                primaryStage.show ();
                }

            /** Method for creating test rental contracts*/
            public void processRentalRequest (ActionEvent event) {
                int capacity = Integer.parseInt(capacityField.getText());
                //double economyResult = getDailyRentalCost();
                //double businessResult = getDailyRentalCost();     
                //create economy rental contract
                if (event.getSource() == economyButton) {

                    EconomyRentalContract Test1 = new EconomyRentalContract(capacity);
                    //economyResult.setText("Rate: "+ formatter.format(Test1.getDailyRentalCost()));
                    System.out.println ("Rate: "+ formatter.format(Test1.getDailyRentalCost()));
                    System.out.println ("Insurance Cost: "+ formatter.format(Test1.dailyInsuranceCost()));
                    System.out.println ("Total Cost: " + formatter.format(Test1.getDailyRentalCost() + Test1.dailyInsuranceCost()));
                    }
                    else {
                    //economyResult.setText("Rate: "+ formatter.format(Test2.getDailyRentalCost()));
                    BusinessRentalContract Test2 = new BusinessRentalContract(capacity);
                    System.out.println ("Rate: "+ formatter.format(Test2.getDailyRentalCost()));
                    System.out.println ("Rewards Points: " + Test2.rewardPointsEarned());
                    }
                    //System.out.println("Enter customer type \n1. Business \n2. Economy: ");
                     //customerType = userIn.nextInt();
            }
                    //System.out.print("Invalid number please enter 1 or 2! ");


                //counter.setText ("" + number);
                //Button convertButton = new Button ("Convert to Fahrenheit");
                //convertButton.setOnAction (this::processConvertRequest);
                //fahrenheitResult = new Text ("Welcome to my temperature converter!");
} //end class

Upvotes: 1

Views: 283

Answers (1)

GhostCat
GhostCat

Reputation: 140457

That is because your if/else code is printing to System.out

What exactly do you expect to happen then?

If you want to print something via your GUI application then you have to add a component to your panel/frame that is able to "print" text.

For example a textarea or a label. You could start with some static content - then look into the various ways how to change the content of such components and how to get your frame to redraw itself.

Upvotes: 1

Related Questions