Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialbehar
10,799 PointsJavaFX root.getChildren not working?
Probably to complicated a question for treehouse, but im wondering why i suddenly cant use the .getChildren() from the root element. Im basically trying to add a canvis to the root, and i would do it like i always add stuff to the root that is: root.getChildren().add(whatever), but since the root for whatever reason dosent have a getChildren() method i cant do that. If anyone out there knows a bit about java, help would be greatly appreciated!
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
// Create canvas (Thing that you can draw on)
Canvas canvas = new Canvas(300,275);
// Add canvas to root
root.getChildren.add(canvas) // FOR SOME REASON THIS DOES NOT WORK!
// Set Graphicscontect ie. the thing used to draw on a canvas
GraphicsContext gc = canvas.getGraphicsContext2D();
// Draw hello world and a globe
gc.setFill(Color.RED);
gc.setStroke(Color.BLACK);
gc.setLineWidth(2);
Font font = Font.font("Times New Roman", FontWeight.BOLD, 48);
gc.fillText("Hello World!", 60, 50);
gc.strokeText("Hello World", 60, 50);
// Show stage
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Caleb Kleveter
Treehouse Moderator 37,862 PointsCaleb Kleveter
Treehouse Moderator 37,862 PointsHey behar. I wish I could tell you what is wrong, but my Java is rusty and I've never used JavaFX, so I'm more lost then you ?. Hope you get it figured out!