Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Setting the location of a hover shell(Want to control the location of a Shell that appear on hovering over a Control)
Setting the location of a hover shell [message #1844889] Tue, 28 September 2021 22:32
Lee Brownston is currently offline Lee BrownstonFriend
Messages: 2
Registered: April 2021
Junior Member
My task is to indicate to the user that a Label is editable by displaying the Image of a pencil near the Label when the mouse cursor hovers over the Label. I found some code on the web for a "HoverShell", and I'm trying to adapt that. The shell does appear, but not close enough to the Label. My attempts to call Shell#setLocation(Point) resulted in the Shell appearing even farther away from the Label, so it appears that I don't know what the Point argument should be.

Pardon my naivete, but I'm very new to SWT and haven't yet found the documentation that might answer my questions.

Here is the HoverShell class:

/**
* Near a Control, display a shell containing a text or Image Label.
*
* @see https://sites.google.com/site/javatipstocode/how-to/how-to-display-hover-text-in-swt
* @see https://stackoverflow.com/questions/55901943/how-to-show-hover-text-for-a-swt-text-box
* @see https://www.tabnine.com/web/assistant/code/rs/5c667bed1095a50001c842a8#L203
*/
class HoverShell {

/** The shell that will be shown upon hover. */
private final Shell theShell;
/** The only Control in the shell; contains the text or Image */
private final Label label;

private HoverShell(final Control control) {
Objects.requireNonNull(control);

// Class Shell can't be subclassed, so we have to compose
theShell = new Shell(control.getShell(), SWT.ON_TOP | SWT.TOOL);
// Set the foreground and background colors to be those of the parent
Display display = control.getShell().getDisplay();
theShell.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
theShell.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
theShell.setLayout(new FillLayout());
// Create the label
label = new Label(theShell, SWT.NONE);
}

/** With an Image label */
public HoverShell(final Control control, final Image image) {
this(control);
Objects.requireNonNull(image);
label.setImage(image);
theShell.pack();
setLocation(control);
}

/** With a text label */
public HoverShell(final Control control, final String text) {
this(control);
Objects.requireNonNull(text);
label.setText(text);
theShell.pack();
setLocation(control);
}

private void setLocation(final Control control) {
// TODO(LSB): This is doing something, but not what I want
Shell controlShell = control.getShell();
Rectangle controlShellBounds = controlShell.getBounds();
// X is to the right of the control; Y is even with the control
theShell.setLocation(control.toDisplay(controlShellBounds.x + controlShellBounds.width, controlShellBounds.y));
}

public void open() {
theShell.pack();
theShell.open();
}

public boolean isVisible() {
return theShell.isVisible();
}

public void setVisible(final boolean b) {
if (theShell.isVisible() != b) {
theShell.setVisible(b);
}
}

And here is how I am using its public interface. Both "outerNameText" and "outerDescText" are of type Label, that have a text String and no Image.

if (outerNameText != null) {
HoverShell imageHoverShell =
new HoverShell(outerNameText, pencilImage);
outerNameText.addListener(
SWT.MouseHover, event -> imageHoverShell.open());
outerNameText.addListener(
SWT.MouseExit, event -> imageHoverShell.setVisible(false));
}
if (outerDescText != null) {
HoverShell textHoverShell = new HoverShell(outerDescText, "Editable");
outerDescText.addListener(
SWT.MouseHover, event -> textHoverShell.open());
outerDescText.addListener(
SWT.MouseExit, event -> textHoverShell.setVisible(false));
}
Previous Topic:U2F hardware support in SWT Browser
Next Topic:Strange problem when Mac is set to Dark and Eclipse theme is Light in RCP app
Goto Forum:
  


Current Time: Mon May 06 12:17:49 GMT 2024

Powered by FUDForum. Page generated in 0.02738 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top