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?