Jean-Philippe Pellet
Jean-Philippe Pellet

Reputation: 60006

How to detect SWT Table's scroll bar visibility changes

How can I detect when a Java SWT Table's vertical ScrollBar becomes visible? I need that information to recompute the columns' widths. Seems like no event (besides Selection) is ever fired on the ScrollBars.

Upvotes: 1

Views: 5482

Answers (3)

Alex
Alex

Reputation: 414

This works for me:

 boolean isScrollVisible = table.getVerticalBar().getVisible();
 Point vBarSize = table.getVerticalBar().getSize();
 int width_diff = 
      current_width.x - totalColumnWidth - (isScrollVisible ? vBarSize.x : 0 );

Upvotes: 2

Eric Vasilik
Eric Vasilik

Reputation: 372

I found that, upon the resize notification, you can take the difference between the bounds and the client area of a Scrollable. A difference in either dimension should suggest the presence of a scrollbar.

Upvotes: 0

Favonius
Favonius

Reputation: 13984

I think you have correctly found out that there is no easy way of detecting when the vertical ScrollBar is visible. Anyway the solution here provided is kind of hack.

I am using the concept presented in this SWT snippet compute the visible rows in a table. Along with that I am also using SWT Paint Event.

The basic concept is like as follows:

  1. Calculate the number of visible rows (items).
  2. Compare it with total number of rows (items).
  3. Do all this in some event which occurs with the addition of rows (items). I have chosen the SWT Paint Event

>> Code

import org.eclipse.swt.*;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class TableScrollVisibilityTest 
{
    private static int count;

    public static void main(String [] args) 
    {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setBounds(10,10,300,300);
        shell.setLayout(new GridLayout(2,true));

        final Table table = new Table(shell, SWT.NONE);
        GridData data = new GridData(GridData.FILL_BOTH);
        data.horizontalSpan = 2;
        table.setLayoutData(data);

        count = 0;

        final Button addItem = new Button (shell, SWT.PUSH);
        addItem.setText ("Add Row");
        data = new GridData(SWT.FILL, SWT.FILL, true, false);
        data.horizontalSpan = 2;
        addItem.setLayoutData(data);

        final Text text = new Text(shell, SWT.BORDER);
        text.setText ("Vertical Scroll Visible - ");
        data = new GridData(SWT.FILL, SWT.FILL, true, false);
        data.horizontalSpan = 2;
        text.setLayoutData(data);


        addItem.addListener (SWT.Selection, new Listener () 
        {
            public void handleEvent (Event e) 
            {
                new TableItem(table, SWT.NONE).setText("item " + count);
                count++;
            }
        });


        table.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                Rectangle rect = table.getClientArea ();
                int itemHeight = table.getItemHeight ();
                int headerHeight = table.getHeaderHeight ();
                int visibleCount = (rect.height - headerHeight + itemHeight - 1) / itemHeight;
                text.setText ("Vertical Scroll Visible - [" + (table.getItemCount()>= visibleCount)+"]");

                      // YOUR CODE HERE
            }
        });


        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }

        display.dispose();
    }

}

>> Output

For itemcount < numberofvisible rows

sample 1

For itemcount >= numberofvisible rows

sample 2

Note- If you are going to use the paint event then try keep the calculations minimum as it is called frequently.

Hope this will help.

Upvotes: 3

Related Questions