Adam Alayli
Adam Alayli

Reputation: 47

pane.getChildren().addAll(); not working in a scene javafx

This code will not allow the line to draw in my window... All I have in the fxml file is a simple pane with the fx:id of hi to test things out. There is no error, the line simply doesn't appear. I've also tried this with a box and circle. I really need help, this is an important project.

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.scene.Scene;
import javafx.scene.paint.Color;

public class PlotSceneController implements Initializable {

    @FXML
    Pane hi;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

    Line line = new Line(0,0,10,110);
        line.setStroke(Color.BLACK);
        line.setStrokeWidth(10);
        hi.getChildren().addAll(line);

    }

}

FXML File

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

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


<Pane fx:id="hi" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-
Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" 
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>

</children>
</Pane>

Main Class, leads to another page with a button that leads to the page I'm having trouble with.

public class Main extends Application {

Stage firstStage;
Scene loginButton;

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

    Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
    firstStage = primaryStage;                  
    loginButton = new Scene(root, 900, 700);    

    primaryStage.setTitle("Treatment Data");            
    primaryStage.setScene(loginButton);         
    primaryStage.show();                        
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {        //Main class
    launch(args);                       //Launches application/window
}

}

Upvotes: 2

Views: 2360

Answers (1)

Keyur Bhanderi
Keyur Bhanderi

Reputation: 1544

You are missed to set a controller class PlotSceneController.java. Set controller class in different two way like using main class setController() method or set controller class in left bottom side controller pane in scene Builder screen.

Using Main

FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));
loader.setController(new PlotSceneController());
Parent root = (Parent) loader.load();

Or Using FXML

Set Controller class with full package path like below way

enter image description here

Upvotes: 7

Related Questions