I am creating a sample code to show the Stage designed in Javafx, It should not have Minimize and Maximize Button only Close ('X') button required.
For that we are using following code.
Stage stage = new Stage();
// Here we have load it using JFXML
stage.initModality(Modality.WINDOW_MODAL);
stage.initStyle(StageStyle.UTILITY);
stage.setResizable(true);
if (title != null && !title.trim().isEmpty()) {
stage.setTitle(title);
}
stage.setWidth(w);
stage.setHeight(h);
stage.getIcons().add(new Image(Dialog.class.getResourceAsStream("/image/myicon.png")));
stage.showAndWait();
Now the icon I set on the stage is not visible.
What I am missing ?
I assume Windows as the OS (in MacOSX the icon is shown). Under Windows, StageStyle.UTILITY leads to using WS_EX_TOOLWINDOW in which case the icon is not shown. You probably need to use another StageStyle.
"PRB: WS_EX_TOOLWINDOW Windows Do Not Show System Menu Icon": http://support.microsoft.com/kb/179376
Related
I am making a simple software to Reserve Stadium Seats. I have a main page wish is the Login Page. I have a simple design. With a transparent background just to give it a form. As you can see in the image below.
As you can see the code can bee seen behind the form. When i login i have a simple logout button that takes me again to the same fxml to load the form. but this time the background is no longer transparent: . As you see here it is green. it is by default white but i was trying to make it transparent again.
this is the code i tried to do:
public void logoutButton (ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
root.setStyle("-fx-background-color: #20825170;");
stage.setScene(scene);
}
even changing the style to transparent wont work i tried it. any help ?
You need to set the fill of the scene to transparent when a user logs out
scene.setFill(Color.TRANSPARENT);
Edit
As #jewelsea stated in the comments, You don't have to set the scene's fill to TRANSPARENT every time if you don't change the scene, you can keep the same scene and only set the fill to transparent the first time (in the Application's start method will work)
I use the below code to open a javafx fxml file in a different window on a button click event and it works fine. But if I click the same button again while the window opened it will create a duplicate window. Is there a possibly solutions to overcome this problem? Thanks in advance.
Parent parent = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Stage stage = new Stage(StageStyle.DECORATED);
stage.setTitle("Title");
stage.setScene(new Scene(parent));
stage.show();
I just stumbled upon this old question and figured I might answer it for anyone new to JavaFX or coding in general (I'm bored..).
In the provided code (see below) a new Stage is created every time, meaning that if this is run inside of a method you actually create a new Stage variable every time that the code is run:
Stage stage = new Stage(StageStyle.DECORATED);
What you could do instead create your Stage variable outside the method so that you either 1. just overwrite it every time or 2. have some "is showing" or a nullcheck or similar to see if a new stage should be created or if the existing one just needs to be shown.
For example:
private Stage stage;
private void onOpenNewStageBtnClicked(){
if(stage == null){
Parent parent = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
stage = new Stage(StageStyle.DECORATED);
stage.setTitle("Title");
stage.setScene(new Scene(parent));
}
stage.show();
}
Also, what I usually do is I create a Stage factory to avoid a lot of duplicate code and so that I can break out the creation of the stages and the loading of fxmls to other classes than my controller.
I am trying my way through JavaFX and still have many - probably silly - beginner questions.
My problem of the day is the following:
I am creating, in Scene builder and Controller, a FlowPane to which I want to add a right-click option, that opens a Context Menu.
Through the scene builder I have added the function OnContextMenuRequested and defined it in the Controller.
To check, I have added a print commend and a Dialog Box to the function, which work well.
Yet, the Context Menu does not work..
Anybody could help and tell me what am I missing???
Thanks in advance...
public void contextMenu(ContextMenuEvent contextMenuEvent) {
// working fine ..
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information");
alert.setHeaderText("Look");
alert.setContentText("Message");
alert.showAndWait();
// working fine
System.out.println("Hello");
// Context Menu ......... not working
ContextMenu contextMenu = new ContextMenu();
MenuItem quit = new MenuItem("quit");
MenuItem hello = new MenuItem("hello");
contextMenu.getItems().addAll(quit, hello);
contextMenu.setX(10.0);
contextMenu.setY(10.0);
contextMenu.show();
????.setContextMenu(????)
}
Unless you have a control, you need to show the ContextMenu "manually" using one of the methods defined in ContextMenu:
// contextMenu.setX(10.0);
// contextMenu.setY(10.0);
contextMenu.show((Node) contextMenuEvent.getSource(), contextMenuEvent.getScreenX(), contextMenuEvent.getScreenY());
I'm a beginner, I don't know how to open a new stage in same window in javaFX8?
Stage modal_stage = new Stage();
modal_stage.setScene(new Scene(root, 500, 575));
modal_stage.setTitle("modal");
modal_stage.initModality(Modality.APPLICATION_MODAL);
modal_stage.initOwner(modal_stage.getOwner());
modal_stage.setResizable(false);
modal_stage.show();
When I opened a new stage through the above code, the stage always opening in a difference window,like this:
But I want the new stage will not create a new icon in StartMenu taskbar, just like Swing's Dialog of Jframe---No matter how many dialogs I open under jframe, It is always displayed as one window. So what can I do?
Sorry, I am not very good at English, I wish I had clarified the question.
Assuming that you have a parent stage:
Stage parentStage = new Stage();
And the child Stage:
Stage childStage = new Stage();
You have to set the init owner of the childStage to be the parent:
childStage.initOwner(parent);
Mention that the above has to be called before the childStage to be shown.
Also mention that doing this you will have some behaviour like when parent child is minimized then childStage will be also minimized.
I am creating a JavaFX Dialog and want to use the default icons for Info/Warning/Error.
In Swing, I can get the Information icon this way:
UIManager.getIcon("OptionPane.informationIcon")
How can I do the same in JavaFX?
I asked for this some days ago. I use this code to make labels with default icons:
Label img = new Label();
img.getStyleClass().addAll("alert", "error", "dialog-pane");
dialog.setGraphic(img);
If you get a copy of the images you can use these ideas.
Alert example:
#FXML void handleHelpButton(ActionEvent event){
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Help");
alert.setHeaderText("Help");
alert.setGraphic(new ImageView(this.getClass().getResource("img/help.png").toString()));
alert.setContentText("Place the cursor over a button for hint.");
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image(this.getClass().getResource("img/help.png").toString()));
alert.showAndWait();
}
Dialog example:
ChoiceDialog<String> dialog = new ChoiceDialog<>(currentFullscreenSetting, choices);
dialog.setTitle("Settings");
dialog.setHeaderText("Settings");
dialog.setContentText("Fullscreen on startup: ");
dialog.setGraphic(new ImageView(this.getClass().getResource("img/settings.png").toString()));
Stage stage2 = (Stage) dialog.getDialogPane().getScene().getWindow();
stage2.getIcons().add(new Image(this.getClass().getResource("img/settings.png").toString()));
// Traditional way to get the response value.
Optional<String> result = dialog.showAndWait();
This part of both examples is tricky. I noticed that this would not work unless I had the image in the same folder as the .fxml and controller.java files or in a folder that is in the same folder has the files mentioned. You might have to play with you file location. In my example it appears that my setGraphic and getIcons images are in the same folder, but they are not.
stage.getIcons().add(new
Image(this.getClass().getResource("img/help.png").toString()));
My file structure looks like:
PlanningChart
css
img
planningchart
img
The second img folder holds the images for stage.getIcons.add(). The images could also be probably use to for setGraphic. I did not try it.
You do not need to create custom JavaFX dialogs for Info/Warning/Error, since JavaFX already have created Alerts for you.
The Alert class subclasses the Dialog class, and provides support for a number of pre-built dialog types that can be easily shown to users to prompt for a response.
You can create different types of Alerts, depending on the AlertType, it will embed the necessary image.
For Information alert use :
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText("An Information Dialog");
alert.setContentText("Information Message");
alert.showAndWait();
Similarly, for Warning alert, you can use
AlertType.WARNING
and for Error alert, you can use :
AlertType.ERROR