Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem moving a JInternalFrame from one JDesktopPane to another

843806Jun 13 2008 — edited Jun 14 2008
I'm having a problem with moving a JInternalFrame from one JDesktopPane to another... I have demo code below...

There is one fixed frame on each desktop, and one movable frame on the top desktop. Left-click on either desktop pane to swap the movable frame from one desktop pane to the other. After the move, click between the fixed frame and the movable frame, on that JDesktopPane. Repeat moving the frame back and fourth, until the problem appears.

The problem is that the movable JInternalFrame will eventually not be placed on top of the other frame when it is selected with the mouse, but will still become the selected frame.

Anyone have a solution?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Driver extends javax.swing.JFrame
{
    JDesktopPane   leftPane;
    JDesktopPane   rightPane;
    JInternalFrame movableFrame;
    boolean        onLeftSide;

    public Driver()
    {
        super("Layering problem moving JInternalFrame(s) from one JDesktop to another");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(600, 600);

        JSplitPane contentPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true);
        contentPane.setLeftComponent(leftPane = new JDesktopPane());
        contentPane.setRightComponent(rightPane = new JDesktopPane());
        contentPane.setDividerLocation(300);
        setContentPane(contentPane);
        
        onLeftSide = true;
        movableFrame = newFrame("Movable - left-click on desktop pane to swap", 25);       
        leftPane.add(movableFrame);
        leftPane.add(newFrame("Fixed Left", 0));
        rightPane.add(newFrame("Fixed Right", 0));
        
        MouseAdapter mouseListener = new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                if( e.getButton() == MouseEvent.BUTTON1)
                {
                    if (onLeftSide)
                    {
                        leftPane.remove(movableFrame);
                        rightPane.add(movableFrame);
                        onLeftSide = false;
                    } else {
                        rightPane.remove(movableFrame);                        
                        leftPane.add(movableFrame);
                        onLeftSide = true;
                    }
                    leftPane.repaint();
                    rightPane.repaint();
                }
            }
        };
        leftPane.addMouseListener(mouseListener);
        rightPane.addMouseListener(mouseListener);
               
        setVisible(true);
    }

    JInternalFrame newFrame(String title, int offset)
    {
        JInternalFrame frame = new JInternalFrame(title, true, true, true, true);
        frame.setSize(300, 200);
        frame.setLocation(offset, offset);
        frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
        frame.setVisible(true);
        return frame;
    }
}
Edited by: JavaIsGreat on Jun 13, 2008 5:40 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 12 2008
Added on Jun 13 2008
2 comments
203 views