Jonny Edmonds
Jonny Edmonds

Reputation: 29

JavaFx program Runs but nothing is blank, Why?

So I was super excited thinking I finally figured this program out and then BAM launching it = blank screen. Am I calling a blank Pane here, and the other info isnt populating? Is it populating but off screen? For some Reason JavaFX is really getting to me. If anyone also has any good places to go and read up on it. (I know google works, A lot of the sites/places I find just aren't doing it for me.

package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class Main extends Application {

Button button1, button2;
Label label1, label2;
Scene scene1, scene2;
BorderPane border1, border2;
Stage stage;  

ComboBox<String> choseImage;
ImageView mga1 = new ImageView(new 
Image(getClass().getResourceAsStream("/res/mga1.jpg")));
ImageView mga2 = new ImageView(new 
Image(getClass().getResourceAsStream("/res/mga2.jpg")));
ImageView mga3 = new ImageView(new 
Image(getClass().getResourceAsStream("/res/mga3.jpg")));

@Override
public void start(Stage primaryStage) throws Exception {

    primaryStage.setTitle("multi-factor key");
    primaryStage.setScene(scene1);

    //passPhrase
    TextField passPhrase = new TextField();
    button1 = new Button("submit");
    button1.setOnAction(e -> handleButtonAction(e));
    label1 =new Label ("Type in your PassPhrase");

    border1=new BorderPane();
    border1.getChildren().addAll(passPhrase, button1, label1);
    border1.setPadding(new Insets(20));
    BorderPane.setAlignment(passPhrase, Pos.CENTER);
    BorderPane.setAlignment(button1, Pos.BOTTOM_CENTER);
    BorderPane.setAlignment(label1, Pos.TOP_CENTER);
    scene1 = new Scene(border1, 350, 500);


    //combobox
    choseImage = new ComboBox<>();
    choseImage.setPromptText("Select a image.");
    choseImage.getItems().addAll("mga1.png"
    + "mga2.jpg"
        +   "mga3.png");

    choseImage.setValue("mga1.png");
    label2=new Label("Select a image");
    System.out.println(choseImage.getValue());
    String Selection = choseImage.getValue();
    if (Selection == "mga1.png")
            BorderPane.setAlignment(mga1, Pos.CENTER_RIGHT);
    if (Selection == "mga2.jpg")
            BorderPane.setAlignment(mga2, Pos.CENTER_RIGHT);
    if (Selection == "mga3.jpg");
            BorderPane.setAlignment(mga3, Pos.CENTER_RIGHT);



    border2= new BorderPane();
    border2.setPadding(new Insets(20));
    border2.getChildren().addAll(choseImage,label2);
    BorderPane.setAlignment(choseImage, Pos.CENTER);
    BorderPane.setAlignment(label2, Pos.TOP_CENTER);

    scene2 = new Scene(border2, 350, 500);
    primaryStage.show();




}

private void handleButtonAction(ActionEvent event) {
    if (event.getTarget()==button1)
        stage.setScene(scene1);
}




}

Upvotes: 0

Views: 1142

Answers (1)

Sunflame
Sunflame

Reputation: 3186

There are many problems why the screen is blank when you start the app

  1. When you call primaryStage.setScene(scene1); the scene is null, so it is expected to be blank, you should set the scene after you instantiate it. In your case after the line : scene1 = new Scene(border1, 350, 500);

  2. Even if you do the previous step the screen still remain blank because you call border1.getChildren().addAll(...) which is wrong. A BorderPane has 5 regions top bottom left right and center, so you should set a Node to this regions instead of adding them to the borderPane's children. It depends where you want to "see" them you should put in the right place. You should do something like: border1.setTop(...) border1.setBottom(...) and so on.

Upvotes: 3

Related Questions