Grumblesaurus
Grumblesaurus

Reputation: 3119

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 ();
    }
}

Upvotes: 7

Views: 1699

Answers (1)

Matt
Matt

Reputation: 3187

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() );

Upvotes: 1

Related Questions