How to split JavaFX Nodes on Class files if Stage needs to be invoked from Main class?

I really like that I can call classes/objects from main method. This way I don’t have whole code in main method (that wouldn’t really feel like object oriented programming).

Now I have a simple code that draws a line using JavaFX. The line is a node, which is inside scene. But it’s all in the main method.

My main class is called Example. It contains whole code

I tried:

public static LineClass extends Example

Then I did put appropriate code there. Compiler didn’t let me compile it because launch() needs to be invoked from main method. I then did what compiler asked me to do, but it would just find more errors.

My code (when it was working):

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.Line; 
import javafx.stage.Stage;  

public class DrawingLine extends Application{ 
   @Override 
   public void start(Stage stage) { 
      //Creating a line object 
      Line line = new Line(); 

      //Setting the properties to a line 
      line.setStartX(100.0); 
      line.setStartY(150.0); 
      line.setEndX(500.0); 
      line.setEndY(150.0); 

      //Creating a Group 
      Group root = new Group(line); 

      //Creating a Scene 
      Scene scene = new Scene(root, 600, 300); 

      //Setting title to the scene 
      stage.setTitle("Sample application"); 

      //Adding the scene to the stage 
      stage.setScene(scene); 

      //Displaying the contents of a scene 
      stage.show(); 
   }      
   public static void main(String args[]){ 
      launch(args); 
   } 
} 

How to split code for more classes (assuming each class has its own .class file)? My aim is to have the JavaFX code (graphics) for Line object/node in different .class file instead of having all that code in main method to avoid mess. Thanks in advance.

Just create another class with the functionality you need, and instantiate it from start().

import javafx.scene.Group ;
import javafx.scene.Parent ;
import javafx.scene.shape.Line ;

public class LineClass {

    private final Group root ;

    public LineClass() {

        //Creating a line object 
        Line line = new Line(); 

        //Setting the properties to a line 
        line.setStartX(100.0); 
        line.setStartY(150.0); 
        line.setEndX(500.0); 
        line.setEndY(150.0); 

        root = new Group(line); 
    }

    public Parent getView() {
        return root ;
    }
}

and then

import javafx.application.Application ;
import javafx.scene.stage.Stage ;
import javafx.scene.Scene ; 

public class DrawingLine extends Application{ 
   @Override 
   public void start(Stage stage) { 

      LineClass lc = new LineClass();

      //Creating a Scene 
      Scene scene = new Scene(lc.getView(), 600, 300); 

      //Setting title to the scene 
      stage.setTitle("Sample application"); 

      //Adding the scene to the stage 
      stage.setScene(scene); 

      //Displaying the contents of a scene 
      stage.show(); 
   }      
   public static void main(String args[]){ 
      launch(args); 
   } 
}