JavaFx 2.2 & Fullscreen mode - fullscreen

Good day!
I try my first application JavaFx. I set the full-screen mode and press the button shows a dialog. When the dialog appears the main window loses its full-screen.
Code:
public class Test1 extends Application {
#Override
public void start(final Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Stage dialogStage = new Stage(StageStyle.UTILITY);
dialogStage.initModality(Modality.APPLICATION_MODAL);
dialogStage.setScene(new Scene(VBoxBuilder.create().
children(new Text("Hi"), new Button("Ok.")).
alignment(Pos.CENTER).padding(new Insets(5)).build()));
dialogStage.show();
System.out.println(dialogStage.getOwner()==primaryStage.getOwner());
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Rectangle2D r = Screen.getPrimary().getBounds();
Scene scene = new Scene(root, r.getWidth(), r.getHeight());
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.setFullScreen(true);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
OS: Windows 7

You need to
dialogStage.initOwner(primaryStage);

Related

JavaFX - How to add ColorPicker to my scene

How can I add a ColorPicker to my application?
I tryed with this code:
public class MyColorPicker extends Application {
#Override
public void start(Stage primaryStage) {
VBox box = new VBox();
ColorPicker cp = new ColorPicker();
box.getChildren().add(cp) // Error: cp Cannot be converted to Node
Scene scene = new Scene(box, 300, 200);
primaryStage.setTitle("colorPicker");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

JavaFX: Showing a dialog in browser on fullscreen mode

I'm experiencing some weird behavior while trying to show a dialog on my application when my Stage is on fullscreen mode.
Everything works OK while I'm launching my application as a standalone app. When I run from the browser, the modal dialog takes the Stage out of fullscreen. Although, it doesn't go really "out" of fullscreen mode... The code still thinks it's fullscreen (fullscreenProperty == true), but it doesn't show as fullscreen. And after closing the dialog, if you press ESC (which would take you out of fullscreen mode), the application crashes.
Here's a sample code:
public class Test1 extends Application {
#Override
public void start(final Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Stage dialogStage = new Stage(StageStyle.UTILITY);
dialogStage.initModality(Modality.APPLICATION_MODAL);
dialogStage.setScene(new Scene(VBoxBuilder.create().
children(new Text("Hi"), new Button("Ok.")).
alignment(Pos.CENTER).padding(new Insets(5)).build()));
dialogStage.initOwner(primaryStage);
dialogStage.show();
System.out.println(primaryStage.isFullScreen());
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Rectangle2D r = Screen.getPrimary().getBounds();
Scene scene = new Scene(root, r.getWidth(), r.getHeight());
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.setFullScreen(true);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
Any idea how to solve this? Or any idea of what I can use instead of a Stage dialog?
Thanks in advance.

Stage is hidden when dialog is shown

I have this code which displays confirmation dialog to exit application.
public class DialogPanels
{
public void initClosemainAppDialog(final Stage primaryStage)
{
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>()
{
#Override
public void handle(WindowEvent event)
{
event.consume(); // Do nothing on close request
// Dialog Stage init
final Stage dialog = new Stage();
dialog.initModality(Modality.APPLICATION_MODAL);
// Frage - Label
Label label = new Label("Exit from the program");
// Button "Yes"
Button okBtn = new Button("Yes");
okBtn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
//primaryStage.close();
//dialog.close();
//Platform.exit();
System.exit(0);
}
});
// Button "No"
Button cancelBtn = new Button("No");
cancelBtn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
primaryStage.show();
dialog.close();
}
});
// Layout for the Button
HBox hbox = new HBox();
hbox.setSpacing(10);
hbox.setAlignment(Pos.CENTER);
hbox.getChildren().add(okBtn);
hbox.getChildren().add(cancelBtn);
// Layout for the Label and hBox
VBox vbox = new VBox();
vbox.setAlignment(Pos.CENTER);
vbox.setSpacing(10);
vbox.getChildren().add(label);
vbox.getChildren().add(hbox);
// Stage
Scene scene = new Scene(vbox);
dialog.setScene(scene);
dialog.show();
}
});
}
}
The problem is that when close the main application the dialog box is displayed and the main stage is hidden. I want to display the dialog box in front of the main stage. Can you help me to correct this?
UPDATE
I tested this code, it's working but when the dialog is displayed the mainstage is not responsible(frozen). How I an make the mainstage responsible when I display dialog?
Consume the closing event and set the owner of the stage if you do not want to see another window when the windows are minimized:
#Override
public void handle(WindowEvent event)
{
event.consume(); // Do nothing on close request
// Dialog Stage init
final Stage dialog = new Stage();
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.initOwner(primaryStage);
// other stuff
}
});
You need to set the proper relationships between primaryStage and dialog stage. Here's a hint to get you going:
...
dialog.initOwner(primaryStage);
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.showAndWait();
You can find more information in Oracle's JavaFX 2 JavaDocs.
More example code (edit)
I'm using setOnHiding(..) instead of setOnCloseRequest(..):
stage.setOnHiding(new AskUserIfHeReallyWantsToQuitWindowHandler(stage));
I extracted your code into a seperate event handler class and fixed the issues I mentioned (sorry, I am little short on time right now):
public class AskUserIfHeReallyWantsToQuitWindowHandler implements EventHandler<WindowEvent> {
private final Stage primaryStage;
public AskUserIfHeReallyWantsToQuitWindowHandler(final Stage primaryStage) {
Objects.requireNonNull(primaryStage);
this.primaryStage = primaryStage;
}
#Override
public void handle(final WindowEvent event) {
event.consume();
final Stage dialog = new Stage();
final Button okBtn = new Button("Yes");
okBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(final ActionEvent event) {
dialog.close();
primaryStage.close();
}
});
// Button "No"
final Button cancelBtn = new Button("No");
cancelBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(final ActionEvent event) {
dialog.close();
Platform.runLater(new Runnable() {
#Override
public void run() {
primaryStage.show();
}
});
}
});
// Layout for the Button
final HBox hbox = new HBox();
hbox.setSpacing(10);
hbox.setAlignment(Pos.CENTER);
hbox.getChildren().add(okBtn);
hbox.getChildren().add(cancelBtn);
// Layout for the Label and hBox
final VBox vbox = new VBox();
vbox.setAlignment(Pos.CENTER);
vbox.setSpacing(10);
vbox.getChildren().add(new Label("Do your really want to exit?"));
vbox.getChildren().add(hbox);
// Stage
final Scene scene = new Scene(vbox);
dialog.setScene(scene);
dialog.initOwner(primaryStage);
dialog.initModality(Modality.NONE);
dialog.showAndWait();
}
}

Popup window with table view in JavaFX 2.0

I want to click a button in order to make a popup window appear with a tableview element inside it. Can anyone tell me how to do it?
Thanks in advance.
This is the code for simple popup window in JavaFX.
Hope this helps.
public class PopupExample extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(final Stage primaryStage) {
primaryStage.setTitle("Popup Example");
final Popup popup = new Popup();
popup.setX(300);
popup.setY(200);
popup.getContent().addAll(new Circle(25, 25, 50, Color.AQUAMARINE));
Button show = new Button("Show");
show.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
popup.show(primaryStage);
}
});
Button hide = new Button("Hide");
hide.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
popup.hide();
}
});
HBox layout = new HBox(10);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
layout.getChildren().addAll(show, hide);
primaryStage.setScene(new Scene(layout));
primaryStage.show();
}
}
What kind of popup window do you need? Implemented with using a new Stage or Popup control? JavaFX has a control named Popup, read about it to see does it meet your needs. An entry point for Stage version could be Dialog with CLOSE button.

How to make drag and drop action in JavaFX 2.2 with Swing?

I have a problem with drag and drop event on JFXPanel that on located JPanel. When i push drag message to DragBoard, javaFX part of application doesnt work anymore. I think its about swing event mechanizm but i am not sure. There is no problem with other events. It made me confused. Is there any solution to this problem? Thanks in advance.
public class MyScene extends Scene {
public MyScene(VBox vBoxMainLayout) {
super(vBoxMainLayout);
HBox hBox = new HBox();
hBox.setPrefSize(10000, 10000);
hBox.setSpacing(40);
Button buttonSource = new Button("Source");
buttonSource.setMinSize(60, 30);
buttonSource.setOnDragDetected(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
Dragboard db = startDragAndDrop(TransferMode.ANY);
ClipboardContent content = new ClipboardContent();
String message = "Drag operatation is done";
content.putString(message);
db.setContent(content);
event.consume();
}
});
buttonSource.setOnDragDone(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
event.consume();
}
});
TextArea textAreaTarget = new TextArea();
textAreaTarget.setMinSize(200, 500);
hBox.getChildren().add(buttonSource);
hBox.getChildren().add(textAreaTarget);
vBoxMainLayout.getChildren().add(hBox);
}
}
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
VBox vBoxMainLayout = new VBox();
MyScene myScene = new MyScene(vBoxMainLayout);
JFrame frame = new JFrame();
JFXPanel arg0 = new JFXPanel();
arg0.setScene(myScene);
frame.getContentPane().add(arg0);
frame.setVisible(true);
}
}
It was a known deadlock in JavaFX 2.1 and pushed to 2.2 (thats what i learned from oracle ) but i guess it stil not solved.

Resources