Label label = new Label(shell, SWT.NONE); label.setText("Click me!"); label.addMouseListener(new MouseListener() { public void mouseDoubleClick(MouseEvent e) { // Do nothing } public void mouseDown(MouseEvent e) { label.setText("Clicked!"); } public void mouseUp(MouseEvent e) { // Do nothing } });
Label label = new Label(shell, SWT.NONE); label.setText("Hover over me!"); label.addMouseListener(new MouseListener() { public void mouseDoubleClick(MouseEvent e) { // Do nothing } public void mouseDown(MouseEvent e) { // Do nothing } public void mouseUp(MouseEvent e) { // Do nothing } public void mouseEnter(MouseEvent e) { showMessage("Hello!"); } public void mouseExit(MouseEvent e) { shell.setToolTipText(null); } }); private void showMessage(String message) { Point cursorLocation = Display.getDefault().getCursorLocation(); shell.setToolTipText(message); shell.setLocation(cursorLocation.x + 10, cursorLocation.y + 10); }In this example, a new label is created with the text "Hover over me!". A mouse listener is added to the label with the addMouseListener method, which listens for mouse enter and mouse exit events. When the user hovers over the label, the showMessage method is called, which displays a message using the shell's tool tip. The message is positioned to appear next to the mouse cursor. When the user exits the label, the tool tip is removed with the mouseExit method. These code examples use the org.eclipse.swt.widgets package library.