JavaFx Showing popup is not working on client machine - javafx-8

Following is my code to show popup windows in my JavaFx Desktop Application.
public boolean popup(Object parent, ViewModelBase viewModel, AsyncCommand cancelCommand) {
javafx.scene.layout.Pane root = null;
try
{
if(!IOC.platformInfo.isPlatformThread())
{
return PlatformUtil.runAndWait(()->
{
return popup(parent,viewModel,cancelCommand);
});
}
String name = viewModel.getClass().getSimpleName();
name = Pattern.compile("(viewModel|Controller)$",Pattern.CASE_INSENSITIVE)
.matcher(name).replaceAll("View");
FXMLLoader loader = new FXMLLoader(com.technoinn.videoprospector.ui.fx.service.WindowService
.class.getResource(String.format("/fxml/%s.fxml",name))
, null, new JavaFXBuilderFactory(), new IocControllerFactory());
if(viewModel instanceof ControllerBase)
{
loader.setController(viewModel);
}
root = loader.load();
if(!(viewModel instanceof ControllerBase))
{
Object controller = loader.getController();
if(controller instanceof ControllerBase)
{
((ControllerBase)controller).setViewModel(viewModel);
}
}
jfxtras.scene.control.window.Window window =
new jfxtras.scene.control.window.Window(viewModel.getDisplayName());
window.getContentPane().getChildren().add(root);
window.setPrefSize(root.getPrefWidth(), root.getPrefHeight());
window.setMinSize(root.getPrefWidth(), root.getPrefHeight());
CloseIcon closeIcon = new CloseIcon(window);
window.getLeftIcons().add(closeIcon);
Scene scene = new Scene(window);
// Scene scene = new Scene(root);
scene.getStylesheets().add(FxModule.StyleFile);
Stage stage = new Stage(StageStyle.UNDECORATED);
stage.setResizable(true);
stage.setMinWidth(root.getPrefWidth());
stage.setMinHeight(root.getPrefHeight());
viewModel.addPropertyChangeListener(ViewModelBase.closeCommand,
(x)->
{
if(x.getNewValue()!=null && x.getNewValue()==Boolean.TRUE)
{
stage.close();
}
});
closeIcon.setCursor(Cursor.HAND);
closeIcon.setOnAction((x)->
{
if(cancelCommand!=null)
cancelCommand.beginExecution();
else
stage.close();
});
/*
stage.setOnCloseRequest((WindowEvent event) -> {
if(cancelCommand!=null)
cancelCommand.beginExecution();
else
stage.close();
});*/
stage.setScene(scene);
stage.centerOnScreen();
stage.initModality(Modality.APPLICATION_MODAL);
if(!parentWindows.isEmpty())
{
stage.initOwner(parentWindows.peek());
stage.initModality(Modality.WINDOW_MODAL);
}
parentWindows.push(stage);
stage.showAndWait();
parentWindows.pop();
}catch(Exception exp)
{
Logger.getGlobal().log(Level.SEVERE,"Error in popup",exp);
}
return true;
}
Problem is, popup shows well and in proper size on my machine.(Dev Machine). But size on target client machine is unpredictable. Sometimes it is very small and sometimes it does not even show the content pane of the popup window. Client machine has jRE 1.8_31. Any idea what can be wrong. Client machine size is same as that of my dev machine.
Thanks

Most probably you are calling next code too soon:
window.setPrefSize(root.getPrefWidth(), root.getPrefHeight());
window.setMinSize(root.getPrefWidth(), root.getPrefHeight());
and
stage.setMinWidth(root.getPrefWidth());
stage.setMinHeight(root.getPrefHeight());
Most layout values are being calculated only after scene is shown. Try to move such code after call to
stage.showAndWait();

Related

Select Children when selecting Parent checkbox

I am using a Grid where the first column is checkbox. Every row is a folder which can have many other elements to be selected. There could be another folder inside a folder.
Now, when I have to select a element I have to select it one by one. I am not able to understand that how could I make it possible that if I check a folder checkbox, It checks the all selectable elements inside this folder.
Please let me know if more info required.
RemoteSortTreeLoader<BasicModel> loader =
new BaseRemoteSortTreeLoader<BasicModel>(proxy, reader) {
public boolean hasChildren(BasicModel parent) {
//code;
}
};
TreeStore store = new TreeStore(loader);
List<ColumnConfig> columnList = new ArrayList<ColumnConfig>();
CheckBoxSelectionModel checkBoxSelectionModel =
new CheckBoxSelectionModel();
columnList.add(checkBoxSelectionModel.getColumn());
ColumnModel columns = new ColumnModel(columnList);
EditorTreeGrid grid = new EditorTreeGrid<BasicModel>(store,columns);
grid.getSelectionModel().setSelectionMode(SelectionMode.SIMPLE);
grid.getSelectionModel().addListener(Events.BeforeSelect,
new Listener<SelectionEvent<BasicModel>>() {
#Override
public void handleEvent(SelectionEvent<BasicModel> event) {
if (event.getModel() instanceof SDPTimelineCatalogModel) {
event.setCancelled(false);
}
} // handleEvent
}
);
grid.getSelectionModel().addSelectionChangedListener(
new SelectionChangedListener<BasicModel>() {
#Override
public void selectionChanged(SelectionChangedEvent<BasicModel> event) {
logger.info(" Inside addSelectionChangedListener ");
if (event.getSelection().size() == 0) {
disableNext();
} else {
enableNext();
}
} // selectionChanged
}
);
thanks

TreeView using Drag and Drop incorrect getGestureSource()

I have a simple tree view with drag and drop enabled. But, on setOnDragOver() call, I receive event with gesture source. The source got is not the cell that starts the dragging (via setOnDragDetected() call), but the pane that contains the tree view. What am I missing ?
My code is shown below:
tree.setCellFactory(
new Callback<TreeView<String>, TreeCell<String>>() {
#Override
public TreeCell<String> call(
final TreeView<String> param) {
TreeCell<String> treeCell =
new TreeCell<String>() {
#Override
protected void updateItem(
final String value,
final boolean empty) {
super.updateItem(value, empty);
if (!empty && (value != null)) {
setText(value);
setGraphic(getTreeItem().getGraphic());
} else {
setText(null);
setGraphic(null);
}
}
};
treeCell.setOnDragDetected(event -> {
Dragboard dragBoard =
startDragAndDrop(TransferMode.MOVE);
ClipboardContent content = new ClipboardContent();
content.putString(treeCell.getTreeItem().getValue());
dragBoard.setContent(content);
event.consume();
});
treeCell.setOnDragOver(event -> {
// Here is I've got pane instead of cell
Object source = event.getGestureSource();
if ((event.getGestureSource() != treeCell)
&& event.getDragboard().hasString()) {
event.acceptTransferModes(TransferMode.MOVE);
}
event.consume();
});
return treeCell;
}
});
The gesture source is the node on which startDragAndDrop(...) was called; you are calling it on whatever node instance you are currently in (I guess you have some custom subclass of Pane, or something).
So you need
treeCell.setOnDragDetected(event -> {
Dragboard dragBoard =
treeCell.startDragAndDrop(TransferMode.MOVE);
// ...
});

Window Only Renders correctly on first show

I have a window that I am opening like so
if (Window == null) {
var con = WindowType.GetConstructor(new Type[0]);
Window = (PopupWindow)con.Invoke(new object[0]);
//The types are subclasses of PopupWindow.
Window.Controller = this;
Window.Show ();
}
This correctly displays the window as long as it is the first of these windows to pop up... If I close the window and create an entirely new one, the window is just a grey area until I restart debugging... Any ideas?
public PopupWindow () : base(Gtk.WindowType.Toplevel)
{
this.AppPaintable = true;
this.Colormap = this.Screen.RgbaColormap;
this.Events = Gdk.EventMask.AllEventsMask;
this.Decorated = false;
this.SkipTaskbarHint = true;
}
Example subclass
public StorageWindow () : base()
{
this.Build ();
this.Move (this.Screen.Width - 428, 55);
//set some label props.
StorageCircle.ExposeEvent += (o, args) => {
//Draw a circle
};
}
P.S. This is how I am destroying the window.
if (Window != null) {
Window.Destroy();
Window = null;
}
The entire issue turned out to be caused by a non gtk timer trying to edit widgets outside of the main thread.

KeyEvent on Dialog not consumed when closing in JavaFX

I have a requirement to navigate a table when the user hits the ENTER key. For this I have created an event filter similar to:
private EventHandler<KeyEvent> keyReleasedFilter = event -> {
if ((event.getCode() == KeyCode.ENTER || event.getCode() == KeyCode.TAB)) {
previousPosition = table.getFocusModel().getFocusedCell();
//do my navigation
}
}
I have run into an issue where JavaFX Modal Dialogs used during navigation of the table to indicate an error, cause issues with this filter. If the user closed the dialog with the ENTER key, that event is trapped by my event filter on the parent stage. I am not sure how to prevent that. It is causing inconsistent navigation.
Here is a simple application that demonstrates the behavior:
public void start(Stage primaryStage) {
final Alert d = new Alert(Alert.AlertType.ERROR);
d.initModality(Modality.WINDOW_MODAL);
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.addEventFilter(KeyEvent.KEY_RELEASED, event -> {
if ((event.getCode() == KeyCode.ENTER || event.getCode() == KeyCode.TAB)) {
d.showAndWait();
}
});
Scene scene = new Scene(new StackPane(btn), 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
I have noticed that the dialog is closed with the KEY_PRESSED event, and will not capture the KEY_RELEASED event.
I have tried adding an EVENT FILTER to the dialog (Button/DialogPane/Scene and even Stage) - none intercept the KEY_RELEASED event.
Thanks.
Unless there is another reason to use the KEY_RELEASED event, then I would recommend switching to triggering on KEY_PRESSED for navigation as well and switch your EventFilter to an EventHandler. The example below will allow you to toggle the Alert on and off. When it's on, you'll notice that the button text doesn't change. When it's off, the button text will change. Take a look at how JavaFX constructs Event chains here if you haven't already.
boolean error = false;
int i = 0;
#Override
public void start(Stage primaryStage)
{
final Alert d = new Alert(Alert.AlertType.ERROR);
d.initModality(Modality.WINDOW_MODAL);
Button err = new Button();
err.setText("Error off");
err.addEventHandler(ActionEvent.ACTION, t ->
{
error = !error;
if (error)
err.setText("Error on");
else
err.setText("Error off");
});
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.addEventHandler(KeyEvent.KEY_PRESSED, event ->
{
if ((event.getCode() == KeyCode.ENTER || event.getCode() == KeyCode.TAB)) {
if (error)
{
d.showAndWait();
}
else
{
i++;
btn.setText(String.valueOf(i));
}
}
});
Scene scene = new Scene(new VBox(btn, err), 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
So I have a hack that works - I really don't like it, so I am hoping that there is a better solution (a clean one) to solve this issue....
Basically, right before I open my dialog, I set a boolean so I know it's open. Then in my event filter, kick out if that is set to true.
public class EventTester extends Application{
public static void main(String[] args){
launch(args);
}
private boolean modalWasShowing = false;
#Override
public void start(Stage primaryStage) {
final Alert d = new Alert(Alert.AlertType.ERROR);
d.initModality(Modality.WINDOW_MODAL);
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.addEventFilter(KeyEvent.KEY_RELEASED, event -> {
if(modalWasShowing){
modalWasShowing=false;
return;
}
if ((event.getCode() == KeyCode.ENTER || event.getCode() == KeyCode.TAB)) {
modalWasShowing = true;
d.showAndWait();
}
});
Scene scene = new Scene(new StackPane(btn), 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
Please let me know if you know of a better way to handle this.

How to change or override the tooltip in JavaFX ColorPicker

I am using JavaFX ColorPicker in my application. As per my requirements, I have mapped the default colors on the color picker to a number. I want this number to be displayed as tooltip on hover over the color instead of hex value of the color. How can I achieve this?
//Part of Code
public void handleNodes(Circle circularNode) {
final Delta offset = new Delta();
circularNode.setOnMouseEntered(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
((Circle)(event.getSource())).setCursor(Cursor.HAND);
}
});
circularNode.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if(event.getButton().equals(MouseButton.SECONDARY)) {
System.out.println("Right click");
Circle parent = ((Circle)(event.getSource()));
final ContextMenu contextMenu = new ContextMenu();
MenuItem editLabel = new MenuItem("Edit Label");
editLabel.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Edit Label");
final ColorPicker colorPicker = new ColorPicker();
colorPicker.setStyle("-fx-border-radius: 10 10 10 10;"
+ "-fx-background-radius: 10 10 10 10;");
colorPicker.setValue((Color) parent.getFill());
colorPicker.showingProperty().addListener((obs,b,b1)->{
if(b1){
PopupWindow popupWindow = getPopupWindow();
javafx.scene.Node popup = popupWindow.getScene().getRoot().getChildrenUnmodifiable().get(0);
popup.lookupAll(".color-rect").stream()
.forEach(rect->{
Color c = (Color)((Rectangle)rect).getFill();
Tooltip.install(rect.getParent(), new Tooltip("Custom tip for "+c.toString()));
});
}
});
panelMain.getChildren().add(colorPicker);
}
});
This is really a hacky answer.
The first problem: you have to find the popup node on the scene once it shows up. But you won't... since its not in the same window!
Having a deep look at how ScenicView does it, the trick is getting the list of windows at that moment, but using a deprectated method:
private PopupWindow getPopupWindow() {
#SuppressWarnings("deprecation") final Iterator<Window> windows = Window.impl_getWindows();
while (windows.hasNext()) {
final Window window = windows.next();
if (window instanceof PopupWindow) {
return (PopupWindow)window;
}
}
return null;
}
Once you have the popup window, we can now check for all the Rectangle nodes using lookupAll and the CSS selector color-rect, to get their color, and install the tooltip over its parent container:
#Override
public void start(Stage primaryStage) {
ColorPicker picker = new ColorPicker();
StackPane root = new StackPane(picker);
Scene scene = new Scene(root, 500, 400);
primaryStage.setScene(scene);
primaryStage.show();
picker.showingProperty().addListener((obs,b,b1)->{
if(b1){
PopupWindow popupWindow = getPopupWindow();
Node popup = popupWindow.getScene().getRoot().getChildrenUnmodifiable().get(0);
popup.lookupAll(".color-rect").stream()
.forEach(rect->{
Color c = (Color)((Rectangle)rect).getFill();
Tooltip.install(rect.getParent(), new Tooltip("Custom tip for "+c.toString()));
});
}
});
}
This is what it looks like:
Based on the code posted by the OP after my first answer, and due to the substancial changes in the problem addressed, I'm adding a new answer that covers both situations:
The ColorPicker is embedded in the main scene, as a regular node
The ColorPicker is embedded in a ContextMenu
In the second situation, the proposed solution for the first one is no longer valid, since the window found will be the one with the context menu.
A task is required to keep looking for windows until the one with the ComboBoxPopupControl is found.
This is a full runnable example:
public class ColorPickerFinder extends Application {
ExecutorService findWindowExecutor = createExecutor("FindWindow");
#Override
public void start(Stage primaryStage) {
AnchorPane panCircles = new AnchorPane();
Scene scene = new Scene(panCircles, 400, 400);
final Random random = new Random();
IntStream.range(0,5).boxed().forEach(i->{
final Circle circle= new Circle(20+random.nextInt(80),
Color.rgb(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
circle.setTranslateX(100+random.nextInt(200));
circle.setTranslateY(100+random.nextInt(200));
panCircles.getChildren().add(circle);
});
panCircles.setPrefSize(400, 400);
ColorPicker colorPicker = new ColorPicker();
panCircles.getChildren().add(colorPicker);
primaryStage.setScene(scene);
primaryStage.show();
// We add listeners AFTER showing the stage, as we are looking for nodes
// by css selectors, these will be available only after the stage is shown
colorPicker.showingProperty().addListener((obs,b,b1)->{
if(b1){
// No need for task in this case
getPopupWindow();
}
});
panCircles.getChildren().stream()
.filter(c->c instanceof Circle)
.map(c->(Circle)c)
.forEach(circle->{
circle.setOnMouseClicked(e->{
if(e.getButton().equals(MouseButton.SECONDARY)){
// We need a task, since the first window found is the ContextMenu one
findWindowExecutor.execute(new WindowTask());
final ColorPicker picker = new ColorPicker();
picker.setStyle("-fx-border-radius: 10 10 10 10;"
+ "-fx-background-radius: 10 10 10 10;");
picker.setValue((Color)(circle.getFill()));
picker.valueProperty().addListener((obs,c0,c1)->circle.setFill(c1));
final ContextMenu contextMenu = new ContextMenu();
MenuItem editLabel = new MenuItem();
contextMenu.getItems().add(editLabel);
editLabel.setGraphic(picker);
contextMenu.show(panCircles,e.getScreenX(),e.getScreenY());
}
});
});
}
private PopupWindow getPopupWindow() {
#SuppressWarnings("deprecation")
final Iterator<Window> windows = Window.impl_getWindows();
while (windows.hasNext()) {
final Window window = windows.next();
if (window instanceof PopupWindow) {
if(window.getScene()!=null && window.getScene().getRoot()!=null){
Parent root = window.getScene().getRoot();
if(root.getChildrenUnmodifiable().size()>0){
Node popup = root.getChildrenUnmodifiable().get(0);
if(popup.lookup(".combo-box-popup")!=null){
// only process ComboBoxPopupControl
Platform.runLater(()->{
popup.lookupAll(".color-rect").stream()
.forEach(rect->{
Color c = (Color)((Rectangle)rect).getFill();
Tooltip.install(rect.getParent(),
new Tooltip("Custom tip for "+c.toString()));
});
});
return (PopupWindow)window;
}
}
}
return null;
}
}
return null;
}
private class WindowTask extends Task<Void> {
#Override
protected Void call() throws Exception {
boolean found=false;
while(!found){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
found=(getPopupWindow()!=null);
}
return null;
}
}
private ExecutorService createExecutor(final String name) {
ThreadFactory factory = r -> {
Thread t = new Thread(r);
t.setName(name);
t.setDaemon(true);
return t;
};
return Executors.newSingleThreadExecutor(factory);
}
public static void main(String[] args) {
launch(args);
}
}
This will be the result after right clicking on a circle, and clicking on the color picker: