Solved

Close dialog box on mouse click problem

Posted on 2010-09-16
1
818 Views
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() {

			@Override
			public void mouseClicked(MouseEvent event) {
				System.out.println ("mouseClicked():");
				// close dlg box here ???
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mousePressed(MouseEvent e) {
			}

			@Override
			public void mouseReleased(MouseEvent e) {
			}
		});

		panel.add(label);

		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) {

		try{
			UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
		}
		catch (Exception e){
			e.printStackTrace();
		}

		new CloseOnLabelClick();

	}

}

Open in new window

0
Comment
Question by:allelopath
1 Comment
 
LVL 86

Accepted Solution

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 58
Selenium docs api java index 3 66
Need Help! Getting a syntax error and don't understand why 3 39
Delete .class files at every compile 4 18
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

756 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