Mouse Events on Disabled Buttons

Hi,

In my application I should make a disabled button to show a tool tip when mouse is entered onto it.

I'm using java.awt.container not Jcontainer.

I have searched in SDN forums and after reading some of the comments what I understood is disabled Swing button can react to Mouse events but a disabled awt button can not react to mouse events.

Is that true or did I not understand correctly?

And how would I be able to implement the required functionality in my

application?

Thanks.

[533 byte] By [sunsdn123a] at [2007-11-27 11:14:28]
# 1

import java.awt.*;

import java.awt.event.*;

public class AwtTooltip {

private Panel getContent(Frame f) {

Button left = new Button("left");

left.setEnabled(false);

Button right = new Button("right");

Panel panel = new Panel(new GridBagLayout());

new TipManager(panel, f);

GridBagConstraints gbc = new GridBagConstraints();

gbc.weightx = 1.0;

panel.add(left, gbc);

panel.add(right, gbc);

return panel;

}

public static void main(String[] args) {

AwtTooltip test = new AwtTooltip();

Frame f = new Frame();

f.addWindowListener(closer);

f.add(test.getContent(f));

f.setSize(300,100);

f.setLocation(200,200);

f.setVisible(true);

}

private static WindowListener closer = new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

};

}

class TipManager extends MouseMotionAdapter {

Panel component;

Window tooltip;

Label label;

public TipManager(Panel panel, Frame frame) {

component = panel;

panel.addMouseMotionListener(this);

initTooltip(frame);

}

/**

* Since enabled Buttons consume MouseEvents we will

* receive events sent only from disabled Buttons.

*/

public void mouseMoved(MouseEvent e) {

Point p = e.getPoint();

boolean hovering = false;

Component[] c = component.getComponents();

for(int j = 0; j < c.length; j++) {

Rectangle r = c[j].getBounds();

if(r.contains(p)) {

hovering = true;

if(!tooltip.isShowing())

showTooltip(c[j], p);

break;

}

}

if(!hovering && tooltip.isShowing()) {

tooltip.setVisible(false);

}

}

private void showTooltip(Component c, Point p) {

String text = ((Button)c).getLabel();

label.setText(text);

tooltip.pack();

convertPointToScreen(p, component);

tooltip.setLocation(p.x+10, p.y-15);

tooltip.setVisible(true);

}

/** Copied from SwingUtilities source code. */

public void convertPointToScreen(Point p, Component c) {

Rectangle b;

int x,y;

do {

if(c instanceof Window) {

try {

Point pp = c.getLocationOnScreen();

x = pp.x;

y = pp.y;

} catch (IllegalComponentStateException icse) {

x = c.getX();

y = c.getY();

}

} else {

x = c.getX();

y = c.getY();

}

p.x += x;

p.y += y;

if(c instanceof Window)

break;

c = c.getParent();

} while(c != null);

}

private void initTooltip(Frame owner) {

label = new Label();

label.setBackground(new Color(184,207,229));

tooltip = new Window(owner);

tooltip.add(label);

}

}

crwooda at 2007-7-29 14:07:04 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thank you so much,I'll try to implement this.

sunsdn123a at 2007-7-29 14:07:04 > top of Java-index,Desktop,Core GUI APIs...
# 3

I tried to implement the above logic, the code in the method mouseMoved() was never reached.

When mouse is moved onto the disabled button, no tool tip is displayed.

sunsdn123a at 2007-7-29 14:07:04 > top of Java-index,Desktop,Core GUI APIs...