Java – jdesktoppane resizing

We have one application with two jframes and two jdesktopanes

The problem we encounter is that after moving the internal frame from the first window to the second window, when we resize the first window, the internal frame of the second window is also resized

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

class FirstFrame extends JFrame
{
  JDesktopPane desktopPane = new JDesktopPane();

  SecondFrame secondFrame;

  public FirstFrame(SecondFrame secondFrame)
  {
    this.secondFrame = secondFrame;
    setTitle("FirstFrame example");
    setDefaultCloSEOperation(EXIT_ON_CLOSE);
    add(desktopPane);

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    JMenuItem item = new JMenuItem("Move");

    item.addActionListener(new ActionListener()
    {

      @Override
      public void actionPerformed(ActionEvent actionevent)
      {
        moveFrame();
      }
    });

    menu.add(item);
    menuBar.add(menu);
    setJMenuBar(menuBar);

  }

  public void addAnInternalFrame()
  {
    JInternalFrame frame = new JInternalFrame();
    frame.setTitle("An Internal Frame");

    desktopPane.add(frame);
    frame.setVisible(true);
    frame.setMaximizable(true);
    try
    {
      frame.setSelected(true);
      frame.setMaximum(true);
    }
    catch (PropertyVetoException e)
    {
      e.printStackTrace();
    }

  }

  public void moveFrame()
  {
    JInternalFrame selectedFrame = desktopPane.getSelectedFrame();
    desktopPane.remove(selectedFrame);
    desktopPane.repaint();

    secondFrame.addInternalFrame(selectedFrame);
  }
}

class SecondFrame extends JFrame
{
  JDesktopPane desktopPane = new JDesktopPane();

  public SecondFrame()
  {
    setTitle("SecondFrame example");
    setDefaultCloSEOperation(EXIT_ON_CLOSE);
    add(desktopPane);
  }

  public void addInternalFrame(JInternalFrame frame)
  {
    desktopPane.add(frame);
  }
}

public class DesktopPaneExample
{
  public static void main(String args[]) throws PropertyVetoException
  {

    SecondFrame secondFrame = new SecondFrame();

    FirstFrame firstFrame = new FirstFrame(secondFrame);

    firstFrame.setSize(400,400);
    firstFrame.setLocation(100,100);
    firstFrame.setVisible(true);
    firstFrame.addAnInternalFrame();

    secondFrame.setSize(400,400);
    secondFrame.setLocation(520,100);
    secondFrame.setVisible(true);




  }
}

In the example application above, copy 1) Click Menu File > move 2) resize the first window

Note: this can only be reproduced in Java 1.7 I use jdk1 7.0_ three

Update: add more information

This is not repeatable on Java 1.6 (jdk1.6.0_21)

Solution

This problem is due to the impact of Java 7 on javax swing. plaf. basic. Adjustment of basic internal framework UI implementation

>Java 1.6 code

public void propertyChange(PropertyChangeEvent evt){

if ((frame.getParent() != null) && !componentListenerAdded) {
            f.getParent().addComponentListener(componentListener);
            componentListenerAdded = true;
        } else if ((newValue == null) && componentListenerAdded) {
            if (f.getParent() != null) {
                f.getParent()
                        .removeComponentListener(componentListener);
            }
            componentListenerAdded = false;
        }

>Java 1.7 code

public void propertyChange(PropertyChangeEvent evt){

if ((frame.getParent() != null) && !componentListenerAdded) {
            f.getParent().addComponentListener(componentListener);
            componentListenerAdded = true;
        }

Note: otherwise, the condition has been deleted This is the culprit

I suggest you two options:

>Option one

JInternalFrame selectedFrame = desktopPane.getSelectedFrame();
desktopPane.remove(selectedFrame);
desktopPane.repaint();

secondFrame.updateUI(); // The magic part,less expensive execution.

secondFrame.addInternalFrame(selectedFrame);

>Option 2

You may need to recompile javax using the "else if" condition above swing. plaf. basic. BasicInternalFrameUI. Java and add it to javax. Jar of your rt.jar library swing. plaf. Basic position

I am here http://www.datafilehost.com/d/dfb7238c Java 1.7.0 is attached 0_ Recompile file for 25

Hope this can help!!!

Greetings, nilindra

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>