Close dialog box on mouse click problem

Posted on 2010-09-16
Last Modified: 2013-11-23
How do I get a mouse click on the JLabel to close the dialog box?

package closeOnLabelClick;

import java.awt.FlowLayout;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.UIManager;

public class CloseOnLabelClick {

	private String CLOSE_BUTTON = "Close";	


	 * constructor


	public CloseOnLabelClick() {

		// panel with link to segment directory

		JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // left-justify the label

		JLabel label = new JLabel("Close dialog when click here");

		label.addMouseListener(new MouseListener() {


			public void mouseClicked(MouseEvent event) {

				System.out.println ("mouseClicked():");

				// close dlg box here ???



			public void mouseEntered(MouseEvent e) {



			public void mouseExited(MouseEvent e) {



			public void mousePressed(MouseEvent e) {



			public void mouseReleased(MouseEvent e) {




		JOptionPane.showOptionDialog(null,       // parent

				panel,                                       // message

				"CloseOnLabelClick",                         // title

				JOptionPane.NO_OPTION,            // option type

				JOptionPane.PLAIN_MESSAGE,    // message type

				null,                                             // icon

				new Object[] {CLOSE_BUTTON},   // buttons

				CLOSE_BUTTON);                        // default selection




	 * @param args


	public static void main(String[] args) {


			UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());


		catch (Exception e){



		new CloseOnLabelClick();



Open in new window

Question by:allelopath
1 Comment
LVL 86

Accepted Solution

CEHJ earned 500 total points
ID: 33693828
You'd be better off using a normal JDialog

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now