Skip to main content

Java Swing: JComponent, JLayeredPane & JDEsktopPane

Java JComponent

The JComponent class is the base class of all Swing components except top-level containers. Swing components whose names begin with "J" are descendants of the JComponent class. For example, JButton, JScrollPane, JPanel, JTable etc. But, JFrame and JDialog don't inherit JComponent class because they are the child of top-level containers.

The JComponent class extends the Container class which itself extends Component. The Container class has support for adding components to the container.

Fields

Modifier and TypeFieldDescription
protected AccessibleContextaccessibleContextThe AccessibleContext associated with this JComponent.
protectedEventListenerListlistenerListA list of event listeners for this component.
static StringTOOL_TIP_TEXT_KEYThe comment to display when the cursor is over the component, also known as a "value tip", "flyover help", or "flyover label"
protected ComponentUIuiThe look and feel delegate for this component.
static intUNDEFINED_CONDITIONIt is a constant used by some of the APIs to mean that no condition is defined.
static intWHEN_ANCESTOR_OF_FOCUSED_COMPONENTIt is a constant used for registerKeyboardAction that means that the command should be invoked when the receiving component is an ancestor of the focused component or is itself the focused component.
static intWHEN_FOCUSEDIt is a constant used for registerKeyboardAction that means that the command should be invoked when the component has the focus.
static intWHEN_IN_FOCUSED_WINDOWConstant used for registerKeyboardAction that means that the command should be invoked when the receiving component is in the window that has the focus or is itself the focused component.

Constructor

ConstructorDescription
JComponent()Default JComponent constructor.

Useful Methods

Modifier and TypeMethodDescription
voidsetActionMap(ActionMap am)It sets the ActionMap to am.
voidsetBackground(Color bg)It sets the background color of this component.
voidsetFont(Font font)It sets the font for this component.
voidsetMaximumSize(Dimension maximumSize)It sets the maximum size of this component to a constant value.
voidsetMinimumSize(Dimension minimumSize)It sets the minimum size of this component to a constant value.
protected voidsetUI(ComponentUI newUI)It sets the look and feel delegate for this component.
voidsetVisible(boolean aFlag)It makes the component visible or invisible.
voidsetForeground(Color fg)It sets the foreground color of this component.
StringgetToolTipText(MouseEvent event)It returns the string to be used as the tooltip for event.
ContainergetTopLevelAncestor()It returns the top-level ancestor of this component (either the containing Window or Applet), or null if this component has not been added to any container.
TransferHandlergetTransferHandler()It gets the transferHandler property.

Java JComponent Example

  1. import java.awt.Color;  
  2. import java.awt.Graphics;  
  3. import javax.swing.JComponent;  
  4. import javax.swing.JFrame;  
  5. class MyJComponent extends JComponent {  
  6.       public void paint(Graphics g) {  
  7.         g.setColor(Color.green);  
  8.         g.fillRect(3030100100);  
  9.       }  
  10. }  
  11. public class JComponentExample {  
  12.       public static void main(String[] arguments) {  
  13.         MyJComponent com = new MyJComponent();  
  14.         // create a basic JFrame  
  15.         JFrame.setDefaultLookAndFeelDecorated(true);  
  16.         JFrame frame = new JFrame("JComponent Example");  
  17.         frame.setSize(300,200);  
  18.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  19.         // add the JComponent to main frame  
  20.         frame.add(com);  
  21.         frame.setVisible(true);  
  22.       }  
  23. }  

Output:

Java Jcomponent

Java JLayeredPane

The JLayeredPane class is used to add depth to swing container. It is used to provide a third dimension for positioning component and divide the depth-range into several different layers.


JLayeredPane class declaration

  1. public class JLayeredPane extends JComponent implements Accessible  

Commonly used Constructors:

ConstructorDescription
JLayeredPaneIt is used to create a new JLayeredPane

Commonly used Methods:

MethodDescription
int getIndexOf(Component c)It is used to return the index of the specified Component.
int getLayer(Component c)It is used to return the layer attribute for the specified Component.
int getPosition(Component c)It is used to return the relative position of the component within its layer.


Java JLayeredPane Example

  1. import javax.swing.*;  
  2. import java.awt.*;  
  3. public class LayeredPaneExample extends JFrame {  
  4.   public LayeredPaneExample() {  
  5.     super("LayeredPane Example");  
  6.     setSize(200200);  
  7.     JLayeredPane pane = getLayeredPane();  
  8.     //creating buttons  
  9.     JButton top = new JButton();  
  10.     top.setBackground(Color.white);  
  11.     top.setBounds(20205050);  
  12.     JButton middle = new JButton();  
  13.     middle.setBackground(Color.red);  
  14.     middle.setBounds(40405050);  
  15.     JButton bottom = new JButton();  
  16.     bottom.setBackground(Color.cyan);  
  17.     bottom.setBounds(60605050);  
  18.     //adding buttons on pane  
  19.     pane.add(bottom, new Integer(1));  
  20.     pane.add(middle, new Integer(2));  
  21.     pane.add(top, new Integer(3));  
  22.   }  
  23.   public static void main(String[] args) {  
  24.       LayeredPaneExample panel = new  LayeredPaneExample();  
  25.       panel.setVisible(true);  
  26.   }  
  27. }  

Output:

Java Jlayeredpane 1


Java JLayeredPane Example

  1. import javax.swing.*;  
  2. import java.awt.*;  
  3. public class LayeredPaneExample extends JFrame {  
  4.   public LayeredPaneExample() {  
  5.     super("LayeredPane Example");  
  6.     setSize(200200);  
  7.     JLayeredPane pane = getLayeredPane();  
  8.     //creating buttons  
  9.     JButton top = new JButton();  
  10.     top.setBackground(Color.white);  
  11.     top.setBounds(20205050);  
  12.     JButton middle = new JButton();  
  13.     middle.setBackground(Color.red);  
  14.     middle.setBounds(40405050);  
  15.     JButton bottom = new JButton();  
  16.     bottom.setBackground(Color.cyan);  
  17.     bottom.setBounds(60605050);  
  18.     //adding buttons on pane  
  19.     pane.add(bottom, new Integer(1));  
  20.     pane.add(middle, new Integer(2));  
  21.     pane.add(top, new Integer(3));  
  22.   }  
  23.   public static void main(String[] args) {  
  24.       LayeredPaneExample panel = new  LayeredPaneExample();  
  25.       panel.setVisible(true);  
  26.   }  
  27. }  

Output:

Java Jlayeredpane 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

Modifier and TypeFieldDescription
static intLIVE_DRAG_MODEIt indicates that the entire contents of the item being dragged should appear inside the desktop pane.
static intOUTLINE_DRAG_MODEIt indicates that an outline only of the item being dragged should appear inside the desktop pane.

Constructor

ConstructorDescription
JDesktopPane()Creates a new JDesktopPane.

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 ,  truetruetruetrue);  
  33.   
  34.       jframe.setBounds(x, y, 25085);  
  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. }  

Output:

Java Jdesktoppane



Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are following header files important to C++ pro