Can I call classes in JavaFX that create objects?

0

I've been stuck for a couple of days, because I would like to know if the methods that I have in my main JavaFX class that create HBboxes, Panels, etc., could take them to an external class in order to have everything more organized, but the ways I've found that they only explain it for normal Java, a language in which I know how to do it. My problem is when I try it in JavaFX, that when doing the returns of the methods they give me errors of all kinds and I do not know how to solve them.

Does anyone know how to do it or have you had this problem before?

I edit following the advice of Balbu

This is the code of my Client class:

package cliente;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import builder.gridBuilder;

public class Cliente extends Application {

    public gridBuilder gB;

    @Override
    public void start(Stage primaryStage) {
        GridPane grid = gB.creaLogin();

        Scene scene = new Scene(grid, 700, 500);

        primaryStage.setTitle("Room For You");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

And this is the one in my gridBuilder class, which is in another package because everything is neater:

package builder;

import javafx.event.ActionEvent;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;

public class gridBuilder{

    public GridPane creaLogin(){
        GridPane grid = new GridPane();

        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(25));
        ImageView logo = new ImageView("imgs/logo.png");
        grid.add(logo, 0, 0, 2, 1);
        GridPane.setHalignment(logo, HPos.CENTER);
        GridPane.setMargin(logo, new Insets(0, 0, 20, 0));

        Label userName = new Label("Nombre:");
        grid.add(userName, 0, 1);
        TextField userTextFied = new TextField();
        grid.add(userTextFied, 1, 1);
        Label password = new Label("Contraseña:");
        grid.add(password, 0, 2);
        TextField passTextField = new TextField();
        grid.add(passTextField, 1, 2);

        Button btn = new Button("Iniciar Sesión");
        grid.add(btn, 1, 4);

        Text actionTarget = new Text();
        grid.add(actionTarget, 0, 6);
        GridPane.setColumnSpan(actionTarget, 2);
        GridPane.setHalignment(actionTarget, HPos.CENTER);

        btn.setOnAction((ActionEvent t) -> {
            actionTarget.setFill(Color.FIREBRICK);
            actionTarget.setText("Sign in button pressed");
        });

        return grid;
    }

}

I get an error when executing the Client by InvocationTargetException when calling gB.creaLogin ()

Does anyone know why?

    
asked by Kirilu 31.05.2018 в 11:49
source

2 answers

0

You can separate the button in this way:

public class MyButton extends Button {

    public MyButton() {
        Button button = new Button();
        button.setText("Say 'Hello World'");
        button.setOnAction((ActionEvent event) -> {
            System.out.println("Hello World!");
        });
    }
}

And so you would have a representative class of your own button. You can do the same with panels and have custom panels with the components you need.

You could also do it with a builder for all the types of buttons you have

public class ButtonBuilder {

    public Button crearBoton1() {
        Button button = new Button();
        button.setText("Say 'Hello World'");
        button.setOnAction((ActionEvent event) -> {
            System.out.println("Hello World!");
        });
        return button;
    }

    public Button crearBotonMuestraTexto(String text) {
        Button button = new Button();
        button.setText("Say '" + text + "'");
        button.setOnAction((ActionEvent event) -> {
            System.out.println(text);
        });
        return button;
    }
}

This you can always adapt to what you need, such as making static methods, parameterizing the ActionEvent so that the same method can create buttons with different functions, etc.

    
answered by 31.05.2018 / 12:12
source
0

Classes that create javafx graphical interfaces must extend from import javafx.application.Application . Now if you can pass it to another class, but you can not do this:

public gridBuilder gB;
    @Override
    public void start(Stage primaryStage) {
        GridPane grid = gB.creaLogin();

I wonder: At what time did you call the public gridBuilder gB constructor? That's why it thunders: gridBuilder gB is null so you can not call creaLogin .

You have to do this: public gridBuilder gB=new gridBuilder(); In addition all the classes that you are going to create begin with capital letters or the compiler will fill you with warnings

    
answered by 01.06.2018 в 17:30