Using JDesktopPanes in Frame-based Applications

If you have several frames to display, you can place JInternalFrames inside a JDesktopPane and control easily which ones are displayed. This example shows how a button in one JInternalFrame can make another InternalFrame invisible. You can use JInternalFrames with other constructors so that, for example, new JInternalFrame('My Frame', false, true) will create a frame that cannot be dragged to a different size but can be closed by clicking on the "X" icon. For details see this Java page.

See below a screenshot and code of a simple demonstration followed by an extension of the code to illustrate a simple menu.

Desktop application in action

Desktop application in action

The code in desktop_app.pas

namespace desktop_app;

interface

uses
  java.util, java.awt.*, javax.swing;
   
type
  DesktopDemo = public class(JFrame, ActionListener)
  private
    lblGreeting : Label;
    vBox1, vBox2 : Box;
    ifrmLabel, ifrmButton : JInternalFrame;
    desktop : JDesktopPane;
    btnHideOtherFrame : JButton;   
   
  public
    constructor;
    class method Main(args: array of String); 
    method actionPerformed(e : actionEvent);  
  end;

implementation

constructor DesktopDemo;
begin
  desktop := new JDesktopPane;
  ifrmLabel := new JInternalFrame('Internal Frame with Label');
  ifrmLabel.Size := new Dimension(200, 100);
  lblGreeting := new Label('Hello World!');
  vBox1 := Box.createVerticalBox;
  vBox1.add(lblGreeting);
  ifrmLabel.add(vBox1);
  ifrmLabel.setVisible(true);
  desktop.add(ifrmLabel);
  ifrmLabel.setLocation(50, 20);

  btnHideOtherFrame := new JButton('Hide other internal frame');
  btnHideOtherFrame.addActionListener(self);
  vBox2 := Box.createVerticalBox;
  vBox2.add(btnHideOtherFrame);
  ifrmButton := new JInternalFrame('Internal Frame with Button');
  ifrmButton.Size := new Dimension(200, 80);
  ifrmButton.add(vBox2);
  ifrmButton.setVisible(true);
  desktop.add(ifrmButton);
  ifrmButton.setLocation(50, 140);

  setContentPane(desktop);

  //Ensure app closes when you click the close button
  DefaultCloseOperation := JFrame.EXIT_ON_CLOSE;
  //Set window caption and size
  Title := 'Desktop Demo';
  Size := new Dimension(300, 280);		
  setVisible(true); 
end;

class method DesktopDemo.Main(args: array of String);
begin
  new DesktopDemo;
end;

method DesktopDemo.actionPerformed(e : actionEvent);
begin
  if e.getSource = btnHideOtherFrame then
    ifrmLabel.setVisible(false); 
end;  

end.

Desktop Application with Menu

Desktop application with menu in action

Desktop application with menu in action

The code in desktop_app2.pas

namespace desktop_app2;

interface

uses
  java.util, java.awt.*, javax.swing.*;
   
type
  DesktopDemo = public class(JFrame, ActionListener)
  private
    lblGreeting : Label;
    vBox1, vBox2 : Box;
    ifrmLabel, ifrmButton : JInternalFrame;
    desktop : JDesktopPane;
    btnHideOtherFrame : JButton;
    menuBar : JMenuBar;
    menu : JMenu;
    menuItem : JMenuItem;   
  public
    constructor;
    class method Main(args : array of String); 
    method actionPerformed(e : actionEvent);
    method createMenuBar : JMenuBar;  
  end;

implementation

method DesktopDemo.createMenuBar : JMenuBar; 
begin
  menuBar := new JMenuBar; 
  //Set up the lone menu.
  menu := new JMenu('Document');
  menuBar.add(menu); 
  //Set up the first menu item.
  menuItem := new JMenuItem('View Label Form');
  menuItem.setActionCommand('Label');
  menuItem.addActionListener(self);
  menu.add(menuItem); 
  //Set up the second menu item.
  menuItem := new JMenuItem('View Button Form');
  menuItem.setActionCommand('Button');
  menuItem.addActionListener(self);
  menu.add(menuItem); 
  //Set up the third menu item.
  menuItem := new JMenuItem('Hide Button Form');
  menuItem.setActionCommand('Hide');
  menuItem.addActionListener(self);
  menu.add(menuItem); 
  result := menuBar;
end;

constructor DesktopDemo;
begin
  setJMenuBar(createMenuBar);
  desktop := new JDesktopPane;
  ifrmLabel := new JInternalFrame('Internal Frame with Label', false, false);
  ifrmLabel.Size := new Dimension(200, 80);
  lblGreeting := new Label('Hello World!');
  vBox1 := Box.createVerticalBox;
  vBox1.add(lblGreeting);
  ifrmLabel.add(vBox1);
  ifrmLabel.setVisible(true);
  desktop.add(ifrmLabel);
  ifrmLabel.setLocation(15, 20);

  btnHideOtherFrame := new JButton('Hide other internal frame');
  btnHideOtherFrame.addActionListener(self);
  vBox2 := Box.createVerticalBox;
  vBox2.add(btnHideOtherFrame);
  ifrmButton := new JInternalFrame('Internal Frame with Button', false, false);
  ifrmButton.Size := new Dimension(200, 80);
  ifrmButton.add(vBox2);
  ifrmButton.setVisible(true);
  desktop.add(ifrmButton);
  ifrmButton.setLocation(15, 120);

  setContentPane(desktop);

  //Ensure app closes when you click the close button
  DefaultCloseOperation := JFrame.EXIT_ON_CLOSE;
  //Set window caption and size
  Title := 'Desktop Demo';
  Size := new Dimension(250, 280);		
  setVisible(true); 
end;

class method DesktopDemo.Main(args: array of String);
begin
  new DesktopDemo;
end;

method DesktopDemo.actionPerformed(e : actionEvent);
begin
  if e.getSource = btnHideOtherFrame then
    ifrmLabel.setVisible(false);
  if e.getActionCommand = 'Label' then
    begin
      ifrmLabel.setVisible(true);
    end;
  if e.getActionCommand = 'Button' then
    ifrmButton.setVisible(true);
  if e.getActionCommand = 'Hide' then
    ifrmButton.setVisible(false);  
end;  

end.
Programming - a skill for life!

How to use widgets such as JButtons, JRadioButtons, JLists, JTables and JDesktopPanes in Oxygene for Java Applications