0

I'm making a loan calculator for a beginner Java course. It will take input from two text fields ("loanAmount" and "loanYears") and, upon clicking a button "showTable", will generate and display a table of increasing interest rates, monthly payments and overall loan totals.

So far I've only begun constructing the program with a few of the required GUI elements. No logic has been implemented. However, I'm having trouble getting this simple stage to display any scene. In fact, the stage (window) never shows up at all. I've cross referenced my syntax and code structure with the guided exercises from our textbook, and everything seems to be in line so I'm confused what the problem could be. This project is using Java 1.7 if that matters.

import javafx.stage.Stage;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;

public class LoanCalculator extends Application{

    protected BorderPane getPane() {
        HBox paneInputs = new HBox(50);
        TextField loanAmount = new TextField();
        TextField loanYears = new TextField();
        Button showTable = new Button("Show Table");
        paneInputs.getChildren().addAll(loanAmount, loanYears, showTable);
        paneInputs.setAlignment(Pos.CENTER);
        paneInputs.setStyle("-fx-border-color: green");

        BorderPane pane = new BorderPane();
        pane.setTop(paneInputs);
        return pane;
    }

    @Override
    public void start(Stage primaryStage) { //Builds stage 
        Scene scene = new Scene(getPane(), 450, 200);
        primaryStage.setTitle("Loan Calculator (16.13)");
        primaryStage.setScene(scene);
        primaryStage.show();
}
vetos
  • 1
  • 1
  • What exactly happens when you run the application? Does your IDE show that its running but the window doesn't appear, or does it exit with an error? – tomdaly Nov 11 '19 at 13:14
  • The IDE does indicate "build successful" but the console shows `java.lang.UnsupportedClassVersionError: org/apache/tools/ant/launch/Launcher : Unsupported major.minor version 52.0` then about a dozen lines referring to different abstract classes that seem to be part of the default Java package. I would copy and paste the whole message but it by far exceeds the character limit of a comment here. – vetos Nov 11 '19 at 17:02
  • That might be the issue - `Unsupported major.minor version 52` means that the application is being run with Java version 8.0, and you said you're developing with Java 7. [This question](https://stackoverflow.com/questions/10382929/how-to-fix-java-lang-unsupportedclassversionerror-unsupported-major-minor-versi) might solve your problem. Make sure you're generating the class with `javac -target 1.7 LoanCalculator.java`, or try to the Java version in your IDE. – tomdaly Nov 11 '19 at 17:40

1 Answers1

0

To run a Java application, you need to have a main() method in at least one class that is called to start it. For a JavaFX application, the main() method should call Application.launch().

Try adding this function to the top of your class:

public static void main(String[] args) {
    Application.launch(args);
}

For more detail on why you need a main() method in a Java application, look here.

tomdaly
  • 467
  • 2
  • 11
  • This is not entirely true. For Java 8 through 11, the `java` command will recognize and launch an `Application` class without the need for a `static void main(String[])` method. – Stephen C Nov 11 '19 at 12:15
  • The @Override statement at the end is meant to override Application.launch with the "start" method following it. This is what we have been instructed to do and it has worked in all the previous exercises I mentioned. – vetos Nov 11 '19 at 12:20
  • @Stephen C Is this not true for Java 12 - 13 anymore? – mipa Nov 11 '19 at 12:21
  • @vetos - According to the Java 7 manual entry for `java`, you need a `main` method; see https://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html. Question: why are you using Java 7? It is **end of life**! Did your teachers tell you that you must use Java 7? – Stephen C Nov 11 '19 at 12:24
  • I'm using Java 7 since the past several units have been covering JavaFX concepts, which are deprecated in newer versions of Java. Our instructor had us using Eclipse and gave us directions on adding JavaFX support while using Java 12, but I couldn't get it working at all. I switched to NetBeans and just settled for using 7 for these units. All of this aside, I also used 7 for the other examples, which included no main methods, and they still worked. I don't understand how omitting a main method from one class would work just fine, while breaking another one? – vetos Nov 11 '19 at 12:31
  • I added a main method as written in @StephenC's first comment and it still is not running. – vetos Nov 11 '19 at 12:38
  • @mipa JavaFX is not longer supplied with JDK anymore, you need to install it separately. – Pavlus Nov 11 '19 at 14:04
  • @Pavlus Yes of course, but that is already true since Java 11. – mipa Nov 11 '19 at 14:29