How to use external css file to style a javafx application - java

i want to style my javafx application with an external css file but when i am adding css file, it is not creating any difference. i am using Netbeans 7.4 IDE and jdk8, although the code is not pointing any error or exception but i am not getting the required output. I am totaly confused what to do. My code is...
package manualstyle;
import java.io.File;
import java.net.URL;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
/**
*
* #author vickyjonnes
*/
public class ManualStyle extends Application {
#Override
public void start(Stage primaryStage) {
Group root=new Group();
Scene scene = new Scene(root, 300, 250);
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setLayoutX(100);
btn.setLayoutY(100);
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
root.getChildren().add(btn);
primaryStage.setScene(scene);
String css = ManualStyle.class.getResource("myStyle.css").toExternalForm();
scene.getStylesheets().add(css);
primaryStage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
I have a css file which resides in the same directory and the content of css file is :
.root{
-fx-background-color: #ff0066;
}

Please go through the following solution. Let me know, if you still face issues :
https://stackoverflow.com/a/22048338/1759128

Related

Why does FileChooser output errors in console? How do I supress them? [duplicate]

In running a Java program, using IntelliJ Community Edition 2021.2, I am seeing console output "java[934:22850] +[CATransaction synchronize] called within transaction" when a FileChooser dialog box is displayed. The dialog appears to work OK, but I'm puzzled about the message.
MacOS 13.0.1 (Ventura);
IntelliJ Community Edition 2021.2;
JDK 15.0.2;
JavaFX 15.0.1+1
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.*;
public class Main extends Application {
// ..................... and now it's time for the main routine .....................
#Override
public void start(Stage primaryStage) {
// we need a FileChooser
FileChooser myChooser = new FileChooser();
// set up a controlled exit
primaryStage.setOnCloseRequest(e -> {
e.consume();
primaryStage.close();
});
// define the main scene's building blocks
VBox root = new VBox(5); // the spacing is between each element in the VBox
Scene mainScene = new Scene(root, 650, 300);
// just one button in the scene
Button websiteSupportButton = new Button("Process file");
websiteSupportButton.setMinWidth(150);
root.getChildren().add(websiteSupportButton);
websiteSupportButton.setOnAction(e -> processFiles(primaryStage, myChooser));
// and now we're ready to associate the root Scene with the primary stage, and show it
primaryStage.setX(20);
primaryStage.setY(20);
primaryStage.setScene(mainScene);
primaryStage.setTitle("Error test");
primaryStage.show();
}
// .................................. tool scenes ..................................
// stuff moved from WebsiteSupport, and severely cut down - select an input file.
private static void processFiles(Stage myStage, FileChooser myChooser) {
File inputFile;
// need to select an input file - displaying this dialog causes multiple "java[1334:69043] +[CATransaction synchronize] called within transaction" messages
inputFile = myChooser.showOpenDialog(myStage);
if (inputFile != null) {
System.out.println(" file processed");
}
}
// .......................................... standard stuff ..........................................
public static void main(String[] args) {
launch(args);
}
}
I tried your code on:
OS X (intel) 13.0.1
JavaFX 19
OpenJDK 19.0.1
It received the same error message (multiple times for a single call):
java[34308:361725] +[CATransaction synchronize] called within transaction
I believe it occurs every time the file chooser showOpenDialog method is invoked in this configuration. A minimal example which replicates the issue on execution is:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class FileChooserCATransactionLogGeneratorApp extends Application {
#Override
public void start(Stage stage) {
stage.setScene(new Scene(new Group()));
stage.show();
FileChooser myChooser = new FileChooser();
myChooser.showOpenDialog(stage);
}
public static void main(String[] args) {
launch(args);
}
}
I advise filing a bug report.
If you do file a bug report and a bug id is generated, you can add a link to the bug report as a comment or an edit on this answer.

Stage styling in javafx

How to apply StageStyle.utility and StageStyle.undecorated to the same stage.
I am using this for the internal window/ popup.
Or if I have to follow any other solution please do stuggest.
Thanks
Your problem :
Application two styles StageStyle.UTILITYand StageStyle.UNDECORATED for the same stage.
Because
you asked for suggestion
to solve your problem.
Suggestion :
I suggest to use in-sideFX/Undecorator
(Downlaod the jar file and add it into your project) then we will make some modifications to keep your need ,I tried to create this sample of code for creating a Popup window inside parent window (Owner window) :
package javafxpopup;
import insidefx.undecorator.Undecorator;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
*
* #author Menai Ala Eddine
*/
public class Undecorated extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Click");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Stage popStage = new Stage();
popStage.initStyle(StageStyle.TRANSPARENT);
StackPane root = new StackPane();
root.getChildren().add(new Label("I'm popup window"));
applyModification(popStage, root);
popStage.show();
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root,500,500));
primaryStage.setTitle("Hello World!");
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
private void applyModification(Stage primaryStage, StackPane root) {
Undecorator undecorator = new Undecorator(primaryStage, root);
undecorator.getStylesheets().add("/skin/undecorator.css");
Scene scene = new Scene(undecorator, 300, 250);
primaryStage.setScene(scene);
scene.setFill(null);
Node stageMenu = undecorator.lookup("#StageMenu");
stageMenu.setVisible(false);
Node maximize = undecorator.lookup(".decoration-button-maximize");
maximize.setVisible(false);
Node manimize = undecorator.lookup(".decoration-button-minimize");
manimize.setVisible(false);
Node restore = undecorator.lookup(".decoration-button-fullscreen");
restore.setVisible(false);
}
}
By clicking the button the pop window will showing into owner window :
As you see StageStyle.UNDECORATED and StageStyle.UTILITY in the same time.
PS: it is a suggestion you can find many solutions.

Image won't show up in JavaFX [duplicate]

This question already has an answer here:
How to load images from URL in JavaFx if recieving HTTP Error 403 [duplicate]
(1 answer)
Closed 5 years ago.
I've been trying for a while now, following various documentations but I just cannot get any images to show up on JavaFX.
Here is my code:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
//stage and stuff
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
//images
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class KingsGame extends Application {
public static ImageView iv = new ImageView();
Button btn = new Button();
public static Image test = new Image("http://puu.sh/vOk8i/754c8fee68.png");
#Override
public void start(Stage primaryStage) {
//stackpane
StackPane root = new StackPane();
root.getChildren().add(iv);
//scene
Scene scene = new Scene(root, 1280, 720);
primaryStage.setTitle("test program lol");
primaryStage.setScene(scene);
primaryStage.show();
//---actual game---
drawMainMenu();
}
public void helloTest() {
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
}
public static void drawMainMenu() {
iv.setImage(test);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Whenever I run the program, all I get is this: http://puu.sh/vOko6/b669cfc20b.png
The strange thing is, when I initially tested this, I used this (https://osu.ppy.sh/ss/8062698) image link as my test image and it somehow worked even though there wasn't even a .jpg or .png extension. It was a game screenshot and I simply just used the link the game gave me to test. When I switched it to another test link, it just broke.
How can I get ALL image links to work?
It looks like an access problem.
If you print the exception returned with this instruction :
System.err.println(test.getException());
you get this :
java.io.IOException: Server returned HTTP response code: 403 for URL: http://puu.sh/vOk8i/754c8fee68.png
The site probably authorizes only the browser clients

Open PDF file with associated program in JavaFX

I want my java application such that if user chooses to click on a button the PDF opens using the default PDF reader that is installed in the computer.
The PDF which i want to be opened is present in same package "application".
The code which I am using is
package application;
import java.io.File;
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(final Stage primaryStage) {
Button btn = new Button();
btn.setText("Load PDF");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
File pdfFile = new File("computer_graphics_tutorial.pdf");
getHostServices().showDocument(pdfFile.toURI().toString());
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
If the PDF file is in the same package as the caller file (as you state), then
getHostServices().showDocument(getClass()
.getResource("computer_graphics_tutorial.pdf").toString());
should solve the problem.
The getResource method can be used really flexibly to locate files. Here is a small description how to use it: JavaFX resource handling: Load HTML files in WebView.

Html source in javafx HTMLEditor

Is there a way to use javaFx HTMLEditor in source mode, like when you select show source code in firefox?
I need this because I want to load strings which include xml tags in the editor and the wigdet is not showing them.
Not HTMLEditor nor Webengine or Webview contains a method like your needs. The only thing I've found is something to display the html in a different textarea. Maybe this will help you.
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;
public class JavaFXApplication1 extends Application {
#Override
public void start(Stage primaryStage) {
HTMLEditor editor = new HTMLEditor();
editor.setHtmlText("<html>"
+ "<head>"
+ "<title>A Test</title>"
+ "</head>"
+ "<body>This is just a Test</body>"
+ "</html>");
TextArea area = new TextArea();
area.setText(editor.getHtmlText());
editor.addEventHandler(EventType.ROOT, (Event event) -> {
area.setText(editor.getHtmlText());
});
VBox root = new VBox();
VBox.setVgrow(area, Priority.ALWAYS);
root.getChildren().addAll(editor, area);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

Categories