Sandeep T D S Sandeep T D S -4 years ago 160
Java Question

Tooltip is not visible for a disabled Control in JAVA SWT

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");

Answer Source

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();
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download