-
Bug
-
Resolution: Cannot Reproduce
-
P4
-
14, 15
-
x86_64
-
windows_10
A DESCRIPTION OF THE PROBLEM :
There is an issue with JInternalFrame packing itself too tightly if setting the size or calling pack() prior to adding it to a JDesktopPane. This occurs under all look and feels as demonstrated by the SSCCE and it occurs in the official "How to Use Internal Frames" demo.
https://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html
Likely the frame borders or some part of the look and feel must not be incorporated in the preferred size. In the SSCCE we can see that the initial internal frame only shows 6 labels, while new ones created from the menu will show 7. If we comment out the call to pack after adding the JInternalFrame to the JDesktopPane, then none of them work and all pack too small only showing 6 labels.
This has also been discussed quite a bit on the internet and is sort of a known unreported issue:
https://stackoverflow.com/questions/22215145/content-of-jinternalframe-packed-too-tightly
http://jfpoilpret.blogspot.com/2009/02/jinternalframe-lesson-learned.html
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just call pack() on a JInternalFrame prior to adding to a JDesktopPane, just as the official JInternalFrame tutorial says to do. Be sure to actually add content to the root pane like a few labels or anything at all really.
https://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
That pack() would produce the same result/dimensions for the JInternalFrame regardless of whether it is called before or after adding to JDesktopPane.
ACTUAL -
The results of pack() are only correct if called after adding a JInternalFrame to a JDesktopPane.
---------- BEGIN SOURCE ----------
/*
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle or the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
import java.awt.event.*;
import java.awt.*;
public class InternalFrameDemo extends JFrame
implements ActionListener {
JDesktopPane desktop;
private static class MyInternalFrame extends JInternalFrame {
static int openFrameCount = 0;
static final int xOffset = 30, yOffset = 30;
public MyInternalFrame() {
super("Document #" + (++openFrameCount),
true, //resizable
true, //closable
true, //maximizable
true);//iconifiable
this.setLayout(new FlowLayout());
//...Create the GUI and put it in the window...
add(new JLabel("Helloooooooooooooooooooooooothere!"));
add(new JLabel("Hello!"));
add(new JLabel("Hello!"));
add(new JLabel("Hello!"));
add(new JLabel("Hello!"));
add(new JLabel("Hello!"));
add(new JLabel("Hello!")); // BUG the initial window puts this one in a second row
//...Then set the window size or call pack...
pack(); // BUG calling pack before adding to JDesktopPane makes TOO SMALL
//Set the window's location.
setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
}
}
public InternalFrameDemo() {
super("InternalFrameDemo");
this.setSize(800, 600);
this.setLocationRelativeTo(null);
//Set up the GUI.
desktop = new JDesktopPane(); //a specialized layered pane
createFrame(); //create first "window" BUG only shows 6 labels!
setContentPane(desktop);
setJMenuBar(createMenuBar());
}
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
//Set up the lone menu.
JMenu menu = new JMenu("Document");
menu.setMnemonic(KeyEvent.VK_D);
menuBar.add(menu);
//Set up the first menu item.
JMenuItem menuItem = new JMenuItem("New");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_N, ActionEvent.ALT_MASK));
menuItem.setActionCommand("new");
menuItem.addActionListener(this);
menu.add(menuItem);
//Set up the second menu item.
menuItem = new JMenuItem("Quit");
menuItem.setMnemonic(KeyEvent.VK_Q);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_Q, ActionEvent.ALT_MASK));
menuItem.setActionCommand("quit");
menuItem.addActionListener(this);
menu.add(menuItem);
return menuBar;
}
//React to menu selections.
public void actionPerformed(ActionEvent e) {
if ("new".equals(e.getActionCommand())) { //new
createFrame();
} else { //quit
quit();
}
}
//Create a new internal frame.
protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true); //necessary as of 1.3
desktop.add(frame);
frame.pack(); // <- These ones show all 7 labels!
// Comment it out and they only show 6 like the initial window!
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}
}
//Quit the application.
protected void quit() {
System.exit(0);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
InternalFrameDemo frame = new InternalFrameDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window.
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
You can always call pack() after adding the frame to a JDesktopPane but that may be difficult to do in existing applications like the one I am trying to address this for.
FREQUENCY : always
There is an issue with JInternalFrame packing itself too tightly if setting the size or calling pack() prior to adding it to a JDesktopPane. This occurs under all look and feels as demonstrated by the SSCCE and it occurs in the official "How to Use Internal Frames" demo.
https://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html
Likely the frame borders or some part of the look and feel must not be incorporated in the preferred size. In the SSCCE we can see that the initial internal frame only shows 6 labels, while new ones created from the menu will show 7. If we comment out the call to pack after adding the JInternalFrame to the JDesktopPane, then none of them work and all pack too small only showing 6 labels.
This has also been discussed quite a bit on the internet and is sort of a known unreported issue:
https://stackoverflow.com/questions/22215145/content-of-jinternalframe-packed-too-tightly
http://jfpoilpret.blogspot.com/2009/02/jinternalframe-lesson-learned.html
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just call pack() on a JInternalFrame prior to adding to a JDesktopPane, just as the official JInternalFrame tutorial says to do. Be sure to actually add content to the root pane like a few labels or anything at all really.
https://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
That pack() would produce the same result/dimensions for the JInternalFrame regardless of whether it is called before or after adding to JDesktopPane.
ACTUAL -
The results of pack() are only correct if called after adding a JInternalFrame to a JDesktopPane.
---------- BEGIN SOURCE ----------
/*
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle or the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
import java.awt.event.*;
import java.awt.*;
public class InternalFrameDemo extends JFrame
implements ActionListener {
JDesktopPane desktop;
private static class MyInternalFrame extends JInternalFrame {
static int openFrameCount = 0;
static final int xOffset = 30, yOffset = 30;
public MyInternalFrame() {
super("Document #" + (++openFrameCount),
true, //resizable
true, //closable
true, //maximizable
true);//iconifiable
this.setLayout(new FlowLayout());
//...Create the GUI and put it in the window...
add(new JLabel("Helloooooooooooooooooooooooothere!"));
add(new JLabel("Hello!"));
add(new JLabel("Hello!"));
add(new JLabel("Hello!"));
add(new JLabel("Hello!"));
add(new JLabel("Hello!"));
add(new JLabel("Hello!")); // BUG the initial window puts this one in a second row
//...Then set the window size or call pack...
pack(); // BUG calling pack before adding to JDesktopPane makes TOO SMALL
//Set the window's location.
setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
}
}
public InternalFrameDemo() {
super("InternalFrameDemo");
this.setSize(800, 600);
this.setLocationRelativeTo(null);
//Set up the GUI.
desktop = new JDesktopPane(); //a specialized layered pane
createFrame(); //create first "window" BUG only shows 6 labels!
setContentPane(desktop);
setJMenuBar(createMenuBar());
}
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
//Set up the lone menu.
JMenu menu = new JMenu("Document");
menu.setMnemonic(KeyEvent.VK_D);
menuBar.add(menu);
//Set up the first menu item.
JMenuItem menuItem = new JMenuItem("New");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_N, ActionEvent.ALT_MASK));
menuItem.setActionCommand("new");
menuItem.addActionListener(this);
menu.add(menuItem);
//Set up the second menu item.
menuItem = new JMenuItem("Quit");
menuItem.setMnemonic(KeyEvent.VK_Q);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_Q, ActionEvent.ALT_MASK));
menuItem.setActionCommand("quit");
menuItem.addActionListener(this);
menu.add(menuItem);
return menuBar;
}
//React to menu selections.
public void actionPerformed(ActionEvent e) {
if ("new".equals(e.getActionCommand())) { //new
createFrame();
} else { //quit
quit();
}
}
//Create a new internal frame.
protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true); //necessary as of 1.3
desktop.add(frame);
frame.pack(); // <- These ones show all 7 labels!
// Comment it out and they only show 6 like the initial window!
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}
}
//Quit the application.
protected void quit() {
System.exit(0);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
InternalFrameDemo frame = new InternalFrameDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window.
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
You can always call pack() after adding the frame to a JDesktopPane but that may be difficult to do in existing applications like the one I am trying to address this for.
FREQUENCY : always