5YrsLaterDBA
5YrsLaterDBA

Reputation: 34730

how to create a JDialog which can only floating within its parent frame

I want to create a JDialog which can only float within its parent Frame. That is it cannot be dragged out from its parent frame. Any idea? mouse-motion listener?

thanks,

EDIT:

  1. My applicaiton is based on frame not internal frame, so I cannot use JInternalFrame
  2. I need a non-modal dialog so, I cannot use JOptionPane with internal feature.

Upvotes: 0

Views: 404

Answers (2)

AgostinoX
AgostinoX

Reputation: 7683

Ok, you should have specified the "hidden" problem in the initial question.

I've have found a very tricky solution, I don't suggest to use it except from developing a better one, perhaps starting from this. Given an JInternalFrame, provide it with a componentMove listener to inhibit moving it in hidden positions. As far as I've tested it, it has refresh problems (maybe they can be solved) and the stability in extreme case to assess too. Provided "as is" for further improvement, not as a nice piece of software :-)

public void componentMoved(ComponentEvent e) {

                Rectangle r = new Rectangle();

                MyInternalFrame mif = MyInternalFrame.this;
                JDesktopPane dp = mif.getDesktopPane();

                if (mif.getX() + mif.getWidth()> dp.getWidth()) {
                    mif.setLocation(mif.getDesktopPane().getWidth()-mif.getWidth(),mif.getY());
                }

                if (mif.getY() + mif.getHeight()> dp.getHeight()) {
                    mif.setLocation(mif.getX(), mif.getDesktopPane().getHeight()-mif.getHeight());
                }

                if (mif.getX()<0) {
                    mif.setLocation(0, mif.getY());
                }

                if (mif.getY()<0) {
                    mif.setLocation(mif.getX(), 0);
                }

            }

        });

Upvotes: 0

Bala R
Bala R

Reputation: 108937

Use JInternalFrame instead. See this page for an example.

Upvotes: 4

Related Questions