I have some code snipped out of a much bigger app, which renders some white text on a black background in a JavaFX WebView. The background colour of the page is set to transparent, using some code from Transparent background in the WebView in JavaFX
import java.lang.reflect.Field;
import org.w3c.dom.Document;
import com.sun.webkit.WebPage;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class TestWebView extends Application {
public void start(Stage stage) throws Exception {
StackPane stackpane = new StackPane();
Scene scene = new Scene(stackpane, stage.getWidth(), stage.getHeight(), Color.BLACK);
stage.setScene(scene);
scene.setFill(Color.BLACK);
stackpane.setStyle("-fx-background-color: BLACK");
WebView webview = new WebView();
stackpane.getChildren().add(webview);
WebEngine webengine = webview.getEngine();
webengine.documentProperty().addListener(new WebDocumentListener(webengine));
webengine.loadContent("<p style='color:white'>Hello World</p>");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
protected class WebDocumentListener implements ChangeListener<Document> {
private final WebEngine wdl_webEngine;
public WebDocumentListener(WebEngine webEngine) {
wdl_webEngine = webEngine;
}
#Override
public void changed(ObservableValue<? extends Document> arg0, Document arg1, Document arg2) {
try {
Field f = wdl_webEngine.getClass().getDeclaredField("page");
f.setAccessible(true);
com.sun.webkit.WebPage page = (WebPage) f.get(wdl_webEngine);
page.setBackgroundColor((new java.awt.Color(0, 0, 0, 0)).getRGB());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Testing on MacOS 10.11.6, with Oracle's JDK:
With JDK 1.8.0_152, this code works nicely - I get white text on black. (And the transparency works too when I layer things underneath it in the stackpane)
With JDK 9 (9+181), com.sun.webkit.WebPage is no longer accessible, so I have to compile and run it with --add-exports javafx.web/com.sun.webkit=ALL-UNNAMED - but having done that, I get black text on a black screen. I can tell the text is there by selecting the text and dragging it, which makes the text appear white while being dragged.
Ideally, I'd like to keep a single codebase that works for both JDK 8 and 9. (Java's usually been good to me with backward compatibility). Or as a second best, how do I get the white text I'm expecting in JDK 9?
Can anyone point me in the right direction? Many thanks in advance.
I had the same issue, I solved it by going further in the reflective process :
Field f = webEngine.getClass().getDeclaredField("page");
f.setAccessible(true);
Object page = f.get(webEngine);
Method m = page.getClass().getMethod("setBackgroundColor", int.class);
m.setAccessible(true);
m.invoke(page, (new java.awt.Color(0, 0, 0, 0)).getRGB());
Related
I am using ControlsFX - HiddenSidesPane where i add some link(ToggleButtons) to be clicked for navigation.
The problem i have is whenever anything is clicked, the HiddenSiddesPane hides.
The desired behavior is when anything inside it is clicked it should not close/hide, unless cursor hovers out of it.
SSCCE to demonstrate undesired behavior
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.controlsfx.control.HiddenSidesPane;
public class MyHiddenSidesPaneDemo extends Application{
public static void main(String[] args) { Application.launch(args); }
#Override
public void start(Stage primaryStage) throws Exception {
VBox root = new VBox();
TableView tv = new TableView();
HiddenSidesPane hiddenSidesPane = new HiddenSidesPane();
hiddenSidesPane.setContent(tv);
hiddenSidesPane.setLeft(new ListView());
root.getChildren().addAll(hiddenSidesPane);
primaryStage.setTitle("HiddenSidesPane Example Demo");
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Try the following:
ListView listView = new ListView();
hiddenSidesPane.setLeft(listView);
listView.setOnMouseEntered(e->hiddenSidesPane.setPinnedSide(Side.LEFT)); //Keep left side pinned
listView.setOnMouseExited(e->hiddenSidesPane.setPinnedSide(null)); //unpin when mouse exits
Is there any event handler present in Java FX, if i close a window directly bt pressing [X] button on Top right side.
Which events gets fire in this case ?
Nothing is working so far , neither setOnHiding not setOnCloseRequest()
Please help.
Try this one
import javafx.application.Application;
import javafx.event.EventHandler;
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.WindowEvent;
public class Main extends Application {
#Override
public void start(Stage stage) {
Text text = new Text("!");
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();
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
public void handle(WindowEvent we) {
System.out.println("Stage is closing");
}
});
stage.close();
}
public static void main(String[] args) {
launch(args);
}
}
Source Stage close event : Stage « JavaFX « Java
I want to maintain single background color(black) for all panes, and for all views. i don't want write css for every view. i am using only vbox and hbox mostly. and very few table views. is there any easy way to write css once and apply to all. thank you in advance
You don't write a css for every view, you give every element the same style class.
Pane pane = new Pane();
pane.getStyleClass().add("bg-black-style");
Somewhere you need to add the stylesheet to the scene
scene.getStylesheets().add("css-file.css");
And in the css file
.bg-black-style {
-fx-background-color: black;
}
This way every thing that should look the same has it's style all in one place.
You can just use .pane in CSS class, and it will work for all the panes.
.pane{
-fx-background-color: black;
}
Same works with .button etc.
You can apply the style sheet to the entire application like this:
package hacks;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import java.net.URL;
/**
* Created by BDay on 7/10/17.<br>
* <br>
* CssStyle sets the style for the entire project
*/
public class CssStyle extends Application {
private String yourCss = "YourResource.css";
public CssStyle() {
try {
Application.setUserAgentStylesheet(getCss()); //null sets default style
} catch (NullPointerException ex) {
System.out.println(yourCss + " resource not found");
}
}
private Button button = new Button("Button Text");
private TextArea textArea = new TextArea("you text here");
private ObservableList<String> listItems = FXCollections.observableArrayList("one", "two", "three");
private ListView listView = new ListView<String>(listItems);
private FlowPane root = new FlowPane(button, textArea, listView);
private Scene scene = new Scene(root);
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(scene);
primaryStage.show();
}
private String getCss() throws NullPointerException {
ClassLoader classLoader = getClass().getClassLoader();
URL resource = classLoader.getResource(yourCss);
String asString = resource.toExternalForm(); //throws null
return asString;
}
}
I'm making a JFrame that will load pictures from the Internet. I have that working, but the problem with this JFrame is that there are many pictures, and so they take quite a while to load. This is fine, but I would like to show the user that the pictures are loading. For some reason, I can't get the JPanel to display in the loading JFrame. I know this is a common error, and I've tried many fixes, but none of them work. Here is the code:
final JFrame weatherLoadPop=new JFrame("Loading weather...");
weatherLoadPop.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
weatherLoadPop.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
weatherPop.dispose();
};
});
weatherLoadPop.setResizable(false);
weatherLoadPop.setBounds(100,50,225,100);
JPanel weatherLoadPanel=new JPanel();
weatherLoadPanel.setBackground(Color.RED);
weatherLoadPanel.setPreferredSize(new Dimension(225,100));
JLabel weatherLoadLabel=new JLabel("Loading...0%");
weatherLoadPanel.add(weatherLoadLabel);
weatherLoadPop.add(weatherLoadPanel);
weatherLoadPop.pack();
weatherLoadPop.validate();
weatherLoadPop.setVisible(true);
I'm not sure I'm using pack() and validate() correctly. I don't use them often. In any case, removing them does not help. The strangest part of this problem, to me, is that the JFrame that loads the pictures works beautifully, while the much simpler loading JFrame doesn't.
Thanks for any help.
It's working fine here. Maybe you should provide an sscce that we can test?
I had to change your event listener to dispose weatherLoadPop instead of weatherPop and added your code into a test class:
package test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
final JFrame weatherLoadPop = new JFrame("Loading weather...");
weatherLoadPop.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
weatherLoadPop.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
weatherLoadPop.dispose();
}
;
});
weatherLoadPop.setResizable(false);
weatherLoadPop.setBounds(100, 50, 225, 100);
JPanel weatherLoadPanel = new JPanel();
weatherLoadPanel.setBackground(Color.RED);
weatherLoadPanel.setPreferredSize(new Dimension(225, 100));
JLabel weatherLoadLabel = new JLabel("Loading...0%");
weatherLoadPanel.add(weatherLoadLabel);
weatherLoadPop.add(weatherLoadPanel);
weatherLoadPop.pack();
weatherLoadPop.validate();
weatherLoadPop.setVisible(true);
}
}
and I'm getting:
using:
java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b20)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)
How to implement something kinda internal frame in JavaFx 2.0 specifically?
My attempt is as so..
import javafx.application.Application;
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.Stage;
public class Main extends Application {
ConnectDb connection;
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage stage) throws Exception {
final Stage stage1 = new Stage();
StackPane pane = new StackPane();
Button btn = new Button("Click Me");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
connection = new ConnectDb();
try {
connection.start(stage1);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Fire some thing..");
}
});
pane.getChildren().add(btn);
stage.setScene(new Scene(pane ,200, 300));
stage.show();
}
}
ConnectDb.java
import javafx.application.Application;
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.Stage;
public class ConnectDb extends Application {
#Override
public void start(Stage stage) throws Exception {
StackPane pane = new StackPane();
Button btn = new Button("Click On Button which is me");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Something here..");
}
});
pane.getChildren().add(btn);
stage.setScene(new Scene(pane ,200, 300));
stage.show();
}
}
First of all, for your approach, you don't really need to (and therefore should not) extend ConnectDb from Application as you just use the start method to create new stages. You just need one Application class (in your case Main). You could just as well create the new stage/scene in your first event handler.
Secondly, there is no real MDI support in JavaFX 2.1. Right now, you can just have multiple Stages (which is the equivalent to having multiple windows/frames). But you cannot have something like an internal frame in a desktop pane.
I guess you could take the following actions:
Just use multiple Stages (windows) with the drawback that they will float quite uninspiredly on your desktop
Use Swing as a container (with JDesktopPane and JInternalFrame) and integrate JavaFX (here's a nice How-To)
Implement your own framework that emulates MDI behavior
Find a framework that provides MDI behavior
Wait for a future release of JavaFX that hopefully provides MDI support (as far as I know, there's a change request pending...)
Create parent AncorPane.
Add several children AnchorPanes to it. They will serve as internal frames. Add different content to these.
Set children AnchorPanes invisible.
Add buttons to hide, resize or close children AnchorPanes. When needed, call function to set all children AnchorPanes invisible, except for one.