Sunday, September 11, 2011

Creating a JDesktopPane

Program Description:

One of the interesting and useful part in making Java Program is creating a JDesktopPane which is an MDI (Multiple Document Interface) commonly used in today's applications. It is a main window called parent window that contains other windows called child windows. It is used to manage several open documents that are being processed in parallel. The Java Program below is a short simple code on how to create a JDesktopPane and JInternalFrame.

Output:


Code:

/**
* File: createJDesktoPane.java
* Tiltle: Creating a JDesktopPane
* Author: http://java-code-complete.blogspot.com/
*/

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;
import java.awt.event.*;

public class createJDesktoPane extends JFrame {

//Initializing program components
private JDesktopPane desktopTest;
private JLabel labels[];
private JTextField inputs[];
private JButton buttons[];
private String labelName[]={"Enter Name: ","Enter Age: ","Enter Address: ","Enter Mobile#: "};
private String buttonName[] = {"Open","Save","Exit"};
private JPanel panel1, panel2;

//Setting up GUI
public createJDesktoPane() {

//Setting up the Title of the Window
super("Creating a JDesktopPane");

//Set Size of the Window (WIDTH, HEIGHT)
setSize(600,500);

//Exit Property of the Window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JMenuBar bar = new JMenuBar(); //Constructing JMenuBar
JMenu menu = new JMenu("File"); //Constructing JMenu name "File"
JMenuItem newFile = new JMenuItem("Add New Data"); //Constructing JMenuItem with "Add New Data" label

menu.add(newFile); //Adding JMenuItem in the JMenu
bar.add(menu); //Adding JMenu in the JMenuBar

setJMenuBar(bar); //Adding JMenuBar in the container

desktopTest = new JDesktopPane(); //Creating a JDesktopPane
desktopTest.setBackground(Color.BLACK); //Setting JDesktopPane background color

//Setting up the container ready for the components to be added.
Container pane = getContentPane();
setContentPane(pane);

pane.add(desktopTest); //Adding JDesktopPane in the container

//Implemeting Even-Listener on newFile JMenuItem
newFile.addActionListener(
new ActionListener() {

//Handle JMenuItem "newFile" event if it is clicked
public void actionPerformed(ActionEvent e) {

//Constructing an Internal Frame inside JDesktopPane
JInternalFrame frame = new JInternalFrame(null,true,true,true,true);

Container container = frame.getContentPane(); //Creating a container inside the JInternalFrame

//Constructing JLabel, JButton, and JTextField inside JInternalFrame
labels = new JLabel[4];
inputs = new JTextField[4];
buttons = new JButton[3];

//Creating a JPanel 1 with GridLayout of 4 rows and 2 columns inside JInternalFrame
panel1 = new JPanel();
panel1.setLayout(new GridLayout(4,2));

//Constructing JLabel and JTextField using "for loop" and add to JPanel 1
for(int count=0; count<labels.length && count<inputs.length; count++) {
labels[count] = new JLabel(labelName[count]);
inputs[count] = new JTextField(10);
panel1.add(labels[count]);
panel1.add(inputs[count]);
}

//Creating a JPanel 2 with GridLayout of 1 row and 3 columns inside JInternalFrame
panel2 = new JPanel();
panel2.setLayout(new GridLayout(1,3));

//Constructing JButton using "for loop" and add to JPanel 2
for(int count=0; count<buttons.length; count++) {
buttons[count] = new JButton(buttonName[count]);
panel2.add(buttons[count]);
}

//Adding JPanel 1 and 2 to the JInternalFrame container
container.add(panel1,BorderLayout.NORTH);
container.add(panel2,BorderLayout.CENTER);

frame.setTitle("Add New Data"); //Set the Title of the JInternalFrame
frame.setResizable(false); //Lock the size of the JInternalFrame
frame.setMaximizable(false); //Disable the Maximize function of JInternalFrame

//Set the size of JInternalFrame to the size of its content
frame.pack();

//Attached the JInternalFrame to JDesktopPane and show it by setting the visible in to "true"
desktopTest.add(frame);
frame.setVisible(true);
}
}
);

/**Set all the Components Visible.
* If it is set to "false", the components in the container will not be visible.
*/
setVisible(true);
}

//Main Method
public static void main (String[] args) {
createJDesktoPane dp = new createJDesktoPane();
}
}

No comments:

Post a Comment