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.
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.
1 2 3 4 |
//Sample 01: Create a button add it setLayout(new FlowLayout()); Button btn1 = new Button("Do Task"); add(btn1); |
As already told, we will display the AWT Window when the user clicks this button. So, our frame class should implement ActionListener
.
1 2 3 4 |
//Sample 02: Implement ActionListener public class FrameWin extends Frame implements WindowListener, 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.
1 2 |
//Sample 03: Register Action Listener btn1.addActionListener(this); |
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.
1 2 3 4 5 6 7 8 9 10 11 |
//Sample 04: Extend from the Window Class public class FlashWindow extends Window { //Sample 05: Private String private String MsgText; //Sample 06: Constructor to create Flash Window public FlashWindow(Frame parent, String message) { super(parent); MsgText = message; } |
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.

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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
//Sample 07: Override Paint method @Override public void paint(Graphics g) { //Sample 09: Draw a Gray Background Dimension d = getSize(); g.setColor(Color.LIGHT_GRAY); g.fillRect(0, 0, d.width, d.height); //Sample 10: Draw Border around the Window g.setColor(Color.BLACK); g.drawLine(0, 0, d.width, 0); g.drawLine(0, 0, 0, d.height); g.setColor(Color.WHITE); g.drawLine(d.width-1, 0, d.width-1, d.height-1); g.drawLine(0, d.width, d.width-1, d.height-1); //Sample 11: Set the Message Text g.setColor(Color.BLACK); g.setFont(new Font("Verdana", Font.PLAIN, 14)); g.drawString(MsgText, 30, 50 ); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
@Override public void actionPerformed(ActionEvent arg0) { //sample 08: Show the Window FlashWindow FlashWin = new FlashWindow(this, "Doing it. Please Wait.."); FlashWin.setLayout(new FlowLayout()); FlashWin.setSize(200,100); FlashWin.setLocation(400,400); FlashWin.setVisible(true); FlashWin.paint(FlashWin.getGraphics()); try { Thread.sleep(4000); } catch (InterruptedException e) {} FlashWin.setVisible(false); } |
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.
Categories: AWT
Tags: AWT Window, drawLine, drawString, fillRect, paint, setColor, setFont