I'm a newbie in JavaFx. I wanted to create a custom progress bar using Canvas
and add it directly to my fxml file (instead of using java code).
This is my custom canvas class:
public class CircularProgressBar extends Canvas {
public CircularProgressBar() {
super();
draw(); // this call here doesn't work
}
public void draw() {
GraphicsContext gc = this.getGraphicsContext2D();
gc.setLineWidth(1.0);
gc.setStroke(Color.GREEN);
gc.setFill(Color.RED);
// draw progress bar
}
}
And the fxml file:
<?imports...?>
<GridPane fx:controller="com.mypackage.MainController">
<CircularProgressBar fx:id="progressBar"
width="500"
height="500"
GridPane.columnSpan="2"
GridPane.rowIndex="0"/>
</GridPane>
When the application starts, CircularProgressBar()
constructor (and therefore the draw()
method) is called but nothing is shown in the canvas. But if I call progressBar.draw()
manually in my application class it works correctly.
I want the canvas to automatically draw when the application starts. What should I do? Am I doing something wrong?