lundi 20 avril 2015

Creating a Moveable Maximized Window on Windows 7 with Java

I'm looking to create a Window that will be remain maximized, but can be dragged to other screens (where it will maximize again). My current implementation fails at doing this.

DESIRED BEHAVIOR:

  • Window Starts maximized
  • Drag window to different monitor
  • Window maximizes on that monitor

ACTUAL BEHAVIOR:

  • Window starts maximized
  • Drag window to different monitor
  • Window remains in unmaximized state (though, oddly enough, based on the window icons in the top-right, it looks as if it "thinks" it is maximized)
  • If I double-click the window to attempt to maximize it (which I don't want to have to do; this is merely an extended observation), it maximizes on the ORIGINAL screen (not the one it's currently on).

Anyone have any ideas?

SSCCE:

import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class ComponentMovedTest extends JPanel
{
    private ComponentMovedTest()
    {
        final JFrame frame = new JFrame("ComponentMovedTest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // FIXME: Naive attempt at ensuring the window becomes maximized
        // on the screen it moves to after moving finishes.
        frame.addComponentListener( new ComponentAdapter()
        {
            @Override
            public void componentMoved( ComponentEvent e )
            {
                frame.setExtendedState( JFrame.MAXIMIZED_BOTH );
            }
        });

        frame.setExtendedState( JFrame.MAXIMIZED_BOTH );
        frame.setResizable( false );
        frame.setVisible(true);
    }

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

Aucun commentaire:

Enregistrer un commentaire