1

Why am I unable to add Labels and TextFields using getChildren.add()? I have used this method before and I'm not sure what is causing it to fail in this project. I am trying to use MVC or MVP to create this, which I have had difficulty implementing in the past. MVP may be the source of my confusion:

GUI

//Error line 76. Error on vBox.getChildren.add(amountOfMoneyLbl); //File: GraphicalInterface.java

//this is the package that the view is created inside of
package com.mvc.view;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Label;
import java.awt.TextField;

//How to create multiple packages: https://www.youtube.com/watch?v=ZE6gdLZydDc
import com.mvc.model.Model;
//provides the control libraries for buttons, textfields, etc.
import javafx.scene.*;
import javafx.application.*;
import javafx.geometry.Insets;
//http://stackoverflow.com/questions/25222811/access-restriction-the-type-application-is-not-api-restriction-on-required-l
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.scene.layout.VBox;
//This file contains the View portion of the project Finance Calculator

public class GraphicalInterface extends BorderPane
{
    private final Model myModel;



    public GraphicalInterface(Model model) 
    {
        this.myModel = model;
        //this.createView();


    }

    @SuppressWarnings("restriction")
    public void createView(Stage primaryStage)
    {
        //Labels
        Label amountOfMoneyLbl = new Label("Amount of money in cents: ");
        Label aprLbl = new Label("APR: ");
        Label numPaymentsLbl = new Label("Number of payments: ");
        Label paymentLbl = new Label("Payment: ");

        //Text Fields
        TextField amountOfMoneyTextFld = new TextField("");
        TextField aprTextFld = new TextField("");
        TextField numPaymentsTextFld = new TextField("");
        TextField paymentTextFld = new TextField("");

        //Button
        Button calcBtn = new Button("Calculate");


        BorderPane myBorderPane = new BorderPane();

        myBorderPane.setVisible(true);


        Scene scene = new Scene(myBorderPane, 600, 400);

        //SET SIZE OF SCENE
        //parameters on color are r, g, b, float opacity?
        primaryStage.setScene(scene);
        primaryStage.setTitle("Finance calculator");
        primaryStage.show();

        VBox vBox = new VBox(15);
        vBox.setVisible(true);
        vBox.setPadding(new Insets(15, 15, 15, 15));      
        vBox.setStyle("-fx-background-color:black");

        //THIS SHOULD WORK, BUT DOESN'T - WHO ARE THE CHILDREN?
        vBox.getChildren.add(amountOfMoneyLbl);

        myBorderPane.setCenter(vBox);


        //myBorderPane.getChildren().addAll();
        //myBorderPane.getChildren().add(calcBtn);
//      
//      this.add(amountOfMoneyLbl);
//      this.add(amountOfMoneyTextFld);
//      
//      this.add(aprLbl);
//      this.add(aprTextFld);
//      
//      this.add(numPaymentsLbl);
//      this.add(numPaymentsTextFld);
//      
//      this.add(paymentLbl);
//      this.add(paymentTextFld);
//      
//      this.add(calcBtn);

        //set prompt for user input in the first textfield
//      amountOfMoneyTextFld.setPromptText();

        //bind fields to model here.....page 429

    }

    public void bindFieldsToModel()
    {

    }


}
///////////////////////////////////////////////////////////////////
// File: myApp.java
///////////////////////////////////////////////////////////////////
package com.mvc.model;

import com.mvc.view.GraphicalInterface;
import com.mvc.view.Presenter;
import com.mvc.model.Model;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import javafx.scene.layout.VBox;

public class myApp extends Application{

    public static void main(String[] args)
    {
        //this method is from inside the Application class.
        //It sets up your program as a javaFx Application, then calls start().

        // Application.launch() -> Application.start()

        Application.launch(args);

    }

    //The primaryStage is the main window.
    @Override
    public void start(Stage primaryStage) throws Exception {


        Model model = new Model();


        //The window is called the stage. The stuff inside the 
        //window is called the scene.

        //This view object creates the stage and the scene.

        GraphicalInterface view = new GraphicalInterface(model);

        view.createView(primaryStage);

        //create scene first because the presented uses the scene to listen
        //Scene scene = new Scene(view);

        Presenter p = new Presenter(model, view);

    }

}
heather
  • 113
  • 1
  • 12

1 Answers1

0

You have (at least) two errors in your code.

Instead of

        vBox.getChildren.add(amountOfMoneyLbl);

it should be

        vBox.getChildren().add(amountOfMoneyLbl);

and you have imported the wrong (AWT) label class for amountOfMoneyLbl.

mipa
  • 10,369
  • 2
  • 16
  • 35
  • Thank you soooooo much, mipa! I kept re-writing it, but did not figure out that I used the wrong import. Your the best! – heather Apr 15 '17 at 18:17