I am new to Java FX and trying to build up an "easy" application which consists of a header button bar and a "content area" below.
I've managed a big part like that:
MainWindow.fxml with a Borderpane: MenuBar in the Top Area
And a Pane with fx:id: content in the center area.
Several X.fxml files for the content ()
One Controller which creates the obj content:
#FXML
private Pane content;
and switches the content:
content.getChildren().clear();
content.getChildren().add(FXMLLoader.load(getClass().getResource("Navi.fxml")));`
Main file which initializes the parent and scene:
Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
Stage Window = primaryStage;
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
Window.setTitle("ICIS - In Car Interactive System");
Window.setScene(scene);
Window.show();`
So far everything works fine!
But now I want to apply a SplitPane and do the "content change" in each side of the Split Pane (TwoWindows.fxml): So I've extended the
Controller with obj. for every Pane of the Split Pane, assigned the fx:id to that pane and want to control them analog to the example before.
#FXML
private Pane SecondWindow1;
SecondWindow1.getChildren().add(FXMLLoader.load(getClass().getResource("Navi.fxml")));
Well, during compilation everything is fine, but while running I get the Null error exception, so that I assume SecondWindow1 (left half of SlitPane) is not known to the controller.
I also assume, it is because I initialize at the beginning only MainWindow.fxml (which includes the content area) but not the TwoWindows.fxml (SplitPane) which inlcude the SecondWindow1 Object.
Well I've tried since hours now to solve it, but apparently I am overlooking sth. Somebody knows how to fix that problem? Do I need one Controller for every FXML File?
Related
My first intention was to set TextInputDialog icon. But I started from setting stage icon. I saw couple SO questions with marvelous answers containing usually 2 lines of code.
First I tried to put this icon to /resources/icons but exception "Invalid URL or resource not found" appeared. To be sure I don't make any mistake writing file path I moved this icon to /source/sample directory. I use code (I will post whole code):
public void start(Stage stage) throws Exception {
FXMLLoader loaderModyfikacjaKonfiguracji = new FXMLLoader(getClass().getResource("FXMLModyfikacjaKonfiguracji.fxml"));
Parent root = loaderModyfikacjaKonfiguracji.load();
stage.setTitle("Modyfikacja konfiguracji");
Image image = new Image("file:icon.png");
//stage.getIcons().removeAll();
stage.getIcons().add(image);
ControllerModyfikacjaKonfiguracji controllerModyfikacjaKonfiguracji = loaderModyfikacjaKonfiguracji.getController();
stage.setScene(new Scene(root, 510, 700));
stage.show();
}
Everywhere it looks so simple to set icon. I also tried .jpg. not using file: throws exception, using file: compiles but I see no effect of changed icon. What am I doing wrong or where is the problem?
I've successfully used this to set an icon before
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("AppIcon.png")));
In my case, the application fxml file and AppIcon.png are in the same directory.
If you dont want to go that route, i would suggest trying
Image image = new Image("file:./icon.png");
But that's a guess.
I made a similar question yesterday but I think it wasnt well explained, so I wanted to ask it again but with some changes I made in my code. I apologise if i write too much but i want to make everything understandable.
So, Im making a pokemon simulator where you can catch and train pokemon.
I have one main fxml file which contains buttons to access to the diferent fxml files like catch, battle, shop, bag...
Yesterday i was doing the bag where all your items will be stored.
Everything is working fine and the windows are switching between them properly. The problem comes when i was trying to add a Label to each item in the bag, which was suposed to show the user the cuantity he has of each item. So i created all the Labels with no text so they are empty.
They will get filled from the information i get from a database, this thing also works properly, i connect to the db and get the item cuantity. The problem comes when i want to show that item cuantity in my bag window.
Why? Because as you can imagine, i want that when you click on the bag button, the bag file loads with all the Labels filled with the cuantity of each item. The Labels are defined on the bag fxml controller, so if i want to fill them with some text, i cant do it from my main windows which uses another controller, i need to do it throught the bag controller.
This is the code i tried to make it work(Located in main controller):
#FXML
void mochila (ActionEvent event) throws IOException, SQLException {
AnchorPane pane = FXMLLoader.load(getClass().getResource("mochila.fxml"));
anchorPaneContent.getChildren().setAll(pane);
anchorPane2.setStyle("-fx-background-color: #3f3f3f;");
cm.getCantidad();
}
getCantidad is a function that i have in my bag controller and this is it:
public void getCantidad() {
lblpokeballCount.setText("Cantidad: "+pokeballcount);
lblsuperballCount.setText("Cantidad: "+superballcount);
lblultraballCount.setText("Cantidad: "+ultraballcount);
lblmasterballCount.setText("Cantidad: "+masterballcount);
}
So when i try to run this function from the main controller, it returns me null pointer exception. That means the labels are not initialized but when i type first AnchorPane pane = FXMLLoader.load(getClass().getResource("mochila.fxml"));Shoudlnt all the resources from the file be loaded?
Because i creaded a button in my bag file, when on clicked runs the same function, and it works correctly because im calling it from the same controller/file.
So now i dont really know what to do, this is a proyect for school but my programing teachers have never touched javafx so they dont even know what im doing. You are my only hope. I tried to understand this post: post
But i dont understand it at all as im new to all this stuff. So guys if you can help me i would be really gratefull, thanks!
edit:
#FXML
void mochila (ActionEvent event) throws IOException, SQLException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
anchorPaneContent.getChildren().setAll(loader);
controladorMochila controller = loader.<controladorMochila>getController();
controller.getCantidad();
}
anchorPaneContent is an anchorpane thats inside the main pane. All the buttons are in the main pane, and depending on the button you click, anchorpanecontent will change for another fxml file. I tried to do it like in the post i mentioned above. But i cant do anchorPaneContent.getChildren().setAll(loader); because it says: the node setAll is not aplicable for the arguments(FXMLLoader)
You are trying to add an FXMLLoader to an anchor pane, which will not work, since an FXMLLoader is not a visual component (it is a thing that loads FXML files).
Additionally, you are trying to get the controller from the FXMLLoader without actually loading the FXML file; this won't work because the controller class is specified in the FXML file (so the FXMLLoader doesn't know what kind of controller to create until it loads the file).
You need to load the FXML file and add the result to the anchor pane:
#FXML
void mochila (ActionEvent event) throws IOException, SQLException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
anchorPaneContent.getChildren().setAll(loader.load());
controladorMochila controller = loader.<controladorMochila>getController();
controller.getCantidad();
}
or, if you want to be a bit more explicit:
#FXML
void mochila (ActionEvent event) throws IOException, SQLException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
Parent pane = loader.load();
anchorPaneContent.getChildren().setAll(pane);
controladorMochila controller = loader.<controladorMochila>getController();
controller.getCantidad();
}
Trying to create a drop-shadow to my BorderPane. I need to use a StackPane to allow room for the drop-shadow. The problem is I cannot seem to set the background of StackPane to transparent. I am using transparent style for my primiaryStage.
There are other example using javaFX which work but, I can't figure it out when using fxml.
.StackPane{
-fx-background-color: transparent;
}
This still shows a white background behind my BorderPane
public class Main extends Application {
public static Stage Window;
#Override
public void start(Stage primaryStage) throws IOException{
Main.Window = primaryStage;
Window.initStyle(StageStyle.TRANSPARENT);
Window.setResizable(false);
FXMLLoader loader = new FXMLLoader(getClass().getResource("Homepage.fxml"));
Parent root = loader.load();
Window.setScene(new Scene(root));
root.getStylesheets().add(getClass().getResource("main.css").toExternalForm());
Window.show();
}
I found that when using a scroll pane it would dynamically create stack panes under it during runtime. Using scenic view I was able to see the style class name as 'viewport' and could change the background color that way.
Setting the window style might not work because the OS you are working on doesnt suppot that style. The documentation says about this:
On some platforms decorations might not be available.
But for a workaround for the stackpane, you should provide an example image of what you are aiming for. Right now its rather unclear what exactually you are trying to accomplish.
Code is at https://bpaste.net/show/b7aa0530f2ac (because of StackOverflow limitations)
I'm currently trying to use
//Menu.java
btn.setOnAction(event -> {
primaryStage.setScene(doubleclick);
});
to change scene from current scene to Scene doubleclick but it isn't found because it is in another class (Mouse.java). Also the variables in that class are needed for it to work. I've tried to copy over the code from Mouse.java to Menu.java but I don't know how to make that work.
So when I click button in the image above I see what is below:
...instead of the first content (main menu).
I wish to remove some of the control buttons from HTMLEditor, since I do not need them. for that I need to reach the desired node. How can I know the IDs of nodes inside HTMLEditor? Please see the following. Thank you!
public class myApp extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("myApp.fxml")); //this fxml has HTMLEditor named htmlEditor.
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
Node someControlInsideHtmlEditor = root.lookup("#htmlEditor").lookup("#what_Is_The_ID_of_This_someControlInsideHtmlEditor")
}
}
Download Scenic View from here
Add this to your application's class path
Add the following line to your start() method's end:
ScenicView.show(scene);
Run the application
Two windows will pop up: the primaryStage with the HTMLEditor and Scenic View's Stage
Now you can visit every node of the Scene Graph. Open the tree at the left pane, and select a Node from the HTMLEditor. You can access the controls by their CSS class.
For example, open HTMLEditor -> ToolBar -> HBox, and select the first Button. Look at "styleClass" in the "Node Details" at the right side. You will need "html-editor-cut". It can be used with this code:
Button cutButton = (Button) root.lookup(".html-editor-cut");
don't know if you're still looking for this answer. In Java 8, and HTMLEditor only has one child, which is a GridPane. The first two children of that are the ToolBars, the third is a WebView. Remove the first two children from the gridpane to do the formatting you want. Does that help?