I Have added JTree
and a JInternalFrame
to JDesktopPane
.
I want to resize the JInternalFrame
but couldn't do it.But I can move the Internalframe inside it.
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.OverlayLayout;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class TableDiff {
public static void main(String[] args) {
JDesktopPane panel = new JDesktopPane() {
// @Override
// public boolean isOptimizedDrawingEnabled() {
// return false;
// }
};
panel.setLayout(new OverlayLayout(panel));
JTree tree = new JTree();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
for (int i = 0; i < 10; i++) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(i + "x");
for (int j = 0; j < 5; j++) {
node.add(new DefaultMutableTreeNode(i + "x"));
}
root.add(node);
}
tree.setModel(new DefaultTreeModel(root));
JScrollPane tscrollpane = new JScrollPane(tree);
JInternalFrame iFrame = new JInternalFrame("asd");
iFrame.setClosable(true);
iFrame.setMaximizable(true);
iFrame.setResizable(true);
iFrame.setIconifiable(true);
iFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
// tscrollpane.setAlignmentX(1.0f);
// tscrollpane.setAlignmentY(0.0f);
// iFrame.setAlignmentX(0.0f);
// iFrame.setAlignmentY(0.0f);
panel.add(tscrollpane);
panel.add(iFrame);
iFrame.setVisible(true);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
}
Your problem is the use of an OverlayLayout
.
Simply remove the following line:
panel.setLayout(new OverlayLayout(panel));
Also, very importantly, do not forget to set the preferred size of your JFrame
and JInternalFrame
!
As per the Oracle documentation for using Internal Frames with Java Swing (link):
You must set the size of the internal frame.
If you do not set the size of the internal frame, it will have zero size and thus never be visible. You can set the size using one of the following methods:setSize
,pack
, orsetBounds
.