Solved

Close dialog box on mouse click problem

Posted on 2010-09-16
1
820 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

623 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