Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stage.setIconified() and Stage.isIconified() not acting properly

I am trying to write a simple method to toggle whether my window is minimized (i.e. iconified). I am getting strange behavior. Below is runnable code that illustrates the problem.

I am getting the same results on Gnome 3.20.4 and XFCE 4.12. I haven't tested this in any other environment yet.


  1. If the window is not maximized, the code works as expected, but the reported state is incorrect at times. Here is the output from the code below. I have put notes (<--) next to lines where the textual output doesn't match the visual output.

Windows Not Maximized Behavior

Before call  (Note: Window appears iconified)
    isIconified(): true
    isMaximized(): true

Setting iconified to false

After call (Note: Window now appears restored, not maximized) 
    isIconified(): true   <-- The window is visually not iconified 
    isMaximized(): true   <-- The window is visually not maximized


Before call (Note: Window still appears restored, not maximized) 
    isIconified(): false  <-- One second later, the report is accurate
    isMaximized(): false

Setting iconified to true

After call  (Note: Window now appears iconified) 
    isIconified(): true
    isMaximized(): true

  1. If the window is maximized, the code does not work as expected. Instead, it is a three-step process. The first call iconifies, the second call restores, and the third call doesn't make any visual changes.

Windows Maximized Behavior

Before call (Note: Window appears iconified)
    isIconified(): true
    isMaximized(): true

Setting iconified to false

After call (Note: Window appears maximized)
    isIconified(): true <-- Does not match visual
    isMaximized(): true


Before call (Note: Window appears maximized)
    isIconified(): true <-- Does not match visual
    isMaximized(): true

Setting iconified to false

After call (Note: Window appears maximized)
    isIconified(): false
    isMaximized(): false  <-- Does not match visual


Before call (Note: Window appears maximized)
    isIconified(): false
    isMaximized(): false  <-- Does not match visual

Setting iconified to true

After call (Note: Window appears iconified)
    isIconified(): true
    isMaximized(): true

<Now it loops>

Before call (Note: Window appears iconified)
    isIconified(): true
    isMaximized(): true

Setting iconified to false

After call (Note: Window appears maximized)
    isIconified(): true  <-- Does not match visual
    isMaximized(): true

...

Here is the runnable code:


import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;

public class StageTest extends Application {

    Stage stage;

    public static void main ( String[] args ) {
        launch ( args );
    }

    @Override
    public void start ( Stage stage ) throws Exception {
        this.stage = stage;
        stage.setResizable( true );
        stage.show( );

        Thread thread = new Thread ( () -> {
            while ( true ) {
                Platform.runLater( () -> {
                    toggleMinimized();
                } );
                try {
                    Thread.sleep ( 1000 );
                } catch ( InterruptedException e ) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        thread.setDaemon( true );
        thread.start();

    }

    public void toggleMinimized() {

        System.out.println ( "Before call" );
        System.out.println ( "\tisIconified(): " + stage.isIconified() );
        System.out.println ( "\tisMaximized(): " + stage.isIconified() );
        System.out.println ();

        if ( stage.isIconified() ) {
            System.out.println ( "Setting iconified to false" );
            System.out.println ();
            stage.setIconified( false );
        } else {
            System.out.println ( "Setting iconified to true" );
            System.out.println ();
            stage.setIconified( true );
        }


        System.out.println ( "After call" );
        System.out.println ( "\tisIconified(): " + stage.isIconified() );
        System.out.println ( "\tisMaximized(): " + stage.isMaximized() );
        System.out.println ();
        System.out.println ();
    }
}
like image 635
JoshuaD Avatar asked Jul 23 '17 19:07

JoshuaD


People also ask

What method is invoked to display a stage in JavaFX?

Showing a Stage The difference between the JavaFX Stage methods show() and showAndWait() is, that show() makes the Stage visible and the exits the show() method immediately, whereas the showAndWait() shows the Stage object and then blocks (stays inside the showAndWait() method) until the Stage is closed.

How do you set a scene in a stage?

You can add a Scene object to the stage using the method setScene() of the class named Stage.

How would you set the title of the stage primary stage?

setTitle("New Title!"); O primaryStage = "New Title!; O primary Stage.


1 Answers

It seems you have a typo in your program you have 2 stage.isIconified()'s when you should have one

public void toggleMinimized() {
    System.out.println ( "Before call" );
    System.out.println ( "\tisIconified(): " + stage.isIconified() );
    System.out.println ( "\tisMaximized(): " + stage.isIconified() );

It should be

public void toggleMinimized() {
    System.out.println ( "Before call" );
    System.out.println ( "\tisIconified(): " + stage.isIconified() );
    System.out.println ( "\tisMaximized(): " + stage.isMaximized() );
like image 174
Matt Avatar answered Oct 04 '22 12:10

Matt