Erwin Smith Erwin Smith - 3 years ago 94
Java Question

JavaFX, external class extends pane, adding that to main class doesn't work

JavaFX main class:

public class Test1 extends Application {

@Override
public void start(Stage stage) throws Exception {

Pane sp = new Pane();
SubClass sc = new SubClass();
sc.c.setFill(Color.AQUA);
sp.getChildren().add(sc);
Scene scene = new Scene(sp, 250, 250);
stage.setTitle("Testing");
stage.setScene(scene);
stage.show();
}


public static void main(String[] args) {
launch(args);
}

}


Here is my another class :

public class SubClass extends Pane
{
Circle c = new Circle(100.0f,100.0f,100.0f);
}


But I don't see my circle appearing anywhere in my output. My output is completely blank. Can somebody point at what am I doing wrong?

Here is my output windows:

enter image description here

Answer Source

All your subclass does it create the circle. It doesn't add it to a pane anywhere. To add it to the pane, you need to call getChildren().add(...) somewhere; for example in the constructor:

public class SubClass extends Pane {   

   Circle c = new Circle(100.0f,100.0f,100.0f);

   public SubClass() {
        getChildren().add(c);
   }

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download