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

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...

Data Warehousing - An Overview

Anurag Information Technology (IT) has historically influenced organizational performance and competitive standing. The increasing processing power and sophistication of analytical tools and techniques have put the strong foundation for the product called data warehouse. There are a number of reasons that any organization should consider a data warehouse, which can be the critical tool for maximizing the organization’s investment in the information it has collected and stored throughout the enterprise. IT managers need to understand the rationale and benefits of data warehouses because they may need to design and implement, or procure this kingpin of business intelligence. The data warehouses are supposed to provide storage, functionality and responsiveness to queries beyond the capabilities of today's transaction-oriented databases. Also data warehouses are set to improve the data access performance of databases. Traditional databases balance the requirement of data access w...

Normalization in DBMS: 1NF, 2NF, 3NF and BCNF in Database

Normalization   is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly.  Anomalies in DBMS There are three types of anomalies that occur when the database is not normalized. These are – Insertion, update and deletion anomaly. Let’s take an example to understand this. Example : Suppose a manufacturing company stores the employee details in a table named employee that has four attributes: emp_id for storing employee’s id, emp_name for storing employee’s name, emp_address for storing employee’s address and emp_dept for storing the department details in which the employee works. At some point of time the table looks like this: emp_id emp_name emp_address emp_dept 101 Nikhil Kangra D001 101 Nikhil Kangra D002 123 Ashish Shimla D890 166 Rahul Pathankot D900 166 Rahul Pathankot D004 The above table is not normalized.  Update anomaly : In the above table we have two rows for employee Nikhil as he belongs ...