TomJ
TomJ

Reputation: 1813

Write a javafx program without extending Application

I am developing a project. My project contains several classes in a single package. My main class extends Application to it. I am using NetBeans IDE to develop my project. Now I need to write other classes without extending Application to it. How can I do it? I am including the code of one of my classes below. Please suggest how to edit it.

package welcomepage;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class About extends Application {

    @Override
    public void start(Stage stage) {

        BorderPane border = new BorderPane();

        HBox hbox = addHBox();
        border.setTop(hbox);
        border.setCenter(addVBox());
        border.setBottom(addHBox1());

        Scene scene = new Scene(border,700,450);
        stage.setScene(scene);
        stage.setResizable(false);

    }

    private HBox addHBox() {

        HBox hbox = new HBox();
        hbox.setPadding(new Insets(15, 12, 15, 320));
        hbox.setSpacing(10); 
        hbox.setStyle("-fx-background-color: #336699;");

        Label lb1=new Label("ABOUT");
        lb1.setAlignment(Pos.CENTER);
        lb1.setFont(Font.font("Trebuchet MS",FontWeight.BOLD,20));

        hbox.getChildren().addAll(lb1);

        return hbox;
    }

    private VBox addVBox() {

        VBox vbox = new VBox();
        vbox.setPadding(new Insets(20));
        vbox.setSpacing(5);

        Label l1=new Label("C - MARK AND ATTENDANCE CALCULATOR");
        l1.setFont(Font.font("Calibri",FontWeight.BOLD,20));
        l1.setTextFill(Color.GREEN);

        Label l2=new Label("\nSoftware to calculate C-mark and attendance easily.\n"
                + "Supported in Windows XP or above.\n"
                + "Developed using Java.\n"
                + "Advantages : Simple user interface, Easy usage.\n\n"
                + "Developed by :\n"
                + "\t\t Adarsh P.S \n"
                + "\t\t Akhilnath A.R \n"
                + "\t\t Arjun P Das \n"
                + "\t\t Tomin Jacob ");
        l2.setFont(Font.font("Calibri",FontWeight.BOLD,18));
        l2.setTextFill(Color.GREEN);

        vbox.getChildren().addAll(l1,l2);
        return vbox;
    }

    private HBox addHBox1()
    {
       HBox hbox1 = new HBox();
       hbox1.setPadding(new Insets(15, 12, 15, 300));

       Button btn1=new Button("BACK");
       btn1.setFont(Font.font("Calibri",FontWeight.BOLD,18));
       btn1.setPrefSize(100,40);

       hbox1.getChildren().addAll(btn1);
        return hbox1;
    }

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

Upvotes: 2

Views: 2159

Answers (1)

brian
brian

Reputation: 10979

In the projects window, right click on the package where you want the new java class. A package looks like a little yellow box, usually all in lower case. Choose new then Java Class. It will ask for a name, use UpperCase like so for class names. A new tab will open with your new class. It won't extend Application and will use a default template.

To call this class (called NewClass) you do this in your main class.

//import newpackage.NewClass //only if it's not in same package. 

//somwhere in code you need to create the new class
NewClass newClass = new NewClass();

//now a button can call methods in newClass
final Button button = new Button("Butt");
button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                newClass.newMethod();
            }
        });

Upvotes: 1

Related Questions