Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Close dialog box on mouse click problem

Posted on 2010-09-16
1
Medium Priority
?
823 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 2000 total points
ID: 33693828
You'd be better off using a normal JDialog
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

782 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