allelopath
asked on
Close dialog box on mouse click problem
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();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.