Im making an kinda information like application for a city and I would like to use either multiple or a scene that gets updated. Is this possible and if so any tips?
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Informative Program");
primaryStage.setScene(new Scene(root, 1000, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The beauty of JavaFX is that you can add listeners to your application to dynamically change any part of your application, including the entire scene.
By using your example you can do
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
scene.setRoot(root);`
root = FXMLLoader.load(getClass().getResource("sample2.fxml"));
scene.setRoot(root2);
Essentially you just need to load a need root and then set it, and you should be good to go.
I use this personally for an Application I've been writing where the users log in first, and then it changes the root node to the next screen after login.
You could change your root at any point, or you could add/remove elements from the root, at any point, as well.
JavaFX is great in how Dynamic/Flexible it is as a language/Framework
Related
I want to be able to play sound from mp3 files for which I saw posts recommending the usage of JavaFX. I implemented the MediaPlayer and initialized the JFXPanel and in eclipse, everything works lovely.
Yet when I export to a runnable jar, and try running the program, I get the following error message: java.lang.NoClassDefFoundError: javafx/scene/media/MediaException.
I presume this is from the exclusion of JavaFX in the newer JRE versions (which I came across during my search to a solution). My main question is how do I ship the jar with JavaFX? Do I have to include a jar, and if yes, where do I get it? Because eclipse doesn't seem to package JavaFX into my runnable if I'm not mistaken.
Here an example which, for me, already triggers this behavior:
// This would throw a java.lang.NoClassDefFoundError for the JFXPanel but is effectively the same problem
public class Test extends Application
{
public static void main(String[] Args)
{
launch(Args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
StackPane root = new StackPane();
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
Thank you for your help!
JavaFx was removed into JDK> = 11 and now is a separate project opensurse [openjfx] (https://openjfx.io/).
And now it is to be made more difficult to create a version of the application javafx runnable everywhere, but it is a continuous evolution and I think that this is good documentation [doc-image-live] (https://openjfx.io/openjfx-docs/#modular).
I had a problem simile when I used for the developing the JDK 1.8 but in my version java system is openjdk11, I think this is the same case.
Your example is wrong because not is a JavaFX application.
The JavaFX application must extend the javafx.application.Application and in the main call the method launch, this method will call the method start inherited from Application.
This is a simple example of the Oracle
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
ps: When you speak the javafx, you must add the java version because we don't know your java version
An alternative path, if you prefer to not use JavaFX, would be to make use of the libraries that originated from JavaZOOM for the task of reading the mp3 files. I am seeing many offerings on github that have been derived from this source, for example, https://github.com/goxr3plus/java-sound-libraries But I have not made use of this particular library myself.
My preference has been to combine JavaFX for GUI with javax.audio.sampled, and a library I built that relies on java.sound.sampled.SourceDataLine for output. But I never bothered to implement reading mp3s. I tend to take the ogg/vorbis route when using compressed audio resources.
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.
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.
I have a javafx code like this
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/view/Login.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Welcom to MSS Login");
stage.setResizable(false);
stage.show();
}
Here I already setResizable(false); but I still can resize the window when run app. How to set a window not resizeable?
Thanks
I came across this problem today, trying to create some windows for my database management system and I clearly wanted fixed window size, non resizable, but unfortunately this is not fully supported by Ubuntu 16.04 LTS, which seems to have a different approach to how it performs with window size handlers.
However, I found the fix after none of stageStyle("TRANSPARENT"), setResizable(false) did not work.
The second one would normally work on Windows.
The fix for Ubuntu:
primaryStage.setMaxHeight(yourH);
primaryStage.setMinHeight(yourH);
primaryStage.setMaxWidth(yourW);
primaryStage.setMinWidth(yourW);
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?