Any body please suggest the code How to tell a JDesktopPane
to fill the entire screen of JFrame
in Java Netbeans IDE
.

- 168,117
- 40
- 217
- 433
-
You don't, you tell the `JFrame` to use the correct layout for the component based on your need. Please see [A Visual Guide to Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) – AxelH May 15 '18 at 06:38
1 Answers
- Set the layout of your
JFrame
toBorderLayout
. Add your
JDesktopPane
to theCENTER
area of your JFrame:JFrame f = new JFrame(); f.setBounds(50, 50, 500, 400); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.setLayout(new BorderLayout()); JDesktopPane desktopPane = new JDesktopPane(); f.add(desktopPane, BorderLayout.CENTER); f.setVisible(true);
The magic is coming from how BorderLayout
manages the layout of its children components. Anything is added to the CENTER
area of BorderLayout
will fill as much area as it can get from the its container.
If you want to maximize a JInternalFrame
inside the JDesktopPane
, you should call setMaximum(true)
on it after it's added to the underlying JDesktopPane
:
public class JDesktop {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setBounds(50, 50, 500, 400);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());
JInternalFrame internalFrame1 = new JInternalFrame("Internal Frame 1", true, true, true, true);
internalFrame1.setSize(150, 150);
internalFrame1.setVisible(true);
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.add(internalFrame1);
try {
internalFrame1.setMaximum(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
f.add(desktopPane, BorderLayout.CENTER);
f.setVisible(true);
}
}
Now that you got the idea it's not bad to know about the defaults. The default layout manager for JFrame
is BorderLayout
and when you add anything to a JFrame
without specifying the constraint for the area, it will be added to the CENTER
area. So you can omit these lines in you code:
f.setLayout(new BorderLayout());
and you can add the desktopPane
simply using this line:
f.add(desktopPane);

- 4,297
- 1
- 25
- 43
-
To be fair, `BorderLayout` is the default layout in a `JFrame`. And adding a component in a `BorderLayout` without specifying a constraint will use `BorderLayout.CENTER`. So `JFrame f = new JFrame(); f.add(desktopPane);` is enough. – AxelH May 15 '18 at 06:40
-
@AxelH: Thanks for the hint. I know that as I work with it every day. I wrote those line there just for the emphasis on the concept of layout management in frames or other container and how `BorderLayout.CENTER` constraint will behave. Since the question asks about netbeans tools on any other window builder I just wanted him to go deeper about the concepts. But I will add your tip to my answer to add information about the default. – STaefi May 15 '18 at 06:45