This question already has an answer here:
JavaFX I want to save Chart-Image completely
(1 answer)
Closed 3 years ago.
I'm trying to get a snapshot of my "Java pop up window". The "Java window" looks like:
http://www.directupload.net/file/d/3616/kajnpeag_png.htm
However the snapshot looks like:
http://www.directupload.net/file/d/3616/cx47ga2o_png.htm
Sorry for the links, but I'm not allowed to post images yet.
So on the y-axis the autorange is missing and on the x-axis the dates are missing. Why didn't get my snapshot-function the whole picture? Do you have any suggestions/ideas how to solve this issue? For any help I would be grateful.
Code snapshot function:
public void saveAsPng(Scene scene) {
WritableImage image = scene.snapshot(null);
File file = new File("chart.png");
try {
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
} catch (IOException ex) {
Logger.getLogger(Server_Application.class.getName()).log(Level.SEVERE, null, ex);
}
}
The calling function:
#Override
public void start(Stage stage) throws Exception {
...
Parent root = FXMLLoader.load(getClass().getResource("MainPicture.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
Server_Application.serverApp.saveAsPng(scene);
...
}
I got the solution:
You have to deactivate the animation of the chart:
lineChart.setAnimated(false);
Related
I am completly lost atm. I have been working with scenebuilder and javaFX in the past but I am stuck like 5 hours now and I didnt get a step further. Let me explain:
I have a working java Eclipse Project, using maven dependencies
The Main is where I want to use JavaFX or load a fxml into
The programm takes many many VCC Files and extracts the data to put it all together in an excel
The programm works but I cant load a FXML file into the main or even show a pane in there
Now does my Java Main class has to extend Application? I tried both ways - doenst work.
Some example code:
public void start(Stage primaryStage) {
try {
bpmain = new BorderPane(FXMLLoader.load(new File("src\\fxml\\UserInterface.fxml").toURI().toURL()));
primaryStage.setScene(new Scene(bpmain));
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
or this (from original Docs)
public void start(Stage stage) {
Circle circ = new Circle(40, 40, 30);
Group root = new Group(circ);
Scene scene = new Scene(root, 400, 300);
stage.setTitle("My JavaFX Application");
stage.setScene(scene);
stage.show();
}
but this start method is just not getting called... where do I put that?
What my Programm should look like is pretty simple actually. I want a small UI Windows that lets you pick a Folder where the VCC data lives in and a OK Button that basically should run the Main method.
So a TextField that when its picked a Path in the Main gets replaced (filepath) and just a simple OK Button that says: yeah run the main - because the main works perfectly it is just that I cant show that ui and I dont know how to really connect it to the Main.java
Any help is appreciated - Ty
Option 1
public class Launch extends Application {
public static Stage stage = null;
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Main.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
this.stage = stage;
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Option 2:
public class SidebarController implements Initializable {
#Override
public void initialize(URL url, ResourceBundle rb) {
}
#FXML
void btnHome_OnMouseClicked(MouseEvent event) throws IOException {
BorderPane borderPane = (BorderPane) ((Node) event.getSource()).getScene().getRoot();
Parent sidebar = FXMLLoader.load(getClass().getResource("/fxml/ContentArea.fxml"));
borderPane.setCenter(sidebar);
}
}
I've started a class on Java, and we've gotten to JavaFX. I'm having trouble getting an image to display. After a little debugging, I was able to narrow it down to the following exception:
com.sun.javafx.iio.ImageStorageException: No loader for image data
I know the path name is correct, as that was the first error/exception I was able to fix. The current GUI shows up blank, as if I've put nothing in the scene. I can add other aspects (such as labels), but the image never loads.
public class Main extends Application{
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Image Belial = new Image("file:C:\\Users\\jakem\\Desktop\\D&D Stuff\\Belial.jpg");
System.out.println("Image loaded? " + !Belial.isError());
if (Belial.isError()) {
System.out.println(Belial.getException());
}
ImageView imageView = new ImageView(Belial);
HBox hbox = new HBox(imageView);
Scene scene = new Scene(hbox);
primaryStage.setScene(scene);
primaryStage.setTitle("Belial");
primaryStage.show();
}
These are the modules I've loaded, along with adding the JavaFX library to my project library.
--module-path ${PATH_TO_FX} --add-modules javafx.controls,javafx.fxml
I'm using Intellij.
EDIT: As it turns out, my image file was not actually in jpg format despite having a jpg extension. Switched to a properly formatted jpg and it worked fine.
Hi guys a little programmer java Swing and I'm trying a new technology like JavaFx just that I just can not figure out how to make operations for navigation between views, in particular.
How can I replace a main view on the stage? for example I have a view that I associate with the calback start method in the scene in the following way
#Override
public void start(Stage primaryStage) {
vistaPrincipale.load();
Scene scene = new Scene(vistaPrincipale.getRoot());
primaryStage.setScene(scene);
primaryStage.show();
}
and during the execution of the program I would like to change the view one with the view two, only that I could not really understand how I did not even find enough material to solve the problem, in swing it was enough to change the frame content bread
another problem that I have encountered is to launch a secondary panel like a jdialog in swing, I solved this problem by creating a new stage and using it in the following way, but to be honest it really seems like a very bad solution
public class InfoAutori {
private static final Logger LOGGER = LoggerFactory.getLogger(InfoAutori.class);
private Stage stage;
public void init(){
FXMLLoader load = new FXMLLoader();
Parent root = new AnchorPane();
try {
root = load.load(getClass().getResourceAsStream("InfoAutori.fxml"));
} catch (Exception e) {
LOGGER.error("Si e' verificato un errore del tipo: " +
e.getLocalizedMessage());
e.printStackTrace();
}
stage = new Stage();
Scene scene = new Scene(root);
stage.setScene(scene);
}
public void visualizza(){
stage.showAndWait();
}
}
I am very new to using JavaFX and have having some trouble using JavaFX with FXML. I am creating a program with a "Setup" button that when clicked, opens a new window with the connection (to an Arduino) settings. On the menu is another button ("Connect") that connects to the board. This closes the window. I'm looking to have this change the text of the original "setup" button to "disconnect", however, I don't seem to be able to access the button from the "setup window". Whenever I click "connect" I get the following error:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
I read online that this is a wrapper for a null pointer exception. I assume that the "setup" button is null and that's why I can't change it, but I can't work out why.
Here is an excerpt from MainController.java:
#FXML
protected void setUpConnection(ActionEvent e) {
SetupController setupController = new SetupController();
setupController.init(this);
}
The above method gets called when the "setup" button is clicked (set in the file: setupMenu.fxml). This then opens up the separate window. Here is the code in SetupController.java that opens the window:
private void openSetupWindow() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("setupMenu.fxml"));
Parent root1 = (Parent)loader.load();
Stage stage = new Stage();
stage.setTitle("Setup Connection");
stage.setScene(new Scene(root1));
stage.show();
} catch(Exception exc) {
exc.printStackTrace();
}
}
When the connect button is clicked, the following method (in SetupController.java) is called:
private void changeButtonText(ConnectionEventType e) {
Button b = main.getSetupButton();
if(e == ConnectionEventType.CONNECT) {
b.setText("Disconnect");
}
else {
b.setText("Setup Connection...");
}
}
(main is the MainController object that was passed in to setupController.init() )
The above code is where I am getting the error.
Just to clarify, I have 2 separate fxml files, one for the main window and one for the pop up. sample.fxml(the main window) has its controller set to MainController and is set up in Main.java (below):
#Override
public void start(Stage primaryStage) throws Exception{
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
GridPane root = loader.load();
Scene scene = new Scene(root, 1200, 900);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("Nest Control");
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
Am I trying to access the button incorrectly? Is anyone able to help? Like I said, I don't have much experience with using JavaFX or FXML.
I think the answer you are looking for is to store the controllers for each window you open so you can access the variables within the controllers, however without the rest of your code would be hard to advise you, but heres an example of what i mean:
private SetupController yourController;
#Override
public void start(Stage primaryStage) throws Exception{
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
GridPane root = loader.load();
this.yourController=loader.<SetupController>getController();
Scene scene = new Scene(root, 1200, 900);
}
}
You could then pass the variable yourController to other instances in a Model-view-controller type way and access its methods.
or something like this in your case :
private void changeButtonText(ConnectionEventType e) {
Button b = this.yourController.getButton(); //a method that returns your #FXML button object in your controller
if(e == ConnectionEventType.CONNECT) {
b.setText("Disconnect");
}
else {
b.setText("Setup Connection...");
}
}
Or alternatively have a specific method within the controller that will set the text of the button without having to return the button object.
See the examples here and here
However please note the error you get seems to typically attributed to missing #FXML annotations so maybe make sure in this instance that you have annotated all the variables in any controllers also. See here for more details.
I faced with the problem when I can't use the second css file by some evil reason.
I use IntelliJ IDEA 14 (build 139.222) Ultimate edition.
I use JavaFX 2.0 which built-in in IntelliJ IDEA 139.222 build
Board.css does not work. LoginForm.css works fine.
Also I've added the ;?*.css resource pattern into Compiler Section.
btn is the button which I click being at the Login Form.
Instead of GameApp class(it's the class which is inherited from Application class),
I used getClass() method and even some other classes. No result.
the css I tried to use in Board.css is .button:hover {-fx-background-color: white;}
}
Here's the code:
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Parent root = null;
try {
root = (Pane) FXMLLoader.load(GameApp.class.getResource("/sample/buttonGrid.fxml"));
root.getStylesheets().add(GameApp.class.getResource("/sample/Board.css").toExternalForm());
currentStage.setScene(new Scene(root));
currentStage.setResizable(false);
currentStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
#Override
public void start(Stage primaryStage) throws InterruptedException, IOException {
this.currentStage = primaryStage;
primaryStage.setTitle("Hello World");
initPane();
Scene scene = new Scene(grid, 698, 364);
scene.getStylesheets().add(GameApp.class.getResource("LoginForm.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
This one does not work too:
try {
Pane pane = (Pane) FXMLLoader.load(GameApp.class.getResource("/sample/buttonGrid.fxml"));
pane.getStylesheets().add("/sample/Board.css");
currentStage.setScene(new Scene(pane));
currentStage.setResizable(false);
currentStage.show();
}
Board.css is:
.button:hover {
-fx-background-color: white;
}
Earlier versions of JavaFX had some issues adding stylesheets to Parent nodes, instead of adding them to the Scene directly. The code in your action handler for the button adds the stylesheet to the node.
I would recommend making sure you are using a suitably recent version of the JDK and JavaFX; preferably Java 8 (which comes bundled with JavaFX 8). If you are forced to use Java 7, then make sure you have the most recent version, which is bundled with JavaFX 2.2. Simply updating your JDK version may solve the problem.
Also try adding the stylesheet directly to the scene, instead of to the root node:
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Parent root = null;
try {
root = (Pane) FXMLLoader.load(GameApp.class.getResource("/sample/buttonGrid.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(GameApp.class.getResource("/sample/Board.css").toExternalForm());
currentStage.setScene(scene);
currentStage.setResizable(false);
currentStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
});