JInternalFrame With JDesktop Pane in Swing


Introduction

In this article we are going to describe how we use the Internal Frame with a Jdesktop pane in Java. And we also descibe what an interanl frame is and jdesktop and where they are used. JDesktop Pane is a container used to create a multiple-document interface or a virtual desktop. And this container is used to add jinternal frame objects. JInternal Frame is a class for displaying a frame like a window. Generally you add internal frames to the desktop pane.The desktop pane, in turn, might be used as the content pane of a JFrame. The desktop pane is an instance of JDesktopPane, which is a subclass of JLayeredPane that has added API for managing multiple overlapping internal frames.

For easily creating an InternalFarme you have to use the following steps.

Step 1 : Importing the necessary package

import javax.swing.JInternalFrame;

import javax.swing.JDesktopPane;

import javax.swing.JMenu;

import javax.swing.JMenuItem;

import javax.swing.JMenuBar;

import javax.swing.JFrame;

import java.awt.event.*;

import java.awt.*;

 

Step 2 : Creating a Class with constructor and generally most everything is defined within the constructor.


public
class TestJInternalFrame extends JFrame

{

JDesktopPane jdp;

static int fcount = 0;
 

Step 3 : Constructor Defining 
 

public TestJInternalFrame()

{

//By using super keyword we pass my sting "JInternalFrame  Test" passing the to parent class constructor
super("JInternalFrame Test");

int inset = 50;

//  here we try to getting your actual screen size by using toolKit class and its method getScreenSize()

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

setBounds(inset, inset, screenSize.width - inset * 2,screenSize.height - inset * 2);
// Add a Window Exit Listener and we create a anonymous class to perform window close operation

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

// creating the object of Desktop pane because its need to adding internal frame

jdp = new JDesktopPane();

createFrame();

setContentPane(jdp);

setJMenuBar(createMenuBar());

jdp.putClientProperty("JDesktopPane.with internal frame", "outline");

}

 

Step 4 : Now we create a menu bar and add a menu item Myframe:


protected
JMenuBar createMenuBar()

{

JMenuBar menuBar = new JMenuBar();

JMenu menu = new JMenu("MyFrame");

menu.setMnemonic(KeyEvent.VK_N);

JMenuItem menuItem = new JMenuItem("New IFrame");

menuItem.setMnemonic(KeyEvent.VK_N);

menuItem.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

createFrame();

}

});

menu.add(menuItem);

menuBar.add(menu);

return menuBar;

}
 

Step 5 : Creating a frame.
 

protected void createFrame()
{

MyInternalFrame frame = new MyInternalFrame();

frame.setVisible(true);

jdp.add(frame);

try

{

frame.setSelected(true);

}

catch (java.beans.PropertyVetoException e)

{

System.out.println(e);          

}

}
 

Step 6 : Main method, and here we create the object of my class


public
static void main(String[] args)
 {

TestJInternalFrame frame = new TestJInternalFrame();

frame.setVisible(true);

}

Step 7 : Now we are creating an internal frame class.

class MyInternalFrame extends JInternalFrame

{

static final int xCordinate = 40, yCordinate = 40;

// constructor defination

public MyInternalFrame()

{

super("Your Internal Frame " + (++fcount)+" is created", true,true,true,true);

setSize(300, 300);

setVisible(true);

setLocation(xCordinate * fcount, yCordinate* fcount);

}

}

}

Complete code
 

import javax.swing.JInternalFrame;

import javax.swing.JDesktopPane;

import javax.swing.JMenu;

import javax.swing.JMenuItem;

import javax.swing.JMenuBar;

import javax.swing.JFrame;

import java.awt.event.*;

import java.awt.*;

public class TestJInternalFrame extends JFrame

{

JDesktopPane jdp;

static int fcount = 0;

public TestJInternalFrame()

{

super("JInternalFrame Usage Demo");

int inset = 50;

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

setBounds(inset, inset, screenSize.width - inset * 2,screenSize.height - inset * 2);

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

jdp = new JDesktopPane();

createFrame();

setContentPane(jdp);

setJMenuBar(createMenuBar());

jdp.putClientProperty("JDesktopPane.dragMode", "outline");

}

protected JMenuBar createMenuBar()

{

JMenuBar menuBar = new JMenuBar();

JMenu menu = new JMenu("Frame");

menu.setMnemonic(KeyEvent.VK_N);

JMenuItem menuItem = new JMenuItem("New IFrame");

menuItem.setMnemonic(KeyEvent.VK_N);

menuItem.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

createFrame();

}

});

menu.add(menuItem);

menuBar.add(menu);

return menuBar;

}

protected void createFrame() {

MyInternalFrame frame = new MyInternalFrame();

frame.setVisible(true);

jdp.add(frame);

try

{

frame.setSelected(true);

}

catch (java.beans.PropertyVetoException e)

{

System.out.println(e);          

}

}

public static void main(String[] args) {

TestJInternalFrame frame = new TestJInternalFrame();

frame.setVisible(true);

}

class MyInternalFrame extends JInternalFrame

{

static final int xCordinate = 40, yCordinate = 40;

public MyInternalFrame()

{

super("Your Internal Frame " + (++fcount)+" is created", true,true,true,true);

setSize(300, 300);

setVisible(true);

setLocation(xCordinate * fcount, yCordinate* fcount);

}

}

}

Output

Sample Cmd output where you run your program.

jinternalcmd.jpg

This is the intial output.

jinternalframe.jpg

Now you click on the frame option of the menu bar and then click new frame.

jinternalframe1.jpg

Using the new frame option you can create n number of frames.

jinternalframe2.jpg

Resources

The Use of JTable in Swing
A Simple Primer to Properties Pane in Expression Blend IDE
Keeping Multiple Panes open in Accordion Web Control
Working With JMenu in Swing

Up Next
    Ebook Download
    View all
    Learn
    View all