How to get ToolTip in a ListViewer in JFace?

2019-08-08 19:56发布

问题:

This is a gridLayout in which the Available side is a Tree Viewer and The Selected side is a ListViewer. Now I have to get a toolTip on the right hand side. Which I am unable to get. I am working on a existing code base , so I am unable to figure out on which line did they add a tooltip + I did not find any keywords like tooltip or mouse Hover. Still how is this implemented. I am mentioning some code. I believe the answer should be somewhere here only.

 availableViewer = new TreeViewer(resultsComposite, SWT.BORDER | this.getStyle());
        availableViewer.setContentProvider(new ResAndResGroupTreeContentProvider());
        availableViewer.setLabelProvider(SelectionItemLabelProvider.getInstance());
        Tree availableResults = availableViewer.getTree();
        GridData availableResultsGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
        availableResultsGridData.widthHint = LIST_WIDTH_HINT;
        availableResultsGridData.heightHint = LIST_HEIGHT_HINT;
        availableResults.setLayoutData(availableResultsGridData);
        availableViewer.getTree().addSelectionListener(new SelectionAdapter()
        {
            @Override
            public void widgetDefaultSelected(SelectionEvent e)
            {
                moveAvailableItemsToSelected();
            }
        });

This is the selectionViewer content.

  selectedViewer = new ListViewer(resultsComposite, SWT.V_SCROLL | SWT.H_SCROLL| SWT.BORDER
            | this.getStyle());
    selectedViewer.setContentProvider(new ResAndResGroupTreeContentProvider());
    selectedViewer.setLabelProvider(new SelectionItemLabelProvider());
    org.eclipse.swt.widgets.List selectedResults = selectedViewer.getList();
    GridData selectedResultsGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
    selectedResultsGridData.widthHint = LIST_WIDTH_HINT;
    selectedResultsGridData.heightHint = LIST_HEIGHT_HINT;
    selectedResults.setLayoutData(selectedResultsGridData);
    selectedViewer.addDoubleClickListener(new IDoubleClickListener()
    {
        @Override
        public void doubleClick(DoubleClickEvent event)
        {
            moveSelectedItemsToAvailable();
        }
    });
    selectedViewer.getList().addKeyListener(new KeyAdapter()
    {
        @Override
        public void keyPressed(KeyEvent e)
        {
            if (e.character == SWT.CR)
            {
                moveSelectedItemsToAvailable();
            }
        }
    });
    selectedViewer.addSelectionChangedListener(new ISelectionChangedListener()
    {
        @Override
        public void selectionChanged(SelectionChangedEvent event)
        {
            updateButtonsEnabled();
        }
    });

Thanks.

回答1:

The ListViewers underlying List widget cannot show different tooltips for each item. You can assign a tooltip for the entire list like so

listViewer.getList().setTooltipText( "..." );

But if you want a differnt tooltip per item you'll have to use a TableViewer.

What you see on the left side is the native Windows tooltip that appears if an item exceeds the horizontal space. The Table (on Windows) has the same behavior, thus you don't need to explicitly provide tooltips.

For a control that has a default tool tip, such as the Tree on Windows, setting the tooltip to null replaces the default, causing no tooltip to be shown.