• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Modify the root node of subscene from different class

 
Ben Taylor
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have an issue regarding the subscene component.
It seems that I can not add children to the root node of the subscene if I call a method from another class, but if I call the method from within the class it works.
No errors appear in the code, therefor I am able to add children to the node but they will not show up in the subscene.
I use FXML, SceneBuilder and JavaFX 8.

My code is written below:

The main class:

//======================================
//Here I load the stage and the scene
package main;

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class main extends Application {

private AnchorPane pane;

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

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

try {
pane = FXMLLoader.load(getClass().getResource("/view/View.fxml"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();

}

}


//======================================
//The FXML class for the View.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.shape.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane fx:id="pane" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.ViewController">
<children>
<SubScene fx:id="sub1" fill="#d32222" height="333.0" layoutX="65.0" layoutY="55.0" width="385.0">
<root>
<Region />
</root>
</SubScene>
<Button fx:id="b1" layoutX="734.0" layoutY="21.0" mnemonicParsing="false" onAction="#addRect" text="Button" />
<Button fx:id="b2" layoutX="734.0" layoutY="71.0" mnemonicParsing="false" text="Button" />
</children>
</AnchorPane>



//======================================
//The ViewController class:

package view;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import auxx.Test;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.SubScene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;

public class ViewController {

@FXML
private ResourceBundle resources;

@FXML
private URL location;

@FXML
private Button b2;

@FXML
public SubScene sub1;

@FXML
private AnchorPane pane;

@FXML
private Button b1;

public Group sub1Group;

@FXML
void initialize() {
assert b2 != null : "fx:id=\"b2\" was not injected: check your FXML file 'View.fxml'.";
assert sub1 != null : "fx:id=\"sub1\" was not injected: check your FXML file 'View.fxml'.";
assert pane != null : "fx:id=\"pane\" was not injected: check your FXML file 'View.fxml'.";
assert b1 != null : "fx:id=\"b1\" was not injected: check your FXML file 'View.fxml'.";

sub1Group = new Group();
sub1Group.setId("sub1GroupID");

sub1.setRoot(sub1Group);
}


public Node getNode(String idNode) {

for (Node node : pane.getChildren()) {
String id = node.getId();
if (id.equals(idNode)) {
return node;
}
}
System.out.println("Not here");
return null;
}

//called by clicking a button in the View.fxml
@FXML
void addRect(ActionEvent event) {

Rectangle rect = new Rectangle(40, 40);
rect.setFill(Color.BLACK);

sub1Group.getChildren().add(rect);

Test test = new Test();
try {
test.addSomething();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void addCircle() {

Circle circle = new Circle(50, Color.PINK);
sub1Group.getChildren().add(circle);

System.out.println("The method was called");
}

}

//========================================
//here I have a class which has a method addSomething() which is supposed to call addCircle() from the ViewController, unfortunatelly nothing shows on the subscene and no error is thrown .
//If I call addCircle() in the viewController, the circle appears in the subscene. It is confusing for me because I am new in JavaFX and I used to work with jMonkeyEngine for 3D graphics and the issue is simple there, I was just supposed to call the group and add the object,
//but here it seems that it is not working that way.
//

package auxx;

import java.io.IOException;
import java.net.URL;

import javafx.fxml.FXMLLoader;
import javafx.scene.layout.AnchorPane;
import view.ViewController;

public class Test {

public void addSomething() throws IOException {

URL location = getClass().getResource("/view/View.fxml");
FXMLLoader fxmlLoader = new FXMLLoader(location);

AnchorPane root = (AnchorPane) fxmlLoader.load();
ViewController controller = (ViewController) fxmlLoader.getController();
controller.addCircle();
}

}



Unbenannt.png
[Thumbnail for Unbenannt.png]
 
Ben Taylor
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue was the fact that I created a new instance in Test class.

Any ideea how I can improve the code?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic