Thursday, 4 October 2012

creating a java desktop pane

This Java program is used for creating a Desktop pane. Copy the following code and paste on a note pad, save it as "" and run the program.

//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)

     //Exit Property of the Window

     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();
     pane.add(desktopTest); //Adding JDesktopPane in the container
     //Implemeting Even-Listener on newFile JMenuItem
  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);
   //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]);
   //Adding JPanel 1 and 2 to the JInternalFrame container
   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
   //Attached the JInternalFrame to JDesktopPane and show it by setting the visible in to "true"

     /**Set all the Components Visible.
      * If it is set to "false", the components in the container will not be visible.
 //Main Method
    public static void main (String[] args) {
     createJDesktoPane dp = new createJDesktoPane();

Post a Comment