Java

JDeskTopPane and JInternalFrame in java with Example using NetBeans

JDesktopPane and JIntenalFrame in Java:

The JDesktopPane is a container primarily used to hold multiple child windows. These sub-windows are mostly realized by JInternalFrame, which we will discuss in more detail in below. The combination of JDesktopPane and JInternalFrames makes it possible to implement a so-called Multiple Document Interface (MDI). This means that multiple documents can be viewed and operated from one main window. The documents are each displayed in a JIntermalFrame. As is known from Windows, the individual frames can be moved, reduced, enlarged, or closed.

The JDesktopPane class has only the default parameterless constructor.


Example: how to add a JDesktopPane to dialog:

package com.mycompany.guiexamples;
import  java.awt.Color ; 
import  javax.swing.* ;


/**
 *
 * @author Fawadkhan
 */
public class JDesktopPaneExample {
    
        // main method 
    public  static  void main ( String [ ] args ) 
    { 
        // create new dialog
        JDialog myJDialog =  new  JDialog ( ) ; 
        myJDialog. setTitle ( "JDesktopPane Testing example" ) ; 
        myJDialog. setSize ( 450 , 300 ) ;
 
        // JDesktopPane is created 
        JDesktopPane deskPane =  new  JDesktopPane ( ) ;
 
        // Background color is set to green 
        deskPane. setBackground ( Color.GREEN) ;
 
        // JDesktopPane is added to the dialog 
        myJDialog. add ( deskPane ) ;
 
        // We display our dialog 
        myJDialog. setVisible ( true ) ;
 
    } 

}

We set the background color of the JDesktopPane to green so that you can see that it is actually added and displayed:

JDeskTopPane



In addition to the methods known from other components, JDesktopPane has a few of its own. The following are important to manage the frames arranged on the JDesktopPane:

method short description
JInternalFrame [ ]

getAllFrames()

This method returns all instances of JInternalFrame placed on the desktop in an array.
JInternalFrame getSelectedFrame() This method returns the currently active JInternalFrame.
void removeAll() This method removes all objects added to the  JDesktopPane.
void setSelectedFrame(

JInternalFrame f)

With this method, the transferred JInternalFrame is selected and placed in the foreground.


JInternalFrame in java

A JInternalFrame is similar to a JFrame. The difference is that it is an internal window that depends on the main window. The JInternalFrame class provides the following constructors:

constructor short description
JInternalFrame() The default constructor creates a JInternalFrame that is not resizable, closable, maximizable, or collapsible and has no title.
JInternalFrame(

String title)

The title that the JinternalFrame should have is passed here.
JInternalFrame(

String title, boolean resizable)

This constructor can be used to specify whether the internal window can be enlarged or reduced.
JInternalFrame(

String title, boolean resizable, boolean closable)

The constructor has an additional parameter that determines whether the frame can be closed.
JInternalFrame(

String title, boolean resizable, boolean closable, boolean maximizable)

This constructor can also be used to set the maximizable.
JInternalFrame(

String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable)

This constructor can also be used to set the minimizable.


Below are some important methods that are often needed when dealing with JInternalFrames:

method short description
public void toBack() The JInternalFrame is set to the back.
public void toFront() The frame is set to the front.
public boolean isSelected() Returns true if the frame is currently active.
public void show() Makes the window visible if it isn’t already and brings it to the front.
public void hide() The window is hidden.
JDesktopPane getDesktopPane() Returns the JDesktopPane that the frame is on


Example: how to use JDesktopPane and JInternalFrame in Java:

package com.mycompany.guiexamples;

import java.awt.Color;
import javax.swing.*;

/**
 *
 * @author Fawadkhan
 */
public class JDesktopPaneExample {

    // main method 
    public static void main(String[] args) {
        // create new dialog
        JDialog myJDialog = new JDialog();
        myJDialog.setTitle("JDesktopPane Testing example");
        myJDialog.setSize(450, 300);

        // JDesktopPane is created 
        JDesktopPane deskPane = new JDesktopPane();

        // Background color is set to green 
        deskPane.setBackground(Color.GREEN);

        //JInternalFrame is created 
        JInternalFrame internalFrame1 = new JInternalFrame("Window 1", true,
                true, true, true);
        JInternalFrame internalFrame2 = new JInternalFrame("Window 2");

        //JInternalFrames are added to our JDesktopPane 
        deskPane.add(internalFrame1);
        deskPane.add(internalFrame2);

        //Size of the JInternalFrames is set 
        internalFrame1.setSize(200, 200);
        internalFrame2.setSize(200, 200);

        //The position of the JInternalFrames is set 
        internalFrame1.setLocation(0, 0);
        internalFrame2.setLocation(200, 0);

        //JInternalFrames are made visible 
        internalFrame1.show();
        internalFrame2.show();

        // JDesktopPane is added to the dialog 
        myJDialog.add(deskPane);

        // We display our dialog 
        myJDialog.setVisible(true);

    }

} 

when you execute the above code you will get the following output:

JDeskTopPane

first, I create a JDesktopPane that I color green. Then I create two JInternalFrames. For the first frame, we used the constructor with the most parameters. Since we have set all properties to true, the window can be resized, minimized, maximized, and closed.

To create the second JInternalFrame, I used the default parameterless constructor. Since these properties are set to false by default, the frame can only be moved.

I have assigned a fixed position to the windows for better demonstration since they would otherwise overlap. In order for them to be displayed, they must be explicitly made visible using show() or setVisible(true).

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button