Jump to content

Get an instance of a Scene (javafx)


Recommended Posts

Posted (edited)

Im trying to find a way so that in main i can declare a class that extends stage and call upon it like a jframe object.

 //I want something like this...
Class Gui extends Stage { ...}
 
 
Class Main{
 
public static void main(String[] args){
 Gui myGui =new Gui();
 
 Gui.start();
 
 String textboxField = Gui.getSomeRandomField();
 
 
}
}
 

At the moment im stuck with having all my code jammed into main and it is an eye soar every time I peek at the code

 

 

Goal is to create a controller to be able to interact with the gui class and my model class.

https://github.com/jaavant/ExcelFormatter

Edited by Qubit
Posted (edited)

Alright, so based on the stuff you've shown here, the best way of doing this that I can think of for you is using getters / setters in the Main class. The reason for this, is because I believe you shouldn't access the GUI everytime you need data, as its an independent object. However, if this approach doesnt suit you, you can always do a singleton.

 

Getter / Setter design:

public class GUI extends Stage {
    private Main instance;

    public GUI(Main instance){
        this.instance = instance;
    }


    // Can be any event, such as the start button being pressed.
    public void start(){
       instance.setTxtBox1(txtBox1.toString()) // whatever is in textbox 1, i forgot the statement to return that
    }

}

public class Main {
    
    private String txtBox1;

    public static void main(String[] args){
        new GUI(this);
    }


    public String getTxtBox1(){
        return txtBox1;
    }

    public void setTxtBox1(String str){
        txtBox1 = str;
    }
}
Edited by Tom

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...