How can I draw a single Axis to a pane?
It gives me a runtime exception if I naively try addAll(axis)
I am browsing through the XYChart source for answers on how it draws axis.
EDIT:
Source
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.NumberAxis;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class TestSingleAxis extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
StackPane root = new StackPane();
root.getChildren().addAll(new NumberAxis(1, 20, 1));
Scene scene = new Scene(root, 1300, 800, Color.WHITESMOKE);
stage.setScene(scene);
stage.show();
}
}
Results
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:548)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:48)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:134)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
at javafx.scene.chart.Axis.layoutChildren(Axis.java:744)
at javafx.scene.chart.ValueAxis.layoutChildren(ValueAxis.java:359)
at javafx.scene.Parent.layout(Parent.java:1014)
at javafx.scene.Parent.layout(Parent.java:1022)
at javafx.scene.Scene.layoutDirtyRoots(Scene.java:537)
at javafx.scene.Scene.doLayoutPass(Scene.java:508)
at javafx.scene.Scene.preferredSize(Scene.java:1467)
at javafx.scene.Scene.impl_preferredSize(Scene.java:1494)
at javafx.stage.Window$10.invalidated(Window.java:720)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:106)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:140)
at javafx.stage.Window.setShowing(Window.java:783)
at javafx.stage.Window.show(Window.java:798)
at javafx.stage.Stage.show(Stage.java:242)
at guifx.TestSingleAxis.start(TestSingleAxis.java:32)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:491)
at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:260)
at com.sun.javafx.application.PlatformImpl$5$1.run(PlatformImpl.java:223)
at com.sun.javafx.application.PlatformImpl$5$1.run(PlatformImpl.java:220)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:220)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
Process finished with exit code 1
Observing some version of code, near the line 744 I see:
if (getSide().equals(Side.LEFT)) {
And this code says me :
#Override
public void start(Stage primaryStage) {
System.out.println(new NumberAxis(1, 2, 3).getSide());
}
>>null
Seems, you need to specify the side anyway..
Does it help?
Related
This question already has an answer here:
App hangs up or "Not on FX application thread" occurs during app activity
(1 answer)
Closed 7 years ago.
Help me to realize the Line Chart and how to do better.
I need to add a Line Chart 2-3 The schedule of 30 - the values reach from the database. I wanted to make a separate stream, but there are errors.
ERROR : 'Exception in thread "Thread-4"
java.lang.IllegalStateException: Not on FX application thread;
currentThread = Thread-4''
import extfx.scene.chart.DateAxis;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.util.Date;
import java.util.GregorianCalendar;
public class main extends Application{
public static void main(String[] args) {
launch(args);
}
public static LineChart<Date, Number> lineChart;
public static Thread threadInTrendFlow;
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("-----------------------");
NumberAxis numberAxis = new NumberAxis();
DateAxis dateAxis = new DateAxis();
lineChart = new LineChart<Date, Number>(dateAxis,numberAxis);
lineChart.getData().add(scFlow.series);
BorderPane pane = new BorderPane();
pane.setCenter(lineChart);
///
Button inLineChart = new Button("insert");
inLineChart.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Task<Void> loaded = new scFlow();
threadInTrendFlow = new Thread(loaded);
threadInTrendFlow.start();
}
});
pane.setTop(inLineChart);
Scene scene = new Scene(pane,800,600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Use Platform.runlater(()->{}) when invoking stuff that goes on the application thread.
There are plenty of threads related to somewhat similar problems. Just search multithreading javafx.
I want to draw red borders around arbitrary javafx.scene.text.Text nodes in my JavaFX scenegraph, for example those in a Button object.
It is easy to retrieve all the Text nodes but not to find where they are in the scene, they have an x and y property which doesn't seem to get set properly but they do not have a width and height.
So far I have tried to add rectangles with a red stroke to a stack pane but the x and y are always wrong and I can't get the size.
One solution is to wrap the text nodes in layout pane (such as an HBox) and use CSS on the layout pane:
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class TextBorderExample extends Application {
#Override
public void start(Stage primaryStage) {
final HBox root = new HBox(5);
root.getChildren().addAll(
new Text("This"), new Text("Is"), new Text("A"), createBorderedText("Red"), new Text("Bordered"), new Text("Text")
);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
private Node createBorderedText(String text) {
final HBox hbox = new HBox();
hbox.getChildren().add(new Text(text));
hbox.setStyle("-fx-border-color: red;");
return hbox ;
}
public static void main(String[] args) {
launch(args);
}
}
Another way is to use a Rectangle, as follows:
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class TextBorderExample extends Application {
#Override
public void start(Stage primaryStage) {
final HBox root = new HBox(5);
final Text red = new Text("Red");
final Rectangle redBorder = new Rectangle(0, 0, Color.TRANSPARENT);
redBorder.setStroke(Color.RED);
redBorder.setManaged(false);
red.boundsInParentProperty().addListener(new ChangeListener<Bounds>() {
#Override
public void changed(ObservableValue<? extends Bounds> observable,
Bounds oldValue, Bounds newValue) {
redBorder.setLayoutX(red.getBoundsInParent().getMinX());
redBorder.setLayoutY(red.getBoundsInParent().getMinY());
redBorder.setWidth(red.getBoundsInParent().getWidth());
redBorder.setHeight(red.getBoundsInParent().getHeight());
}
});
root.getChildren().addAll(new Text("This"), new Text("Is"), new Text("A"), red, new Text("Bordered"), new Text("Text"));
root.getChildren().add(redBorder);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I'm new to working with javaFX and recently ran into the following error when running the application through Eclipse.
My code:
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class WindowBetaV02 extends AbstractWindow{
public static void main(String[] args){
launch(args);
}
public void start(Stage mainStage){
height = 480;
width = 640;
mainStage = new Stage();
mainStage.setTitle("AMQ");
mainStage.setScene(drawGUIGameNormal());
mainStage.setResizable(false);
mainStage.show();
}
private Scene drawGUIGameNormal(){
//Draw gui
}
}
import javafx.application.Application;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;
public abstract class AbstractWindow extends Application {
protected int height;
protected int width;
protected GameFunctions controller;
public abstract void start(Stage stage);
protected GridPane createPlayerPane(int width, int height){
GridPane playerPane = createPixelGridPane(width, height);
playerPane.getStyleClass().add("playerPane");
playerPane.add(createPlayerTable(), 10, 10, width-10, width-10);
return playerPane;
}
#SuppressWarnings({ "rawtypes", "unchecked" })
protected TableView createPlayerTable(){
TableView table = new TableView();
TableColumn points = new TableColumn("Points");
points.setMaxWidth(30);
points.setMinWidth(30);
points.setCellFactory(new PropertyValueFactory<Players, Integer>("points"));
TableColumn player = new TableColumn("Player");
player.setMaxWidth(100);
player.setMinWidth(100);
player.setCellFactory(new PropertyValueFactory<Players, String>("playerName"));
table.setItems(controller.getPlayers());
table.getColumns().addAll(player, points);
return table;
}
protected GridPane createPixelGridPane(int width, int height){
GridPane pane = new GridPane();
for(int i = 0; i < width; i++){
pane.getColumnConstraints().add(new ColumnConstraints(1));
}
for(int i = 0; i < height; i++){
pane.getRowConstraints().add(new RowConstraints(1));
}
return pane;
}
}
The error
Exception in thread "main" java.lang.RuntimeException: Application launch error
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:122)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.UnsatisfiedLinkError: com.sun.glass.ui.win.WinApplication._invokeLater(Ljava/lang/Runnable;)V
at com.sun.glass.ui.win.WinApplication._invokeLater(Native Method)
at com.sun.glass.ui.Application.invokeLater(Application.java:338)
at com.sun.javafx.tk.quantum.QuantumToolkit.defer(QuantumToolkit.java:620)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:173)
at com.sun.javafx.application.PlatformImpl.runAndWait(PlatformImpl.java:212)
at com.sun.javafx.application.PlatformImpl.tkExit(PlatformImpl.java:320)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:421)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
... 1 more
Anyone know how to fix this error?
-Edit-
Added the code for the AbstractWindow class
You are in JavaFx, your Main class have to extends Application and not Window
Need to show an example in which you show and hide a window in JavaFX 2.
A stage is a window in javafx-2. It provide the hide and show methods:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class HideShowApp extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
final Stage window = new Stage();
window.setX(10);
Scene innerScene = new Scene(new Label("inner window"));
window.setScene(innerScene);
HBox root = new HBox(10d);
Button showButton = new Button("show");
showButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
window.show();
}
});
Button hideButton = new Button("hide");
hideButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
window.hide();
}
});
root.getChildren().add(showButton);
root.getChildren().add(hideButton);
stage.setScene(new Scene(root));
stage.show();
}
}
I want to create a borderpane what is transparent.I tried setting background color to transparent but it appears white. plz let me know if there is a way.
Code i tried.
BorderPane root=new BorderPane();
root.setStyle("-fx-background-color:transparent");
Scene scene=new Scene(root);
stage.setScene(scene);
stage.show();
Thank you...
Try setting the stage to transparent:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class TransparentStage extends Application {
#Override
public void start(Stage stage) {
// important line
stage.initStyle(StageStyle.TRANSPARENT);
Text text = new Text("Transparent!");
text.setFont(new Font(40));
VBox box = new VBox();
box.getChildren().add(text);
final Scene scene = new Scene(box,300, 250);
scene.setFill(null);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}