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;
    }
}


Output:

If We Close the Internal Frame in the Pane then it will report to terminal.



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.

Similar Reads

Default constructors of JDesktopPane

JDesktopPane(): It is a Default Constructor of JDesktopPane Class, it helps to Create a new JDesktopPane....

Following are the programs to implement JDesktopPane

1. Java Program to demonstrate a simple JDesktopPane...

Contact Us