This tutorial demonstrates usage of JLayer component in Java Swing applications.
Approximately 30 minutes.
JDK 7 introduces a new Swing component,JLayer that serves as a universal decorator for Swing components. This new component is implemented as the javax.swing.JLayer class, and is based upon JXLayer, a part of the Swing Helper project from Swing Labs.
The JLayer class is a flexible and powerful decorator for Swing components. It enables you to draw on components and respond to component events without modifying the underlying component directly.
Using a JLayer, you can put a LayerUI together with any existing Component. You can also change the composition of LayerUIs and Components at run time.
The following is a list of software requirements:
Before starting this tutorial, you should have the software installed as listed under Software Requirements.
To demonstrate the custom painting feature of JLayer. You have to create a demo application, which reverses text entered into a texrfield when a button is pressed. The demo application uses JLayer to paint a wallpaper pattern behind the user interface . Perform the following steps.
|
. |
Create a New Project.
|
|---|---|
|
. |
Select Java from the Categories column and Java Application from the Projects column and then click Next.
|
|
. |
Perform the following steps. a. Name the project JLayerDemo
|
|
. |
Right-click JLayerDemo Project and select New > Java Class .
|
|
. |
Name the class WallPaperDemo, package name demo, and then click Finish.
|
|
. |
Add the following import statements to the java class, WallPaper.java import java.awt.*;
|
|
. |
To create, the UI add the below method createUI(), to the class.
This method creates UI with the follwing components -Jlabel,JTextField and a JButton. These components are added to a JPanel and the panel is returned.
|
|
. |
Next add the method createLayer() to the class. }
|
|
. |
Add the following event handling code to Reverse, to the CreateUI() method. ActionListener al; The Reverse button responds to the click event and retrieves the text entered in the JTextField, txtName and reverses the String and displays it in the textfield.
|
|
. |
Next add the main method to the class. In the main method createLayer() is invoked.
|
|
. |
In the Projects pane, right-click CelsiusConverter.java and choose Run File.
|
|
. |
You can test the application by entering a String in the Name field and then clicking Reverse.
Output with the reversed String will be as in the below screenshot.
|
|
. |
To create a WallPaper using JLayer .Add the below class to WallPaper.java class WallpaperLayerUI extends
LayerUI<JComponent> } } JLayer delegates the handling of painting and input events to a LayerUI
object, which performs the decoration.
|
|
. |
To add the Wallpaper to the UI using JLayer.Modify the createLayer() method as below 1. Add the following code .
The first argument to the JLayer constructor can be any class extending
java.awt.Component, is the Swing component you want to decorate.
2. Delete the below code. f.add(createUI());
|
|
. |
In the Projects pane, right-click CelsiusConverter.java and choose Run File.
|
|
. |
You can test the application by entering a String in the Name field and then clicking Reverse. Output with the reversed String and WallPaper applied to the UI will be as in the below screenshot.
|
JLayer class can be used to decorate text fields to show if they contain valid data. The JLayer class is used to provide a visual indication for fields that have invalid data. When the ValidationLayerUI class paints the text field, it draws a red X if the field contents cannot be parsed.To demonstrate the custom painting feature of JLayer. You have to create a demo application, which reverses text entered into a texrfield when a button is pressed. The demo application uses JLayer to paint a wallpaper pattern behind the user interface . Perform the following steps.
|
. |
Right-click JLayerDemo Project and select New > Java Class .
|
|---|---|
|
. |
Name the class FieldValidatorDemo, package name demo, and then click Finish.
|
|
. |
Add the following import statements to the java class, FieldValidator.java import java.awt.*;
|
|
. |
To create UI add a method createContent()
to the class and add the below code to the method.
This method perorms the following operations. 1.Creates a JLayer UI. 2.Creates a JLabel , JFormattedTextField with a initial value of 25. 3. Creates a JPanel.The Layout manager for the panel is set to be GridLayoutManager with 2 rows and 1 column. JLabel and the JFormattedTextField created in step2 is added to the JPanel. 4. Creates a JLabel Field,date . An instance of JFormattedTextField is created and its initial value is set for the current date . 5.Adds the JLabel and JFormattedTextField to the JPanel .
|
|
. |
Next add the main method to the class. In the main method createContent() is invoked.
|
|
. |
To implement FieldValidation using JLayer. Add the below class to FieldValidator.java class ValidationLayerUI extends
LayerUI<JFormattedTextField> { JLayer jlayer = (JLayer)c; // Paint the red X. The class ValidationLayerUI is a LayerUI subclass and the paint() method is overridden to implement the FieldValidation.
|
|
. |
In the Projects pane, right-click CelsiusConverter.java and choose Run File.
The output will be as below
|
|
. |
You can test the application for the following input coditions. 1. Delete the initial value in the Number Field . Output after FieldValidation will be as below
2. Delete the initial Data value displayed in the date field. Output after FieldValidation will be as below.
|
This tutorial describes some of the capabilities of the JLayer class. JLayers support for custom painting , event detection and Field Validation lets you improve the UI of individual components.
Credits
![]()
|
Copyright © 2011, Oracle and/or its affiliates. All rights reserved |