Java JDesktopPane
Java JDesktopPane is a part of the Swing library that helps Java Developers to make desktop-like applications, It allows the developers to manage and organize child frames within a parent frame. In this article, we are going to see some constructors, methods, fields, and some examples of JDesktopPane.
Default constructors of JDesktopPane
JDesktopPane(): It is a Default Constructor of JDesktopPane Class, it helps to Create a new JDesktopPane.
Create a new JDesktopPane
JDesktopPane desktopPane = new JDesktopPane();
Methods of the JDesktopPane
Methods |
Description |
---|---|
add(JInternalFrame frame) |
Method for Adds a JInternalFrame(Child) to the JDesktopPane(Parent). |
remove(JInternalFrame frame) |
Method for Removes a JInternalFrame from the JDesktopPane |
getAllFrames() |
Method for Returns an array of all the internal frames within the JDesktopPane. |
getSelectedFrame() |
Method for Returning the currently selected internal frame. |
Fields for Java JFrame
Fields |
Description |
---|---|
DEFAULT_DRAG_MODE |
It is a constant, that represents the default drag mode for internal frames. |
LIVE_DRAG_MODE |
This constant indicates that when an internal frame is dragged within the JDesktopPane, the entire contents of the item should be visible during the drag operation. |
OUTLINE_DRAG_MODE |
This constant indicates that, during the drag operation, only an outline or a frame-like representation of the item being dragged should appear inside the JDesktopPane. |
IS_CLOSED_PROPERTY |
It is a Constant,that is used for check the internal frame is close or not. |
Following are the programs to implement JDesktopPane
1. Java Program to demonstrate a simple JDesktopPane
Below is the implementation of the JDesktopPane:
Java
// Java Program to demonstrate // Simple JDesktopPane import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; // Driver Class public class JDesktopPaneExample { // main function public static void main(String[] args) { // Create and show the main application frame SwingUtilities.invokeLater(() -> { JFrame parentframe = new JFrame( "JDesktopPane Example" ); parentframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); parentframe.setSize( 800 , 600 ); // Create a JDesktopPane to manage internal frames JDesktopPane desktopPane = new JDesktopPane(); parentframe.add(desktopPane, BorderLayout.CENTER); // Create and add two internal frames createInternalFrame(desktopPane, "Child Frame 1" ); createInternalFrame(desktopPane, "Child Frame 2" ); // Display the main application frame parentframe.setVisible( true ); }); } private static void createInternalFrame(JDesktopPane desktopPane, String title) { // Create a new internal frame JInternalFrame internalFrame = new JInternalFrame(title, true , true , true , true ); internalFrame.setBounds( 50 , 50 , 300 , 200 ); // Add a text area with the frame's title as content JTextArea textArea = new JTextArea(title); internalFrame.add(textArea); // Create a "Close" button to dispose of the internal frame JButton closeButton = new JButton( "Close" ); closeButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Dispose of the internal frame when the "Close" button is clicked internalFrame.dispose(); } }); // Create a panel for the "Close" button and add it to the internal frame JPanel buttonPanel = new JPanel(); buttonPanel.add(closeButton); internalFrame.add(buttonPanel, BorderLayout.SOUTH); // Add the internal frame to the JDesktopPane and make it visible desktopPane.add(internalFrame); internalFrame.setVisible( true ); } } |
Output :
2. Java Program to Demonstrate the IS_CLOSED_PROPERTY Field in JDesktopPane
Below is the implementation of the JDesktopPane:
Java
// Java Program to Demonstrate // IS_CLOSED_PROPERTY Field in JDesktopPane import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; public class JDesktopPaneExample { public static void main(String[] args) { // Create and display the main application frame SwingUtilities.invokeLater(() -> { JFrame parentFrame = new JFrame( "JDesktopPane Example" ); parentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); parentFrame.setSize( 800 , 600 ); // Create a JDesktopPane to manage internal frames JDesktopPane desktopPane = new JDesktopPane(); parentFrame.add(desktopPane, BorderLayout.CENTER); // Create and add an internal frame JInternalFrame internalFrame = createInternalFrame(desktopPane, "Frame 1" ); desktopPane.add(internalFrame); // Add a PropertyChangeListener to the internal frame to detect closure internalFrame.addPropertyChangeListener(JInternalFrame.IS_CLOSED_PROPERTY, new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { boolean isClosed = (Boolean) evt.getNewValue(); if (isClosed) { System.out.println( "Internal frame is closed." ); } else { System.out.println( "Internal frame is not closed." ); } } }); // Display the main application frame parentFrame.setVisible( true ); }); } private static JInternalFrame createInternalFrame(JDesktopPane desktopPane, String title) { // Create a new internal frame JInternalFrame internalFrame = new JInternalFrame(title, true , true , true , true ); internalFrame.setBounds( 50 , 50 , 300 , 200 ); // Add a text area to the internal frame JTextArea textArea = new JTextArea(title); internalFrame.add(textArea); // Create a "Close" button to dispose of the internal frame JButton closeButton = new JButton( "Close" ); closeButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { internalFrame.dispose(); } }); // Create a panel for the "Close" button and add it to the internal frame JPanel buttonPanel = new JPanel(); buttonPanel.add(closeButton); internalFrame.add(buttonPanel, BorderLayout.SOUTH); // Make the internal frame visible internalFrame.setVisible( true ); return internalFrame; } } |
Contact Us