• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Set stage title from other class?

 
Marcus Fernstrom
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to Java but having tons of fun.

I've run into an issue where I need to set the stage title from a class other than the one it is defined in.

I'm using Netbeans and Scene Builder to create FXML applications.

In the same class as main, I have this:
public void setTitle(String _theString) {
stage.setTitle(_theString);
}

Which I'm trying to access from another class, but when I do, I get non-static variable method setTitle(String) can not be referenced from a static context

The only thing static, is main, which I tried removing static from but no change.
 
John Damien Smith
Ranch Hand
Posts: 299
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your application has a start method to which the stage is provided.
Record the stage passed into the the start method in a static member and provide a static function to access the stage from another class.

For example:



Usually recording static data that is not just plain constants is often not really a good design, but in this case it is relatively OK, because the JavaFX application semantics guarantee that the Application will only ever be created once in the JVM, so you the recorded static stage will never be inadvertently overwritten by a new stage for a another application instance, because than can be no other application instance.

You might want to read up on static data and how it is used in a Java program:
https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic