Tooltip is not visible for a disabled Control in JAVA SWT
Asked Answered
I

3

5

I am using JAVA SWT for the GUI of my JAVA application.

Now i have set a checkbox to be disabled, but i want to show the tooltip for the same.

Is this possible?

My code is:

myCheckbox.setSelection(false);
myCheckbox.setEnabled(false);
myCheckbox.setToolTipText("Tooltip message");
Ignorant answered 23/8, 2016 at 8:39 Comment(0)
Z
8

As pointed out by greg-449 in his answer, it is not possible.

But if you really want to, you could workaround this limitation by encapsulating your checkbox in a Composite with the same tooltip text.

This method was proposed by Andrzej Witecki in this Eclipse forum topic.

An example:

public static void main(String[] args) {

    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());

    Composite c = new Composite(shell, SWT.NONE);
    c.setLayoutData(new GridData());  // default values so it doesn't grab excess space
    c.setLayout(new FillLayout());

    Button myCheckbox = new Button(c, SWT.CHECK);
    myCheckbox.setText("Checkbox text");
    myCheckbox.setToolTipText("Tooltip message");
    myCheckbox.setEnabled(false);

    // assign the same tooltip to the encapsulating composite
    myCheckbox.getParent().setToolTipText(myCheckbox.getToolTipText());  

    shell.setSize(200, 200);
    shell.open();

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}
Zetland answered 23/8, 2016 at 13:22 Comment(1)
Hi Loris, I came across this solution elsewhere and found it to be useful. Though not a perfect solution, it seems to be an effective workaroundIgnorant
D
1

No, this isn't possible.

Disabled controls don't generate the events necessary to display a tool tip.

Duston answered 23/8, 2016 at 9:52 Comment(2)
Hi Greg, thanks for the response. Could you mention your reference. We need to know if it is a limitation on SWTIgnorant
I don't know of a reference off hand but it is definitely the case that SWT doesn't generate events on disabled controls.Duston
C
-1

If you create a specific composite for your disabled component (button, checkbox, etc...) and add a tooltip on the composite, it will be displayed when component is disabled.

Don't forget to add the tooltip for the component too if you also want to display it when it is enabled.

Causeway answered 1/3, 2017 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.