• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Set stage title from other class?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 387
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic