Introduction to JDesktopPane
JDesktopPane is a class available in the JavaFX that can be used for generating multi-documented interfaces, which can hold many windows or application pages. It is achieved by making use of the contentPane and JInternalFrame from the main window and internal window respectively.
Syntax:
JDesktopPane jdp = new JDesktopPane();
Where, JDesktopPane() acts as the constructor method used for making a new JDesktopPane.
Other methods used from the JDesktopPane are getUI(), remove(int i), removeAll(), getDragMode(), getAllFrame(), getUIClassID(), UpdateUI(), paramString(), setDragMode(int dm), selectFrame(Boolean fwd), etc.
Constructor:
It has only one constructor JDesktopPane() that helps in creating a new JDesktopPane.
Methods
It has numerous methods that perform several functionalities. Following are the commonly used methods of JDesktopPane:
- getAllFrames(): JInternalFrames that is presently displayed in the desktop will be returned.
- getAllFramesInLayer(int l): JInternalFrames that is presently displayed in the desktop on the specified layer l will be returned.
- getDesktopManager(): Desktop manager that manages User Interface actions that are desktop specific will be returned.
- getAccessibleContext(): AccessibleContext that is related to the JDesktopPane will be returned.
- getUI(): Look and Feel object that renders the component will be returned.
- remove(int i): Indexed component i will be removed from this pane.
- removeAll(): Each and every component from the container will be removed.
- getDragMode(): The style of dragging will be returned which is currently used by the desktop pane.
- getSelectedFrame(): JInternalFrame will be returned which is currently active in the desktop pane. If no active JInternalFrame is present, then null will be returned.
- setUI(DesktopPaneUIuserinterface): Look and Feel object that renders the component will be set.
- setDragMode(int dm): Dragging style dm will be set in the desktop pane.
- remove(Componentcmp): Component cmp from the container will be removed.
- selectFrame(boolean fwd): JInternalFrame will be selected which is next in this desktop pane.
- setSelectedFrame(JInternalFramejf): JInternalFrame will be set which is currently active in the desktop pane.
- getUIClassID(): Name of the Look and Feel object that renders the component will be returned.
- updateUI(): A notification that Looks and Feels object has changed will be updated from UIManager.
- setDesktopManager(DesktopManagerd): DesktopManager will be set that handles User Interface actions of the desktop.
- paramString( ): String representation will be returned for the JDesktopPane.
- addImpl(Componentcmp, Object constr, int i): Specified component cmp will be added to the container at the index i.
Program to Implement JDesktopPane
Now, let us see different JavaFX programs:
Program #1 – Create JDesktopPane with 4 windows
Code:
// Java program to create JDesktopPane
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
// Class that extends JFrame class
public class JDesktopPaneExample extends JFrame
{
// constructor of the created class
public JDesktopPaneExample()
{
// create a desktoppane dp
CustomDesktopPane dp = new CustomDesktopPane();
// create a container
Container cp = getContentPane();
// add the desktoppane to the container
cp.add(dp, BorderLayout.CENTER);
// display the pane
dp.display(dp);
// set a title
setTitle("Example for JDesktopPane");
// set the size
setSize(350,400);
// set visibility as true
setVisible(true);
}
// main method
public static void main(String args[])
{
// call the constructor
new JDesktopPaneExample();
}
}
class CustomDesktopPane extends JDesktopPane
{
//no of frames
int n = 4;
//set the value for x and y
int x = 40, y = 40;
//definition of the function display
public void display(CustomDesktopPane dp)
{
//loop to create n number of frames
for(int i = 0; i < n ; ++i )
{
//create a JInternalFrame
JInternalFrame jf = new JInternalFrame("JInternal Frame no : " + i , true, true, true, true);
// set bounds for the frame
jf.setBounds(x, y, 250, 60);
//create container
Container cnt = jf.getContentPane( ) ;
// add the label to the container
cnt.add(new JLabel("Sample label for demonstration "));
//add the JInternalFrame to the desktoppane
dp.add( jf );
// set visibility as true
jf.setVisible(true);
// increment the value of y
y += 85;
}
}
}
Output:
Here, a pane is displayed with 4 internal frames with 4 labels for demonstration. Suppose JInternalFrame no:0 is maximized, the result will be as shown below:
Similarly, all other windows can also be maximized like the above one.
Program #2 – Create with 1 internal frame
Code:
//Java program to create JDesktopPane with one internal fr
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
//class
public class JDesktopPaneExample {
//main method
public static void main(final String[] args) {
//create a JFrame
JFrame fr = new JFrame();
//window close when close button when it is clicked
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create a JDesktopPane
JDesktopPane dp = new JDesktopPane();
// create an internal frame
JInternalFrame iframe = new JInternalFrame("Sample frame for demo purpose", true, true, true, true);
// add internal frame to desktopPane
dp.add(iframe);
// set the bounds for internal frame
iframe.setBounds(60, 100, 500, 260);
// create a label
JLabel lb = new JLabel(iframe.getTitle(), JLabel.CENTER);
// create a button
JButton btn = new JButton("Button for demo purpose");
// add label to the internal frame
iframe.add(lb, BorderLayout.EAST);
// add button to the internal frame
iframe.add(btn, BorderLayout.WEST);
// set the visibility of internal frame as true
iframe.setVisible(true);
fr.add(dp, BorderLayout.CENTER);
// set the size of frame
fr.setSize(650, 550);
// set the visibility of frame as true
fr.setVisible(true);
}
}
Output:
On executing the code, a frame will be displayed with a button and a label inside it.
Conclusion
JDesktopPane is used in multi-document interfaces or applications which contain several windows in it. Many of the products are using this pane for their commercial purpose. The main products include Microsoft PowerPoint, Allaire HomeSite, Corel Draw, and Symantec Visual Café. In all these products, the main desktop pane will be present that contains all other windows. In order to use this pane, instantiate the class javafx.swing.JDesktopPane.
Recommended Articles
This is a guide to JDesktopPane. Here we discuss the syntax, constructors, used methods and different examples of JDesktopPane. You may also look at the following articles to learn more –