You are on page 1of 15

Object Oriented Programming

Tutorial On JDesktopPane Class

Presented By: Group#6


Objectives
Since this tutorial has been prepared for the beginners to help them understand the
basic functionality related to Classe JDesktop and you will learn in this tutorials Classe
Hierarchy, Constructors and their Functions With Examples:

1.Java JDesktopPane:
The JDesktopPane class, can be used to create "multi-document" applications. A multi-
document application can have many windows included in it. We do it by making the
contentPane in the main window as an instance of the JDesktopPane class or a subclass.
Internal windows add instances of JInternalFrame to the JdesktopPane instance. The
internal windows are the instances of JInternalFrame or its subclasses.

Fields

Constructor

Java Swing | Internal Frame with examples


JInternalFrame is a part of Java Swing . JInternalFrame is a container that provides many
features of a frame which includes displaying title, opening, closing, resizing, support for
menu bar, etc. 
Constructors for JInternalFrame 
 
1. JInternalFrame() : creates a new non- closable, non- resizable, non- iconifiable,
non- maximizable JInternalFrame with no title
2. JInternalFrame(String t) :creates a new non- closable, non- resizable, non-
iconifiable, non- maximizable JInternalFrame with a title specified
3. JInternalFrame(String t, boolean resizable) :creates a new non- closable, non-
iconifiable, non- maximizable JInternalFrame with a title and resizability specified
4. JInternalFrame(String t, boolean resizable, boolean closable) : creates a new
non- iconifiable, non- maximizable JInternalFrame with a title, closability and
resizability specified
5. JInternalFrame(String t, boolean resizable, boolean closable, boolean
maximizable) :creates a new non- iconifiable JInternalFrame with a title, closability,
maximizability and resizability specified 
 
6. JInternalFrame(String t, boolean resizable, boolean closable, boolean
maximizable, boolean iconifiable) : creates a new JInternalFrame with a title,
closability, maximizability, iconifiability and resizability specified

Commonly used methods 


 
1. setFrameIcon(Icon icon) : sets the icon for the frame to the specified image
2. setLayout(LayoutManager manager) : sets the layout of the frame to specified
layout manager
3. setTitle(String t): set the title of the frame to specified title
4. getTitle() : get the title of the frame
5. reshape(int x, int y, int width, int height) : resize the frame to specified width
and height and a specified location
6. add(Component c) : adds the specified component to the container.
7. addImpl(Component c, Object co, int i) : adds the specified component.
8. addInternalFrameListener(InternalFrameListener l) : adds the specified
InternalFrameListener to the list.
9. createRootPane() : called by the constructor to set up the JRootPane.
10. dispose() : makes this internal frame invisible, unselected, and closed.
11. fireInternalFrameEvent(int id) : fires an internal frame event.
12. getAccessibleContext() : gets the AccessibleContext associated with this
JInternalFrame.
13. getContentPane() : returns the content pane for this internal frame.
14. getDefaultCloseOperation() : returns the default operation that occurs when the
user initiates a “close” on this internal frame.
15. getDesktopIcon() : returns the JDesktopIcon used when this JInternalFrame is
iconified.
16. getDesktopPane() : convenience method that searches the ancestor hierarchy for a
JDesktop instance.
17. getFocusOwner() : If this JInternalFrame is active, returns the child that has
focus.
18. getFrameIcon() : returns the image displayed in the title bar of this internal frame
19. getGlassPane() : returns the glass pane for this internal frame.
20. getInternalFrameListeners() : Returns an array of all the InternalFrameListeners
added to this JInternalFrame with addInternalFrameListener
21. getJMenuBar() : returns the current JMenuBar for this JInternalFrame
22. getLastCursor() : returns the last Cursor that was set by the setCursor method
23. getLayer() : convenience method for getting the layer attribute of this component.
24. getLayeredPane() : returns the layered pane for this internal frame.
25. getMostRecentFocusOwner() : returns the child component of this
JInternalFrame that will receive the focus when this JInternalFrame is selected.
26. getNormalBounds() : If the JInternalFrame is not in maximized state, returns
getBounds(); otherwise, returns the bounds that the JInternalFrame would be restored
to.
27. getRootPane(): returns the rootPane object for this internal frame.
28. getUI() : returns the look-and-feel object that renders this component.
29. getWarningString() : gets the warning string that is displayed with this internal
frame.
30. isClosable() : returns whether this JInternalFrame can be closed by some user
action.
31. isClosed() : Returns whether this JInternalFrame is currently closed.
32. isIcon() : returns whether the JInternalFrame is currently iconified.
33. isMaximizable() : gets the value of the maximizable property.
34. isMaximum() : returns whether the JInternalFrame is currently maximized.
35. isResizable() : returns whether the JInternalFrame can be resized or not.
36. isSelected() : returns whether the JInternalFrame is the currently active frame or
not.
37. pack() : causes components of this JInternalFrame to be laid out at their preferred
size.
38. paintComponent(Graphics g) : Overridden to allow optimized painting when the
internal frame is being dragged.
39. paramString() : Returns a string representation of this JInternalFrame.
40. remove(Component c) : removes the specified component from the container.
41. removeInternalFrameListener(InternalFrameListener l) : removes the
specified internal frame listener.
42. setClosable(boolean b) : sets whether this JInternalFrame can be closed by some
user action.
43. setContentPane(Container c) : sets this JInternalFrame’s contentPane property.
44. setCursor(Cursor c) : sets the cursor image to the specified cursor.
45. setDefaultCloseOperation(int o): sets the operation that will happen by default
when the user initiates a “close” on this internal frame.
46. setDesktopIcon(JInternalFrame.JDesktopIcon d) : sets the JDesktopIcon
associated with this JInternalFrame.
47. setGlassPane(Component g) : sets this JInternalFrame’s glassPane property.
48. setIcon(boolean b) : Iconifies or de-iconifies this internal frame.
49. setJMenuBar(JMenuBar m) : sets the menuBar property for this JInternalFrame.
50. setIconifiable(boolean b) : sets the iconable property, which must be true for the
user to be able to make the JInternalFrame an icon.
51. setJMenuBar(JMenuBar m) : sets the menuBar property for this JInternalFrame.
52. setLayer(int l) : convenience method for setting the layer attribute of this
component.
53. setLayer(Integer l) : convenience method for setting the layer attribute of this
component.
54. setLayeredPane(JLayeredPane l) : sets this JInternalFrame’s layeredPane
property.
55. setMaximizable(boolean b) : sets the maximizable property, which determines
whether the JInternalFrame can be maximized by some user action.
56. setMaximum(boolean b) : Maximizes and restores this internal frame.
57. setNormalBounds(Rectangle r) : sets the normal bounds for this internal frame.
58. setResizable(boolean b) :sets whether the JInternalFrame can be resized by some
user action.
59. setRootPane(JRootPane r) : sets the rootPane property for this JInternalFrame.
60. setRootPaneCheckingEnabled(boolean e) : sets whether calls to add and
setLayout are forwarded to the contentPane.
61. setSelected(boolean s) : selects or deselects the internal frame if it’s showing.
62. setUI(InternalFrameUI ui) : sets the UI delegate for this JInternalFrame.
63. show() : makes the internal frame visible.
64. toBack(): sends this internal frame to the back.
65. toFront() : Brings this internal frame to the front.
66. updateUI() : notification from the UIManager that the look and feel has changed.

EXERCISE Questions:

Q#1 Java JDesktopPane Example:


1. import java.awt.BorderLayout;  
2. import java.awt.Container;  
3. import javax.swing.JDesktopPane;  
4. import javax.swing.JFrame;  
5. import javax.swing.JInternalFrame;  
6. import javax.swing.JLabel;  
7. public class JDPaneDemo extends JFrame  
8. {  
9.   public JDPaneDemo()   
10.   {  
11.     CustomDesktopPane desktopPane = new CustomDesktopPane();  
12.     Container contentPane = getContentPane();  
13.     contentPane.add(desktopPane, BorderLayout.CENTER);  
14.     desktopPane.display(desktopPane);  
15.   
16.     setTitle("JDesktopPane Example");  
17.     setSize(300,350);  
18.     setVisible(true);  
19.   }  
20.   public static void  main(String args[])  
21.   {  
22.     new JDPaneDemo();  
23.   }  
24. }  
25. class CustomDesktopPane extends JDesktopPane  
26. {  
27.   int numFrames = 3,  x = 30, y = 30;  
28.   public void display(CustomDesktopPane dp)   
29.   {  
30.     for(int  i = 0; i < numFrames ; ++i )   
31.     {  
32.       JInternalFrame jframe = new JInternalFrame("Internal Frame " + i ,  true, tr
ue, true, true);  
33.   
34.       jframe.setBounds(x, y, 250, 85);  
35.      Container c1 = jframe.getContentPane( ) ;  
36.      c1.add(new JLabel("I love my country"));  
37.      dp.add( jframe );  
38.      jframe.setVisible(true);         
39.      y += 85;  
40.     }  
41.   }  
42. }  
Q#2 Program to create a simple JInternalFrame : 

// java Program to create a simple JInternalFrame


import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solution extends JFrame {

// frame
static JFrame f;

// label to display text


static JLabel l;

// main class
public static void main(String[] args)
{
// create a new frame to
f = new JFrame("frame");

// create a internal frame


JInternalFrame in = new JInternalFrame();

// set the title of the frame


in.setTitle("InternalFrame");

// create a Button
JButton b = new JButton("button");

// create a label to display text


l = new JLabel("This is a JInternal Frame ");

// create a panel
JPanel p = new JPanel();

// add label and button to panel


p.add(l);
p.add(b);

// set visibility internal frame


in.setVisible(true);

// add panel to internal frame


in.add(p);

// add internal frame to frame


f.add(in);

// set the size of frame


f.setSize(300, 300);

f.show();
}
}
Q#3: program to create multiple internal frames 

// java Program to create multiple internal frames


import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solution extends JFrame {

// frame
static JFrame f;

// label to display text


static JLabel l, l1;

// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("frame");

// set layout of frame


f.setLayout(new FlowLayout());

// create a internal frame


JInternalFrame in = new JInternalFrame("frame 1", true, true, true, true);

// create a internal frame


JInternalFrame in1 = new JInternalFrame("frame 2", true, true, true, true);

// create a Button
JButton b = new JButton("button");
JButton b1 = new JButton("button1");

// create a label to display text


l = new JLabel("This is a JInternal Frame no 1 ");
l1 = new JLabel("This is a JInternal Frame no 2 ");

// create a panel
JPanel p = new JPanel();
JPanel p1 = new JPanel();

// add label and button to panel


p.add(l);
p.add(b);
p1.add(l1);
p1.add(b1);

// set visibility internal frame


in.setVisible(true);
in1.setVisible(true);

// add panel to internal frame


in.add(p);
in1.add(p1);

// add internal frame to frame


f.add(in);
f.add(in1);

// set the size of frame


f.setSize(300, 300);

f.show();
}
}
Q#4 Program to create multiple frame and set icon to the frame 
 
// java Program to create multiple frame and set icon to the frame
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solution extends JFrame {

// frame
static JFrame f;

// label to display text


static JLabel l, l1;

// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("frame");

// set layout of frame


f.setLayout(new FlowLayout());

// create a internal frame


JInternalFrame in = new JInternalFrame("frame 1",
true, true, true, true);

// create a internal frame


JInternalFrame in1 = new JInternalFrame("frame 2",
true, true, true, true);

// set icon for internal frames


in.setFrameIcon(new ImageIcon("f:/gfg.jpg"));
in1.setFrameIcon(new ImageIcon("f:/gfg.jpg"));

// create a Button
JButton b = new JButton("button");
JButton b1 = new JButton("button1");

// create a label to display text


l = new JLabel("This is a JInternal Frame no 1 ");
l1 = new JLabel("This is a JInternal Frame no 2 ");

// create a panel
JPanel p = new JPanel();
JPanel p1 = new JPanel();

// add label and button to panel


p.add(l);
p.add(b);
p1.add(l1);
p1.add(b1);

// set visibility internal frame


in.setVisible(true);
in1.setVisible(true);

// add panel to internal frame


in.add(p);
in1.add(p1);

// add internal frame to frame


f.add(in);
f.add(in1);
// set the size of frame
f.setSize(300, 300);

f.show();
}
}

You might also like