The developers at the Java Swing team are anxious of reproducing the look of the operating system the Java Runtime Environment runs under.
A window created with Java under Windows® XP shall look like a native window would look like under this OS. Running under a MacOS®, the window
shall look like a native MacOS® window.
However, when it comes to Windows® Vista®, developers are certainly a step behind: many elements differ from the standard components of the OS. In another tutorial I was already about how to implement a Vista® like help viewer component. This time I will write about the information windows that are used extensively throughout GUI programs. They are called message boxes.
In this tutorial I will show how to easily create Vista®-like message boxes. At the end of the tutorial you will find a usefull class which offers various message box types and can easily be used within your own application.
The difference
![]() |
![]() |
On the left you see the standard message box that Swing generates using one of the JOptionPane.showMessageDialog(...) methods under the Windows look and feel. On the right you see a dialog window resembling the ones in Windows® Vista®, which you will be able to create after reading this tutorial.
Getting started
We don't use the standard message boxes that Swing offers any more and create an own class for creating the dialogs. This said, we need to take a look at the Microsoft® guidelines of creating message boxes in Vista® first. They can be found in the Windows® Vista® Developer Center [LINK].
Since we want to build similar looking dialogs we first need to take a look at the ones that exist in Vista®. As the container component we choose a JDialog. Compared to a JWindow or JFrame it has the advantage of being able to be set as modal. This means that our program will interrupt its work until the message box is being closed by the user.
The needed elements comprise a ImageIcon for the small image, a JTextArea for the message text and another JLabel for the blue colored title text. An ActionListener is registered to the button and closes the dialog window after a click on.
In lines 10-16 some conatants for the fonts and colors are declared. The little image is loaded within lines 30 to 39. You find the needed images for download at the end of the tutorial. The short blue title text is inserted in lines 42 to 44. The title text and the image are put onto a JPanel (lines 47-51).
import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.IOException; { /** Fonts for the texts */ /** Colors for the background and the title text */ /** * Shows a dialog window in Windows Vista style * * @param title the dialog's title * @param labelText the blue title text * @param messageText the description text */ super(); // Icon for the little image BufferedImage icon = null; try{ icon = ImageIO.read(getClass().getResource("images/error.png")); } } // JLabel for the blue title text titleText.setForeground(titleTextColor); titleText.setFont(titleFont); // JPanel for the image and the title headerPanel.setBackground(background); headerPanel.add(iconLabel); headerPanel.add(titleText); // JTextArea for the detailed description text messages.setLineWrap(true); messages.setWrapStyleWord(true); messages.setFont(messageFont); messages.setEditable(false); // JPanel for the information text mainPanel.setBackground(background); messages.setBackground(background); // an empty border is used for spacing issues // JPanel for the button closeButt.setText("Close"); // dispose the dialog after a click on the button dispose(); } }); buttonPanel.add(closeButt); // add the panels to the dialog window // settings for the dialog setTitle(title); setIconImage(null); setModal(true); setAlwaysOnTop(true); pack(); setResizable(false); setLocationRelativeTo(null); // center the window on the screen setDefaultCloseOperation(DISPOSE_ON_CLOSE); // show the dialog window setVisible(true); } }
Afterwards, the detailed description text is put into a text area and into another JPanel (lines 54-67).
Then, the close button is being added (lines 70-80). In the end some settings concerning the dialog window itself are being made and the dialog is being shown. A call within your program could be done using something like this:
new VistaDialog("Error", "An unusual error occurred...", "A very severe error occurred while saving the file!");

Diese Seite gibt es auch auf deutsch.


Leave a comment in the forums. You don't need to be registered.