ted
ted

Reputation: 4975

Dialog with resize minimize or Frame depending on parent

I would like to have a window that behaves like a Dialog, in the terms, that it closes with the parent window, however it shall behave like a normal Frame, especially it shall have the maximize/restore button. How can I create windows, that are bound to a parent window (they close when the parent is closed) and inherit some properties, i.e. the windowicon?

The best I can think off is writing my own class wich wraps a JFrame and takes a parent. This class installs a Listener to the parent and keeps track of all its instances, so it can close all instances when the parent is closed. Exit_on_close can not be used for the parent, since there is a rest of the application which is supposed to keep running.

So is there a an easy way, or do I have to roll my own class?

Upvotes: 0

Views: 1187

Answers (1)

Mikle Garin
Mikle Garin

Reputation: 10143

You can copy almost any JDialog behavior except its positioning on top of JFrame (there was some native solution for that case for Win platform but its a bad thing to use... really).

Here is an example of what you can do in just a few minutes:

ChildFrameTest.java

public class ChildFrameTest
{
    public static void main ( String[] args )
    {
        JFrame application = new JFrame ();
        application.setSize ( 600, 600 );
        application.setLocationRelativeTo ( null );
        application.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );

        JChildFrame tool = new JChildFrame ( application );
        tool.setModalExclusionType ( Dialog.ModalExclusionType.APPLICATION_EXCLUDE );
        tool.setSize ( 100, 600 );
        tool.setLocation ( application.getX () + application.getWidth (), application.getY () );

        new WindowFollowListener ( tool, application );

        application.setVisible ( true );
        tool.setVisible ( true );
    }

    public static class JChildFrame extends JFrame
    {
        public JChildFrame ( JFrame parent )
        {
            super ();
            parent.addWindowListener ( new WindowAdapter ()
            {
                public void windowClosing ( WindowEvent e )
                {
                    dispose ();
                }
            } );
        }
    }
}

And WindowFollowListener to add some nice child frame behavior:

WindowFollowListener.java

public class WindowFollowListener extends ComponentAdapter
{
    private boolean enabled = true;
    private Window followingWindow;
    private Window parentWindow;
    private Point ll;

    public WindowFollowListener ( Window followingWindow, Window parentWindow )
    {
        super ();

        this.followingWindow = followingWindow;
        this.parentWindow = parentWindow;
        this.ll = parentWindow.getLocation ();

        parentWindow.addComponentListener ( this );
    }

    public boolean isEnabled ()
    {
        return enabled;
    }

    public void setEnabled ( boolean enabled )
    {
        this.enabled = enabled;
    }

    public Window getFollowingWindow ()
    {
        return followingWindow;
    }

    public void setFollowingWindow ( Window followingWindow )
    {
        this.followingWindow = followingWindow;
    }

    public Window getParentWindow ()
    {
        return parentWindow;
    }

    public void setParentWindow ( Window parentWindow )
    {
        this.parentWindow = parentWindow;
    }

    public void componentResized ( ComponentEvent e )
    {
        this.ll = parentWindow.getLocation ();
    }

    public void componentMoved ( ComponentEvent e )
    {
        if ( enabled && followingWindow != null && parentWindow != null )
        {
            Point nl = parentWindow.getLocation ();
            Point fwl = followingWindow.getLocation ();
            followingWindow.setLocation ( fwl.x + nl.x - ll.x, fwl.y + nl.y - ll.y );
            this.ll = nl;
        }
    }
}

Upvotes: 2

Related Questions