Problem
I have a decent sized javaFX window that appears to work perfectly well, until i resize any window in the application. After the resize all components that have a drop-down or similar action (ie. Menu, ComboBox, TabPane) become horribly slow.
Cause
I have narrowed the problem down to a progress bar, if i remove the progress bar from the scene i can resize the window(s) as much as i want, if i add it then any window resize and they start to become unresponsive for about half a second; sometimes as much as two seconds if i do a lot of resizing.
The window
A view of the window so that you can see all the components.
I can't add all the window code as its simply way too much to post.
Class with the progress bar
/**
* The class that holds and displays the progress bar
*/
public class BottomToolBarImpl extends ToolBar {
/**
* The label that display the "Waiting for input" text at the bottom of the
* window
*
* The {#code LLabel()} class is a label that gets its text from a
* properties file
*/
private final Label text = new LLabel().setTextKey("waiting").register();
/**
* This is the progress bar itself that is causing the problem
*/
private final ProgressBar progressBar = new ProgressBar();
/**
* Constructs the tool bar and adds the components
*/
public BottomToolBarImpl() {
super();
addItems();
}
/**
* Adds the progress bar and label the this object
*/
private void addItems() {
Region r = new Region();
r.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(r, Priority.ALWAYS);
progressBar.setMinWidth(192);//This line has no effect on the performance
this.getItems().add(r);
this.getItems().add(text);
this.getItems().add(progressBar);//If i comment out this line then all works perfectly
}
}
Additional information
Most of the visible components on the window (ie. TableView, ToolBar, ListView) are implementations. I doubt this is the problem
A lot of the widely used components such as Buttons and Labels are implementations that implement an interface that allows them to use a key that then gets the key value from a language file for its text. This doesn't do much on the rendering side nor is it called very often so i also doubt this is the problem.
The window starts up rather fast (less than a second).
I have a gaming pc so my hardware should not be a problem.
Java version: 1.8.0_40 (build 1.8.0_40-b25) 64-Bit
I suppose what i'm asking here is has any body else has this problem and if so how did you fix it?
Do you know what the problem could be? I don't think this is a bug as google doesn't have any/many results on it.
Any help would be appreciated as I'm completely stuck here.
An mcve that reproduces the results
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Reproduce problem");
final StackPane root = new StackPane();
primaryStage.setScene(new Scene(root, 500, 400));
final VBox layout = new VBox(10);
layout.getChildren().addAll(new MenuImpl(), new ProgressToolBar());
root.getChildren().add(layout);
primaryStage.show();
}
private class ProgressToolBar extends ToolBar {
private final Label text = new Label("Random Text Here");
private final ProgressBar progressBar = new ProgressBar();
public ProgressToolBar() {
super();
addItems();
}
private void addItems() {
Region r = new Region();
r.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(r, Priority.ALWAYS);
progressBar.setMinWidth(192);
this.getItems().add(r);
this.getItems().add(text);
this.getItems().add(progressBar); //Still causes the problem
}
}
private class MenuImpl extends MenuBar {
public final Menu FILE = new Menu("File", null, new MenuItem("A"), new MenuItem("B"), new MenuItem("C"));
public MenuImpl() {
super();
this.getMenus().addAll(FILE);
}
}
}
Click on the 'File' Menu and scroll through the items before and after resizing the window.
The problem seems to be related to this bug:
[Windows] Very poor performance of application (or Menus) when using an Animation.
As a workaround, run the program with -Dprism.vsync=false or -Dprism.order=sw as VM argument.
Related
I am trying to lose the white background on the webviewer
i have tried work around and other things but nothing seems to work
public static String url = "URL"; //lets say this has a transparent image on it (it does in my case)
public static Scene FrameWorks;
public static StackPane InnerFrame;
public static WebView viewer;
public static WebEngine webEngine;
public static void web() {
viewer = new WebView();
webEngine = viewer.getEngine();
webEngine.executeScript("document.width");
WebSite(url);
}
public static void WebSite(String URL) {
webEngine.executeScript("window.location = \""+URL+"\";");
}
public void start(Stage Level) {
web();
InnerFrame = new StackPane();
FrameWorks = new Scene(InnerFrame, 909, 609);
InnerFrame.getChildren().add(viewer);
FrameWorks.setFill(Color.TRANSPARENT);
InnerFrame.setStyle("-fx-background-color: rgba(255, 0, 0, 0);");
Level.setScene(FrameWorks);
Level.initStyle(StageStyle.TRANSPARENT);
Level.show();
}
I'm not sure that I fully understand your problem so correct me if I am wrong. Do you want everything other than the webview to be transparent? To more or less copy paste a comment from another post, on here I (we) want a Minimal, Complete, and Verifiable example demonstrating the problem. You should also remember to check if the question has already been asked and answered before asking your own question.
Now that I got that out of the way, here's an example where you can see the stack pane with a black background and the webview with a white background.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.layout.Background;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
WebView v = new WebView();
v.setOpacity(1); // 0 = fully transparent, 1 = fully visible.
v.setPrefWidth(100);
v.setMaxWidth(100);
v.setMaxHeight(100);
v.setPrefHeight(100);
v.setStyle("-fx-background-color:black;");
StackPane root = new StackPane();
root.getChildren().add(v);
root.setStyle("-fx-background-color:black;");
/*
* Remove the setStyle above, and use either one of these to only have the webview
* visible. root.setBackground(Background.EMPTY);
* root.setStyle("-fx-background-color:TRANSPARENT;");
*/
Scene scene = new Scene(root, 300, 250);
scene.setFill(Color.TRANSPARENT);
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.show();
}
}
If you remove the line:
root.setStyle("-fx-background-color:black;");
and replace it with either one of the lines that currently are in comments then you will now only see the webview:
/*
* Remove the setStyle above, and use either one of these to only have the webview
* visible.
* root.setBackground(Background.EMPTY);
* root.setStyle("-fx-background-color:TRANSPARENT;");
*/
And you can of course also set a transparency on the actual webview if you want to do that for some reason:
v.setOpacity(0); /*0.0 Fully Transparent - 1.0 Fully Visible*/
Please note that to do this I had to set the StageStyle to transparent, and unfortunately I'm not sure if it is possible to do it in any other way. Maybe someone else can put in comments if it is possible or not.
I hope this answers your question.
I am in the process of teaching myself JavaFX. Coming from the Swing world there are a lot of similarities between the 2. Especially event processing. Part of my process is to try and mimic an existing application as closely as possible. One of the things I am doing is creating a dialog that will allow the user to select a font to use. There is a text field for them to type in the font name and a list where they can scroll and select one. When they start typing the list will automatically scroll to through the list to start matching what the user is typing. I am also trying to populate the text field with the currently matched font name and then highlight the portion that the user has not typed yet so they can continue to type until the correct match is found.
For example if the user types the letter 't' on Windows the first font found is Tahoma. So the text field will be set to Tahoma and the carat will be positioned right after the 'T' and the 'ahoma' will be highlighted. What happens instead is that the field is populated with Tahoma and the carat is positioned at the end and nothing is highlighted. So it is like it is ignoring the 2 lines of code for positioning and highlighting or the event processor is causing my calls to JavaFX libraries to be run out of order.
I think this may be a bug with JavaFX but it could also be my misunderstanding of the event system. Please let me know which one and why.
Here is a complete sample code showing the problem. Just start typing in the text field to try it out.
package test;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class TestTyping extends Application {
ChangeListener<String> textChange;
#Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
TextField text = new TextField();
root.setTop(text);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
textChange = (observable, oldValue, newValue) -> {
text.textProperty().removeListener(textChange);
for (String family : Font.getFamilies()) {
if (family.equalsIgnoreCase(newValue) || family.toLowerCase().startsWith(newValue.toLowerCase())) {
text.setText(family);
text.positionCaret(newValue.length());
text.selectEnd();
break;
}
}
text.textProperty().addListener(textChange);
};
text.textProperty().addListener(textChange);
}
public static void main(String... args) {
launch(args);
}
}
Wrap caret position and select end into Platform.runLater. The problem is in events order. I don't know correct details about this issue so I will not provide you a detailed answer, only solution.
Platform.runLater(()-> {
text.positionCaret(newValue.length());
text.selectEnd();
});
Here's an alternative approach entirely, which uses a TextFormatter to modify changes to the text. The advantage here is that it doesn't rely on the "timing" of various property changes with respect to event handling, which is not documented and thus could possibly change in later JavaFX versions. It also avoids the slightly ugly "remove the listener and add it back" idiom.
import java.util.function.UnaryOperator;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.TextFormatter.Change;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class TestTyping extends Application {
ChangeListener<String> textChange;
#Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
TextField text = new TextField();
root.setTop(text);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
UnaryOperator<Change> filter = c -> {
// for delete, move the caret, or change selection, don't modify anything...
if (c.getText().isEmpty()) {
return c ;
}
for (String family : Font.getFamilies()) {
if (family.toLowerCase().startsWith(c.getControlNewText().toLowerCase())) {
c.setText(family.substring(c.getRangeStart(), family.length()));
c.setAnchor(c.getControlNewText().length());
break ;
}
}
return c ;
};
text.setTextFormatter(new TextFormatter<String>(filter));
}
public static void main(String... args) {
launch(args);
}
}
I have a simple stage with StageStyle.TRANSPARENT (no default buttons).
Therefore I tried to create my own custom buttons, represented each by an ImageView with the next events activated: setOnMouseEntered, setOnMouseExited and of course setOnMouseClicked.
Problem is for the Minmized Button. Is a simple implementation like below
ImageView.setOnMouseClicked((MouseEvent event) -> {
stage.setIconified(true);
});
Lets imagine that my ImageView is a White rectangle. On mouse enter event, it changes its color into Black. On mouse exit, it is going back to White color.
When the ImageView is clicked, the window will be minimized, everything perfectly workable until now.
Problem is when the application is restored (maximized), the Minimized custom button is stuck with color Black (the color that represent the button is hovered), instead of White (default color when is not focused).
P.S. it seems that everything like relocate, setImage etc. inside the onMouseClicked handler is cut by the the setInconified(true);
Any help would be most appreciated.
Thank you for your time of reading this.
Updates to clear a bit the question
The normal print-screen image (when it is not hovered)
The hover print-screen (when it is hovered)
As you can observe, everything works perfectly. In the moment when "-" button (minimize button) is pressed, when the application is restored, it will remain stuck in hover mode, until the mouse cursor will hover again the button (then everything comes back to normal). Sadly neither CSS approach or event listeners on image view dose not seems to solve this issue.
Update code loaded
This is a simple one source file with just a button that call minimize
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class Main extends Application {
private Scene scene;
private Stage stage;
#Override
public void start(Stage stage) {
try {
this.stage = stage;
stage.initStyle(StageStyle.TRANSPARENT);
stage.setAlwaysOnTop(true);
stage.setFullScreen(true);
stage.setFullScreenExitHint("");
createScene(stage);
stage.setScene(scene);
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
private void createScene(Stage stage) {
Pane layer = new Pane();
layer.setPickOnBounds(false);
scene = new Scene(layer, 800, 600);
scene.getStylesheets().add("application/application.css");
layer.getChildren().add(buildMinimizeImage());
}
private ImageView buildMinimizeImage() {
ImageView imv = new ImageView();
int width = 43 ;
int height = 36;
imv.setId("myImage");
imv.setFitWidth(width);
imv.setFitHeight(height);
imv.setOnMouseClicked((MouseEvent event) -> {
stage.setIconified(true);
});
imv.relocate(100, 100);
return imv;
}
public static void main(String[] args) {
launch(args);
}
}
And the application.css is very simple as well
#myImage
{
-fx-image: url("minimize.png");
}
#myImage:hover
{
-fx-image: url("minimizeIn.png");
}
Issue is reproducible on Ubuntu 14.04 and Windows 10. I do not think is an OS problem
RESOLVED
Please find enclose the Harry Mitchel solution (thank you one more time for it). It is perfectly workable.
If you want to fix the code from above I by adding the setOnMousePressed event.
imv.setOnMousePressed((MouseEvent event) -> {
imv.setImage(image);
});
You can listen to the maximized property of the Stage class. Inside the changed() method, set the ImageView's image.
stage.maximizedProperty().addListener(new ChangeListener<Boolean>() {
#Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
//Display the desired icon here.
}
});
Here is a custom minimize button. You provide the two images and the stage as parameters. When the mouse is not over the button, it will show the image referenced in the constructor's first parameter. When the mouse is over the button, it will show the image referenced in the constructor's second parameter. When you click the image the stage will be minimized.
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class MinimizeButton extends Button {
/**
*
* #param img the image when the button is NOT selected
* #param imgHover the image when button is selected
* #param stage the stage that will be minimized
*/
public MinimizeButton(Image img, Image imgHover, Stage stage) {
ImageView imgView = new ImageView(img);
this.setGraphic(imgView);
this.addEventHandler(MouseEvent.MOUSE_ENTERED, (MouseEvent e) -> {
imgView.setImage(imgHover);
});
this.addEventHandler(MouseEvent.MOUSE_EXITED, (MouseEvent e) -> {
imgView.setImage(img);
});
this.setOnAction((ActionEvent event) -> {
stage.setIconified(true);
imgView.setImage(img);
});
}
}
Here is an example app that uses the MinimizeButton class.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class CustomMinimize extends Application {
#Override
public void start(Stage stage) {
Image imgWhite = new Image(getClass().getResourceAsStream("imgWhite.png")); //your image here
Image imgGreen = new Image(getClass().getResourceAsStream("imgGreen.png")); //your hover image here
MinimizeButton btnMinimize = new MinimizeButton(imgWhite, imgGreen, stage);
btnMinimize.setStyle("-fx-background-color: black;");
btnMinimize.setPrefSize(50, 50);
Button btnExit = new Button("X");
btnExit.setMinSize(50,50);
btnExit.setOnAction((ActionEvent event) -> {
System.exit(0);
});
btnExit.setStyle("-fx-background-color: black;");
HBox hBox = new HBox();
hBox.setSpacing(2);
hBox.getChildren().addAll(btnMinimize, btnExit);
AnchorPane anchorPane = new AnchorPane();
anchorPane.getChildren().addAll(hBox);
AnchorPane.setRightAnchor(hBox, 5.0);
AnchorPane.setTopAnchor(hBox, 5.0);
Scene scene = new Scene(anchorPane);
stage.initStyle(StageStyle.TRANSPARENT);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Your question is not very clear (not that it is very unclear though), so I will attempt to solve your problem.
I am assuming that your color change is done through ImageView.setOnMouseEntered() and ImageView.setOnMouseExited(). If this is so, you should instead use CSS.
.myImageView
{
-fx-image: url("my_white_image.png");
}
.myImageView:hovered
{
-fx-image: url("my_black_image.png");
}
For the things in your "PS" section, I couldn't understand, so I would not be able to give any advice on that.
So this problem is a bit of a tricky one. This class is for when, in my main program, I can produce a warning window for potentially dangerous actions that the user might do. The nice thing about this window is that if the user clicks OK, then it will return true to my main class. This is done through the following:
private boolean showWarningWindow(String message)
{
ConfirmationBox warning = new ConfirmationBox(message);
warning.showAndWait();
if (warning.isSelected())
{
return true;
}
return false;
}
This method is in my main GUI class. The problem is below, within ConfirmationBox. The line initModality(Modality.APPLICATION_MODAL); doesn't work properly. If you accidentally click back to your original GUI window, you're screwed, because now your ConfirmationBox window is trapped under your main window AND because of Modality.APPLICATION_MODAL, you won't be able to click anything to get the window back. There isn't a separate application on your taskbar to get the window back in focus and you can't even alt-tab to try and fix it.
Clearly the Modality.APPLICATION_MODAL works but somehow it doesn't make the connection that it needs to interrupt the main window.
Try it in your own applications. Add the showWarningWindow method to your application and add the ConfirmationWindow class and you will see what I mean. I'm not quite sure how to solve this.
package application;
import javafx.beans.property.*;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
public class ConfirmationBox extends Stage
{
private VBox layout = new VBox();
private ReadOnlyBooleanWrapper selected = new ReadOnlyBooleanWrapper();
public boolean isSelected()
{
return selected.get();
}
public ReadOnlyBooleanProperty selectedProperty()
{
return selected.getReadOnlyProperty();
}
public ConfirmationBox(String question)
{
// Core functionality of the ConfirmationBox.
setTitle("Warning");
initStyle(StageStyle.UTILITY);
initModality(Modality.APPLICATION_MODAL);
setResizable(false);
layout.setSpacing(10);
layout.setPadding(new Insets(10));
createControls();
// Add the Label and Buttons to the Confirmation Box.
layout.getChildren().addAll(new Label(question + "\n\n\n"), createControls());
java.awt.Toolkit.getDefaultToolkit().beep();
setScene(new Scene(layout));
sizeToScene(); // workaround because utility stages aren't automatically sized correctly to their scene.
}
private HBox createControls()
{
final Button ok = new Button("OK");
ok.setOnAction(e -> {
selected.set(true);
close();
});
final Button cancel = new Button("Cancel");
cancel.setOnAction(e -> {
selected.set(false);
close();
});
final HBox controls = new HBox(10, ok, cancel);
controls.setAlignment(Pos.CENTER_RIGHT);
return controls;
}
}
How can I get the height of JDialog title bar?
I've tried with getInsets().top, but it returns 0.
Give this a try:
Container c = this.getContentPane();
Point pt = c.getLocation();
pt = SwingUtilities.convertPoint(c, pt, this);
The pt variable now holds the location of the content pane, relative to the origin of the JDialog. Therefore, pt.x is the distance from the left edge and pt.y is the distance from the top.
Caveats:
This assumes no JMenuBar. If you have one, use the location of the JMenuBar instead.
This will include any borders added to the contentPane, or the layerdPane. You'll need to subtract those out.
I don't know if, if you're using OS's own window manager, this may not be possible. The reason is that, the title bar is drawn outside Java. If you really need this information you will probably need to use JNI.
However, if you are using internal frames, you can do the following
JInternalFrame mydialog = new JInternalFrame();
((BasicInternalFrameUI)mydialog.getUI()).getNorthPane().getHeight();
But a more important question is, why do you want to know? The idea of a windowing system is so that the programmer can abstract the content of his application from the window environment. This is so that window appearances can be customised dynamically by the user, look homogeneous across all apps, and not interfere with the application's normal running.
Such an interface between OS and app would require a whole message-passing API to inform when window decorations change etc.
kofucii, you are on the right path - use the dialog getInsets().top as you did in your example.
I guess you got 0 probably because the JDialog object was invisible. Otherwise the 'top' value should be ~36 ...
Here is an isolated case:
package dejan.various;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
/**
*
* #author dejan
*/
public class TestDialog extends JDialog implements ActionListener {
private javax.swing.JButton testButton;
public TestDialog() {
setPreferredSize(new Dimension(640,480));
getContentPane().setLayout(new BorderLayout());
testButton = new JButton("Click me");
testButton.addActionListener(this);
getContentPane().add(testButton, BorderLayout.SOUTH);
pack();
}
#Override
public void actionPerformed(ActionEvent e) {
Insets insets = this.getInsets();
System.out.println(insets.top);
System.out.println(insets.left);
}
/**
* #param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestDialog().setVisible(true);
}
});
}
} // TestDialog class
On the STDOUT I get:
36
9
To get insets you need to make sure your JDialog is visible. if you try to get it before showing on screen you got 0.