In the SSCCE below, you can see that if you maximize one of the JInternalFrame
s, then it maximizes them both. This only (AFAIK) happens with the "Windows" LookAndFeel
(if you omit the LookAndFeel
code, it works as expected).
To reproduce:
- Run the SSCCE below.
- Maximize one of the
JInternalFrame
s. - Close (X) the one you maximized
- Observe the other one was maximized as well.
Is there any way to stop this behavior? Is this a bug?
SSCCE:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
class Test2 {
public static void main(String[] args) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
JDesktopPane jdp = new JDesktopPane();
jdp.setBackground(Color.gray);
frame.add(jdp);
jdp.setVisible(true);
frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH );
frame.setVisible(true);
JInternalFrame jiffy1 = new JInternalFrame("Jiffy1", true, true, true, true);
jdp.add(jiffy1);
JLabel jiffy1_label = new JLabel("Jiffy1");
jiffy1_label.setHorizontalAlignment(SwingConstants.CENTER);
jiffy1_label.setFont(new Font("Tahoma", 0, 50));
jiffy1.add(jiffy1_label);
jiffy1.setPreferredSize(new Dimension(300,300));
jiffy1.setVisible(true);
jiffy1.pack();
centerJIF(jdp, jiffy1);
JInternalFrame jiffy2 = new JInternalFrame("Jiffy2", true, true, true, true);
jdp.add(jiffy2);
JLabel jiffy2_label = new JLabel("Jiffy2");
jiffy2_label.setHorizontalAlignment(SwingConstants.CENTER);
jiffy2_label.setFont(new Font("Tahoma", 0, 50));
jiffy2.add(jiffy2_label);
jiffy2.setPreferredSize(new Dimension(200,200));
jiffy2.setVisible(true);
jiffy2.pack();
centerJIF(jdp, jiffy2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static void centerJIF(JDesktopPane jdp, JInternalFrame jiffy) {
Dimension desktopSize = jdp.getSize();
Dimension VInternalFrameSize = jiffy.getSize();
int width = (desktopSize.width - VInternalFrameSize.width) / 2;
int height = (desktopSize.height - VInternalFrameSize.height) / 2;
jiffy.setLocation(width, height);
}
}