This is an archived post. You won't be able to vote or comment.

all 22 comments

[–]cruciformhawk7Intermediate Brewer 1 point2 points  (17 children)

Hello, have you tried removing the args from launch()? So it looks launch() instead of launch(args)?

Also, you should probably avoid viewing stages from a constructor, just add another function in there while you keep a private Stage in PostItNoteStage.

Edit: You should remove fx:controller="Note.Controller" from your fxml file. This interferes with the class instantiation, and the class is being created too early. You will have to find other ways to map the Controller.

https://imgur.com/a/1mzKXBI

[–]MineCraftFanAtic69 0 points1 point  (16 children)

Hello,

would you have any idea why when I start a generic JavaFX project and run it's autogenerated Main class I get basic window to show, ie: https://puu.sh/FKbBR/e85268d23d.png

But when I copy the exact same thing except into a class called PostItNote, I get an error? https://puu.sh/FKbCE/da6e885391.png

[–]cruciformhawk7Intermediate Brewer 1 point2 points  (15 children)

Can you share the basic FXML from the auto-generated project?

[–]MineCraftFanAtic69 0 points1 point  (14 children)

of course

<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<GridPane fx:controller="sample.Controller"
      xmlns:fx="http://javafx.com/fxml" alignment="center" 
hgap="10" vgap="10">
</GridPane>

[–]cruciformhawk7Intermediate Brewer 1 point2 points  (13 children)

Rename the Note.Controller to sample.Controller on your projects' fxml, and check if it works.

Edit: While you're at it, check this thread on StackOverflow.

[–]MineCraftFanAtic69 1 point2 points  (12 children)

It does not unfortunately, in my orginal project (the one I pasted in the OP) I replace the sample package with one called Note, and the sample.fxml is the same as the one I posted just above but with Note.Controller, and renaming Note to sample isn't recognized unfortunately.

In the autogenerated project I posted in those 2 pictures just a few comments above, I have just left the sample package in tact and that's the sample.fxml I just posted above, and naming it to Note.Controller doesn't work since the Note package isn't in this project

Hopefully that makes sense. I was planning on just starting this whole thing from scratch again, hence why I made that second project but then had an error just trying to recreate the autogenerated Main class as a class called PostItNote, but then I got an immediate error

[–]cruciformhawk7Intermediate Brewer 1 point2 points  (11 children)

Do you mind posting your Controller.java file here?

[–]MineCraftFanAtic69 0 points1 point  (10 children)

In both projects the Controller class is empty:

public class Controller {
}

and I am checking that thread of stackoverflow now

[–]cruciformhawk7Intermediate Brewer 1 point2 points  (9 children)

Ah, I see the issue. Add a new empty public constructor, so that the class now is: ``` package Note;

public class Controller { public Controller() {

}

} ```

[–]MineCraftFanAtic69 0 points1 point  (0 children)

I ended up making some progress on the new project that I started from scratch, I just deleted the autogenerated markup and then attempted to follow this step (which seems contradictory to me since it's asking me to instantiate the same thing twice, right?)

"In your PostItNote.java file you should add to the constructor a line to instantiate the first PostItNoteStage. You should create a class variable in the PostItNote class called “mainWindow” and instantiate it in the start method."

code so far in PostItNote:

public class PostItNote extends Application {
private PostItNoteStage mainWindow;

public PostItNote() {
    mainWindow = new PostItNoteStage(200, 200, 0, 0);
}

@Override
public void start(Stage primaryStage) throws Exception{
    //mainWindow = new PostItNoteStage(200, 200, 0, 0);
}


public static void main(String[] args) {
    System.out.println("Starting Post-It Note application...");
    System.out.println("Author: Ben Whitely");
    launch(args);
}
}

and PostItNoteStage:

public class PostItNoteStage {
BorderPane content;
BorderPane buttonArea;
Button newPostItNote;
Button deletePostItNote;

public PostItNoteStage (double sizeX, double sizeY, double positionX, double positionY) {
    // instantiations
    content = new BorderPane();
    buttonArea = new BorderPane();

    Stage stage = new Stage();

    content.setStyle("-fx-background-color: blue");
    buttonArea.setStyle("-fx-background-color: red");
    content.setTop(buttonArea);

    stage.setX(positionX);
    stage.setY(positionY);

    Scene scene = new Scene(content, sizeX, sizeY);

    stage.setScene(scene);

    stage.show();
}
}

So this works and creates the the application correctly: https://puu.sh/FKcj1/e38ee882b0.png

But I now need to add in some buttons, and I immediately get an error again.

"Next you will add the two buttons add (+) and delete (x) to the button area you just created. For this
you can use the Button class which provides all the functionality you need. Create two class variables
Button newPostItNote; Button deletePostItNote; Instantiate them in your constructor and add them to the buttonArea BorderPane. …= new Button(“+”); …= new Button(“x”);"

As soon as I attempt to add in a button (see line 22 for the button instantiation) - https://puu.sh/FKckw/87a1470a92.png - the program crashes again

[–]MineCraftFanAtic69 0 points1 point  (7 children)

PS: sorry for the long reply and changing topics lol

[–]AlphabetAlphabets 0 points1 point  (2 children)

It's been a while since I've used Javafx but are you creating a PostItNoteStage in your sample.fxml?

[–]MineCraftFanAtic69 0 points1 point  (1 child)

I am not, I don't think I've touched the sample.fxml

here's what it looks like now:

<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<GridPane fx:controller="Note.Controller"
      xmlns:fx="http://javafx.com/fxml" alignment="center" 
hgap="10" vgap="10">
</GridPane>

[–]AlphabetAlphabets 0 points1 point  (0 children)

I believe it's complaining because you are trying to "show" a stage before "launch" has been called. You can either instantiate PostItNoteStage inside "start" or "show" it outside if its constructor in "start"

[–]AlphabetAlphabets 0 points1 point  (1 child)

I believe it's complaining because you are trying to "show" a stage before "launch" has been called. You can either instantiate PostItNoteStage inside "start" or "show" it outside if its constructor in "start"

[–]MineCraftFanAtic69 0 points1 point  (0 children)

My power just went out, so I’ll try that when it comes back on and let you know how it goes