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(30, 30, 100, 100);  
  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(200, 200);  
  7.     JLayeredPane pane = getLayeredPane();  
  8.     //creating buttons  
  9.     JButton top = new JButton();  
  10.     top.setBackground(Color.white);  
  11.     top.setBounds(20, 20, 50, 50);  
  12.     JButton middle = new JButton();  
  13.     middle.setBackground(Color.red);  
  14.     middle.setBounds(40, 40, 50, 50);  
  15.     JButton bottom = new JButton();  
  16.     bottom.setBackground(Color.cyan);  
  17.     bottom.setBounds(60, 60, 50, 50);  
  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(200, 200);  
  7.     JLayeredPane pane = getLayeredPane();  
  8.     //creating buttons  
  9.     JButton top = new JButton();  
  10.     top.setBackground(Color.white);  
  11.     top.setBounds(20, 20, 50, 50);  
  12.     JButton middle = new JButton();  
  13.     middle.setBackground(Color.red);  
  14.     middle.setBounds(40, 40, 50, 50);  
  15.     JButton bottom = new JButton();  
  16.     bottom.setBackground(Color.cyan);  
  17.     bottom.setBounds(60, 60, 50, 50);  
  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 ,  true, true, 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. }  

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

Genotype Representation

Binary Representation This is one of the simplest and most widely used representation in GAs. In this type of representation the genotype consists of bit strings. For some problems when the solution space consists of Boolean decision variables – yes or no, the binary representation is natural. Take for example the 0/1 Knapsack Problem. If there are n items, we can represent a solution by a binary string of n elements, where the x th  element tells whether the item x is picked (1) or not (0). For other problems, specifically those dealing with numbers, we can represent the numbers with their binary representation. The problem with this kind of encoding is that different bits have different significance and therefore mutation and crossover operators can have undesired consequences. This can be resolved to some extent by using  Gray Coding,  as a change in one bit does not have a massive effect on the solution. Real Valued Representation For problems where we want to define ...