12

i have undecorate the window in javafx2. Now i want to minimize the window by means of the action. This is my code

    minIcon.setOnMouseClicked(new EventHandler<MouseEvent>() {
        public void handle(MouseEvent me) {
            primaryStage.toBack();
        }
    });

The window is go back when another one is open. otherwise its not. pls let me know how how to do this?

3 Answers 3

43

After searching for some times i found answer myself.

minIcon.setOnMouseClicked(new EventHandler<MouseEvent>() {
    public void handle(MouseEvent me) {
        primaryStage.setIconified(true);
    }
});

This works fine..

3
  • I don't get the solution... a little bit more context would be nice. I am using CSS. How can I implement this minimize action?
    – Airlike
    Jul 21, 2014 at 14:52
  • 6
    ÷1 for your answer, -1 to javafx devs for the less intuitive method name ever made Aug 17, 2017 at 23:37
  • but how to get primaryStage?
    – FearX
    Sep 11, 2019 at 20:09
1

The following code should work:

iconid.setOnMouseClicked( event -> {
  Stage obj = (Stage) iconid.getScene().getWindow();
  obj.setIconified(true);
});

Edit: I'm new here so I didn't know to present my code better. So I'll try my best.

Here's the expln:- Variable meanings- iconid: The fxid of your ImageView element. obj : random stage object that you can declare.

The event ->{} is a lambda function and it reduces my work so I use it often.

What the second line does is it creates a new stage object and equates it to the current stage being shown, which is retrieved using .getScene().getWindow() property. I used the same ImageView element for consistency but you can use any element belonging to the same stage.(e.g. a button from the same window)

Third line is where you call the method setIconified(boolean) [Not the best naming ik, but I think it has to do with 'iconifying' it to the taskbar-turning it into an icon from a window] Setting it to 'true' minimizes your specified window. That's about it.

1
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
    – Tyler2P
    Dec 5, 2020 at 20:01
0

A simple way to close the current stage is:

@FXML
public void onMinimize(){
    ((Stage) mainPage.getScene().getWindow()).setIconified(true);
}

This method is called when a button is pressed. The main focus of this answer is the code block within.

This is a very compact solution. First, we get the window from the main page. Then cast it to a stage. Then we use the .setIconified(true). There is no animation, but it works. The variable mainPage is AnchorPain. I think it would work with any type of pane. I defined it in the class as such:

@FXML
private AnchorPane mainPage;
1
  • make sure to get the spelling of the technical terms correctly. btw: note that public methods don't need the fxml annotation
    – kleopatra
    May 23, 2022 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.