Academic Java    Java Tutorial  >  Swing  >  JDesktopPane

JDesktopPane Example

Java JDesktopPane EXAMPLE output A JDesktopPane is a container used to create a multiple-document interface or a virtual desktop.

This JDesktopPane example creates two instances of JInternalFrame and adds them to it.
//JDesktopPane Example
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class JDesktopPaneExample {

   static Box createBox(String[] sa) {
      Box b = new Box(BoxLayout.Y_AXIS);
      for(int i=0;i<sa.length;++i) b.add(new JLabel(sa[i]));
      return b;
   }

   public static void main(String[] args) {
      Box b1 = createBox(new 
         String[]{"cipher","bug","code","trojan"});

      JInternalFrame jif1 = new JInternalFrame(
                     "secrets", true, true, true, true);
      jif1.setContentPane(b1);
      jif1.pack();
      jif1.setVisible(true);

      Box b2 = createBox(new 
         String[]{"mole","plant","informer","spy","agent"});

      JInternalFrame jif2 = new JInternalFrame(
                     "lies", true, true, true, true);
      jif2.setContentPane(b2);
      jif2.pack();
      jif2.setVisible(true);

      JDesktopPane dtp = new JDesktopPane();
      dtp.add(jif1);
      dtp.add(jif2);

      JFrame f = new JFrame("DesktopPane");
      f.getContentPane().add(dtp);
      f.setSize(200, 300);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setVisible(true);
   }
}