|
How to implement a label that does not give any indication it has been clicked |
|
|
This Java Swing tip illustrates a method of implementing a label that
does not give any indication it has been clicked. This is an extension
of the JLabel class that listens to mouse clicks and converts them to
ActionEvents, which in turn are reported via an EventListenersList object.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SecretLabel extends JLabel {
public SecretLabel(String msg) {
super(msg);
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
fireActionPerformed(new ActionEvent(SecretLabel.this,
ActionEvent.ACTION_PERFORMED,
"SecretMessage"));
}
});
}
public void addActionListener(ActionListener l) {
// We'll just use the listenerList we inherit from JComponent.
listenerList.add(ActionListener.class, l);
}
public void removeActionListener(ActionListener l) {
listenerList.remove(ActionListener.class, l);
}
protected void fireActionPerformed(ActionEvent ae) {
Object[] listeners = listenerList.getListeners(ActionListener.class);
for (int i = 0; i < listeners.length; i++) {
((ActionListener)listeners[i]).actionPerformed(ae);
}
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.