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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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…

757 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

24 Experts available now in Live!

Get 1:1 Help Now