Creating AWT Flash Window

1. About AWT Window

In this example, we will create an AWT Flash Window to display message texts. Like AWT Frame, the Window class is also a Top-Level window. So, it can house other AWT Components like Labels, Texts, Radio Buttons, etc. However, AWT Window needs an owner, and the owner can be a Frame Window, a dialog or even a Window. The AWT Window does not provide any border and menus. You can imagine this as a frame-less window. Java AWT leaves it to the Java coder to do all the customization of AWT Window by implementing the paint method.

In this example, we will use paint method and Graphics object of the window, to customize it. Here, we will create AWT Flash Window by using the Java AWT Window class.

2. About AWT Flash Window Example

The below picture shows the example which we will create here. In the AWT Frame, we will display only one button. The button Do Task will open a AWT Window which shows a status message for a small while. This window is a sign to the user that some processing is going on. But our example mimics the long-running task with the ‘Thread.sleep’ method.

About the AWT Flash Window Example
About the AWT Flash Window Example

After a few seconds, the window closes automatically. You can use this same example to create a flash window. Here, we displayed some text in the window. In a flash window, you can display an image to show your brand.

3. Preparing Frame Window

First, we create a Frame window with only one button named Do Task. The Flow layout manager manages our frame window.

As already told, we will display the AWT Window when the user clicks this button. So, our frame class should implement ActionListener.

Next, we register our button with the ActionListener to handle the ActionEvent. We will write the actionPerformed handler function later after creating the Flash Window for Messages.

4. Creating AWT Flash Window

Flash Window will display for a few seconds and goes off. In our case, we will display a flash window to show an informative message to the user when a long-running task is going on. Here, we will derive a class from the AWT Window class and then override its paint method. Inside, we will perform the customization for the window.

4.1 Extend From AWT Window

In the below code, we derive our custom window called FlashWindow from the AWT Window. As we know, AWT Window is frame-less and hence we should take care of the window’s appearance by overriding the paint method. The private member MsgText will receive the string from the constructor. When the window is displayed, we show this message text using the Graphics object, which comes as an argument to the paint method. Note, the constructor is receiving the parent object as Frame. This parent is required by the base class constructor.

4.2 Override paint Method

Below picture shows the task which we perform via the Graphics object and its paint method. The first task is drawing a background for the window. Here, we show the window with a gray background color. The second task is drawing a white line in the right and bottom portion of the AWT Window. Third, we draw a black color lines in the left and top portion. These 4 lines together give a inscribed feel of the window. Finally, we show the message inside the window.

The Window with Graphics Task
The Window with Graphics Task

In the below code, we make use of the fillRect method of the graphics API to fill the Gray background to the window. Note, the getSize method will give the required dimension for this fillRect API call. Next, using the drawLine API, we draw all four lines to create a border. The color change of lines (Black and White) is to create deep-set look of the window. Before drawing the message string via drawString API call, we set a Font to the Graphics object to show the string bigger than the default.

5. Showing AWT Flash Window

Remember, we should implement the event handler for the Do Task button of the Frame Window. Have a look at the below code:

Here, we create the instance of our Frame Window. The first param is the ‘this’ object which represents our current AWT Frame Window. The second param is the message we want to display in the flash window (Line 4-5). After construction, we set its location and size and show it by calling the setVisible method. At Line 10, you can see we retrieve the graphics object of the FrameWindow itself and pass that as a param to the paint method, which we call on the same object. The call to static method Thread.sleep is to block the Frame Window thread for 4 seconds. This mimics there is a long-running task (A task that runs for 4 seconds) and after the sleep resumes, we set the visibility of the flash window to false. This hides the Flash Window.

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.