Introduction
JDesktopPane is a container used to create a multiple-document interface or a virtual desktop. JInternalFrame objects can be created and added to the JDesktopPane. JDesktopPane extends JLayeredPane to manage the potentially overlapping internal frames. A JDesktopPane object owns DesktopManager objects. They are responsible for implementing L&F-specific behaviors for the JDesktopPane.
Importance and Function of JDesktopPane
It also maintains a reference to an instance of DesktopManager that is set by the UI class for the current Look and Feel (L&F). This class is normally used as the parent of JInternalFrames to provide a pluggable DesktopManager object to the JInternalFrames. The installUI of the L&F specific implementation is responsible for setting the desktopManager variable appropriately. When the parent of a JInternalFrame is a JDesktopPane, it should delegate most of its behavior to the desktop manager (closing, resizing, etc).
The constructor JDesktopPane() can be used to create a desktop pane.
Methods used
Methods |
Description |
getAllFrames() |
Returns all JInternalFrames currently displayed on the desktop. |
getAllFramesInLayer(int Layer) |
Returns all JInternalFrames currently displayed in the specified layer of the desktop. |
getDesktopManager() |
Returns the DesktopManager that handles desktop-specific UI actions. |
setDesktopManager(DesktopManager d) |
Sets the DesktopManager that will handle desktop-specific UI actions |
An example illustrates using the JDesktopPane with multiple JInternalFrame windows.
import javax.swing.*;
public class DesktopPaneExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JDesktopPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
JDesktopPane desktopPane = new JDesktopPane();
for (int i = 1; i <= 3; i++) {
JInternalFrame internalFrame = new JInternalFrame(
"Internal Frame " + i, true, true, true, true
);
internalFrame.setBounds(50 * i, 50 * i, 300, 200);
internalFrame.setVisible(true);
desktopPane.add(internalFrame);
}
frame.add(desktopPane);
frame.setVisible(true);
}
}
Output
![Internal Frame]()
Code Explanation
This program illustrates the use of the JDesktopPane class in Java Swing to create a Multiple Document Interface (MDI) environment. In this example, a JFrame is created as the main window, and a JDesktopPane is added to it. The JDesktopPane serves as a virtual desktop that can contain multiple internal frames. Inside a loop, three JInternalFrame objects are created, each with its title and position. These internal frames are configured to be resizable, closable, maximizable, and iconifiable, and then added to the JDesktopPane. Each frame is also made visible. When the application runs, the user sees a main window containing three smaller, movable sub-windows, just like the windows seen in traditional desktop applications. This approach allows developers to manage multiple child windows within a single parent window, offering a flexible user interface design.
DesktopManager Interface
A JDesktopPane object owns DesktopManager objects. They are responsible for implementing L&F-specific behaviors for the JDesktopPane. JInternalFrame implementations should delegate specific behaviors to the DesktopManager.
Methods used
Methods |
Description |
maximizeFrame(JInternalFrame f) |
Generally, the frame should be resized to match its parents' bounds. |
minimizeFrame(JInternalFrame f) |
Generally, this indicates that the frame should be restored to its size and position before a maximizeFrame() call. |
ActiveFrame(JInternalFrame f) |
Generally, indicate that this frame has focus. |
endResizingFrame(JComponent f) |
This method signals the end of the resize session. |
endDraggingFrame(JComponent f) |
This method signals the end of the dragging session. |
dragFrame(JComponent f, int newX, int newY) |
The user has moved the frame. |
deiconifyFrame(JInternalFrame f) |
Generally, remove any iconic representation that is present and restore the frame to its original size and location. |
deactivateFrame(JInternalFrame f) |
Generally, indicate that this frame has lost focus. |
beginDragggingFrame(JComponent f) |
This method is normally called when the user has indicated that they will begin dragging a component around. |
closeFrame(JIntenalFrame f) |
Generally, this call should remove the frame from its parent. |
beginResizingFrame(JComponent f, int direction) |
This method is normally called when the user has indicated that they will begin resizing the frame. |
An example illustrates using a custom DesktopManager to track frame actions.
import javax.swing.*;
import javax.swing.plaf.DesktopPaneUI;
import javax.swing.plaf.basic.BasicDesktopPaneUI;
class CustomDesktopManager extends DefaultDesktopManager {
@Override
public void closeFrame(JInternalFrame f) {
System.out.println("Closing frame: " + f.getTitle());
super.closeFrame(f);
}
@Override
public void activateFrame(JInternalFrame f) {
System.out.println("Activating frame: " + f.getTitle());
super.activateFrame(f);
}
@Override
public void beginDraggingFrame(JComponent f) {
System.out.println("Started dragging: " + ((JInternalFrame) f).getTitle());
super.beginDraggingFrame(f);
}
@Override
public void endDraggingFrame(JComponent f) {
System.out.println("Ended dragging: " + ((JInternalFrame) f).getTitle());
super.endDraggingFrame(f);
}
}
public class DesktopManagerExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Custom DesktopManager Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setDesktopManager(new CustomDesktopManager());
JInternalFrame internalFrame = new JInternalFrame("Managed Frame", true, true, true, true);
internalFrame.setBounds(100, 100, 300, 200);
internalFrame.setVisible(true);
desktopPane.add(internalFrame);
frame.add(desktopPane);
frame.setVisible(true);
}
}
Output
![Desktop manager]()
Code Explanation
This program shows how to use a custom DesktopManager with JDesktopPane in Java Swing. A class named CustomDesktopManager is created by extending DefaultDesktopManager, and several methods like closeFrame(), activateFrame(), beginDraggingFrame(), and endDraggingFrame() are overridden to print messages when actions happen on an internal frame. In the main program, a JFrame is created, and a JDesktopPane is added to it. The custom desktop manager is then set for the JDesktopPane, and one JInternalFrame is added and made visible. When you interact with the internal frame (like moving or closing it), the overridden methods print messages in the console. This shows how developers can control or monitor the behavior of internal frames using the DesktopManager interface.
Another Example illustrates how to manage multiple interactive windows with custom controls using JDesktopPane and DesktopManager in Java Swing.
Source Code
import javax.swing.*;
import javax.swing.plaf.basic.BasicDesktopPaneUI;
class CustomDesktopManager extends DefaultDesktopManager {
@Override
public void activateFrame(JInternalFrame f) {
System.out.println("Activated: " + f.getTitle());
super.activateFrame(f);
}
@Override
public void closeFrame(JInternalFrame f) {
System.out.println("Closed: " + f.getTitle());
super.closeFrame(f);
}
@Override
public void beginDraggingFrame(JComponent f) {
System.out.println("Begin dragging: " + ((JInternalFrame) f).getTitle());
super.beginDraggingFrame(f);
}
@Override
public void endDraggingFrame(JComponent f) {
System.out.println("End dragging: " + ((JInternalFrame) f).getTitle());
super.endDraggingFrame(f);
}
}
public class CombinedDesktopDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("JDesktopPane with Custom DesktopManager");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900, 600);
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setDesktopManager(new CustomDesktopManager());
for (int i = 1; i <= 3; i++) {
JInternalFrame internalFrame = new JInternalFrame("Frame " + i, true, true, true, true);
internalFrame.setBounds(60 * i, 60 * i, 300, 200);
internalFrame.setVisible(true);
desktopPane.add(internalFrame);
}
frame.add(desktopPane);
frame.setVisible(true);
}
}
Output
![Output]()
Code Explanation
This combined program demonstrates how to use JDesktopPane along with a custom DesktopManager to manage multiple internal frames in a Java Swing application. A JFrame is created as the main application window, and inside it, a JDesktopPane serves as the virtual desktop environment. A custom class named CustomDesktopManager extends DefaultDesktopManager and overrides several methods, such as activateFrame(), closeFrame(), beginDraggingFrame(), and endDraggingFrame(), to log user interactions with the internal frames. In the main part of the program, three JInternalFrame objects are created with titles and positions, and each is added to the JDesktopPane and made visible. These frames are resizable, closable, and movable. Whenever a user activates, drags, or closes a frame, the corresponding message is printed to the console through the custom desktop manager. This code effectively shows how to combine multiple internal windows with customized control logic in a single application window.
Summary
The document provides a detailed overview of the JDesktopPane and DesktopManager interface in Java Swing (JFC), highlighting their role in building Multiple Document Interface (MDI) applications. JDesktopPane is a container that allows the use of multiple JInternalFrame objects within a virtual desktop, offering functionality such as layering, resizing, and dynamic window management. The DesktopManager interface is responsible for managing the Look and Feel (L\&F) behavior of internal frames, enabling actions like maximizing, minimizing, dragging, and closing frames. The document includes three Java code examples: the first demonstrates creating and displaying multiple JInternalFrame windows; the second showcases a custom DesktopManager that logs user interactions; and the third combines both features, using a custom DesktopManager within a JDesktopPane containing multiple internal frames. These examples collectively illustrate how developers can design rich, flexible UIs by managing child window behavior and desktop-level interactions using these powerful Swing components.