Java Swing | Creating Custom Message Dialogs

Though Java Swing provides built-in message dialog to display messages, we can create custom message dialog by using JWindow and other Java Swing elements. The advantage of creating them is that they are highly customizable and we can add the desired look-and-feel and functionalities to them.

In this article we will see how to create custom message in Java Swing .

Examples:



First we create a simple JWindow and add label and button to it.
Output:


Then we will shape the window and background color to it.
Output:


Then will set the look and feel of the label and
button to System look and feel and then add glossy 
appearance to the window by applying per pixel
translucency.
Output:

In the following programs we will see how to create a message dialog.

1.Program to create a simple JWindow and add label and button to it.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to create a simple JWindow
// and add label and button to it.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class message implements ActionListener {
  
    // window
    JWindow w;
  
    // constructor
    message()
    {
        // create a window
        w = new JWindow();
  
        // create a label
        JLabel l = new JLabel("This  is a message dialog");
  
        // create a new button
        JButton b = new JButton("OK");
  
        // add action listener
        b.addActionListener(this);
  
        // create a panel
        JPanel p = new JPanel();
  
        // add contents to panel
        p.add(l);
        p.add(b);
  
        w.add(p);
  
        w.setSize(200, 100);
        w.setLocation(300, 300);
  
        w.show();
    }
  
    // if button is pressed
    public void actionPerformed(ActionEvent evt)
    {
        w.setVisible(false);
    }
  
    // main class
    public static void main(String args[])
    {
        // create aobject
        message m = new message();
    }
}

chevron_right


output:

2.Program to create a message window, shape the window and background color to it.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to create a message window,
// and shape the window and add background color to it
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class message1 implements ActionListener {
    // window
    JWindow w;
  
    // constructor
    message1()
    {
        // create a window
        w = new JWindow();
  
        // set background of window transparent
        w.setBackground(new Color(0, 0, 0, 0));
  
        // create a label
        JLabel l = new JLabel("This  is a message dialog");
  
        // create a new button
        JButton b = new JButton("OK");
  
        // add action listener
        b.addActionListener(this);
  
        try {
            // set windows look and feel
            UIManager.setLookAndFeel(UIManager.
                             getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
        }
  
        // create a panel
        JPanel p = new JPanel() {
            public void paintComponent(Graphics g)
            {
  
                g.setColor(new Color(100, 100, 240));
                g.fillRoundRect(0, 0, 200, 100, 20, 20);
  
                g.setColor(new Color(10, 10, 255));
                g.drawRoundRect(0, 0, 200, 100, 20, 20);
            }
        };
  
        // create a font
        Font f = new Font("BOLD", 1, 14);
  
        l.setFont(f);
  
        // add contents to panel
        p.add(l);
        p.add(b);
  
        w.add(p);
  
        w.setSize(200, 100);
        w.setLocation(300, 300);
  
        w.show();
    }
  
    // if button is pressed
    public void actionPerformed(ActionEvent evt)
    {
        w.setVisible(false);
    }
  
    // main class
    public static void main(String args[])
    {
        // create aobject
        message1 m = new message1();
    }
}

chevron_right


output:

3. Program to create a message window, shape the window, add background color to it and also add glossy appearance to the window by applying per pixel translucency

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to create a message window, shape the window
// add background color to it and also add
// glossy appearance to the window by applying per pixel translucency
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class message2 implements ActionListener {
    // window
    JWindow w;
  
    // constructor
    message2()
    {
        // create a window
        w = new JWindow();
  
        // set background of window transparent
        w.setBackground(new Color(0, 0, 0, 0));
  
        // create a label
        JLabel l = new JLabel("This  is a message dialog");
  
        // create a new button
        JButton b = new JButton("OK");
  
        // add action listener
        b.addActionListener(this);
  
        try {
            // set windows look and feel
            UIManager.setLookAndFeel(UIManager
                         .getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
        }
  
        // create a panel
        JPanel p = new JPanel() {
            public void paintComponent(Graphics g)
            {
  
                g.setColor(new Color(100, 100, 240));
                g.fillRoundRect(0, 0, 200, 100, 20, 20);
  
                g.setColor(new Color(10, 10, 255));
                g.drawRoundRect(0, 0, 200, 100, 20, 20);
  
                // create a glossy apperance
                for (int i = 0; i < 100; i++) {
                    g.setColor(new Color(255, 255, 255, i));
                    g.drawLine(0, i, 200, i);
                }
            }
        };
  
        // create a font
        Font f = new Font("BOLD", 1, 14);
  
        l.setFont(f);
  
        // add contents to panel
        p.add(l);
        p.add(b);
  
        w.add(p);
  
        w.setSize(200, 100);
        w.setLocation(300, 300);
  
        w.show();
    }
  
    // if button is pressed
    public void actionPerformed(ActionEvent evt)
    {
        w.setVisible(false);
    }
  
    // main class
    public static void main(String args[])
    {
        // create aobject
        message2 m = new message2();
    }
}

chevron_right


Output :

Note : The following program might not run in an online compiler please use an offline IDE.



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.