0

The code below is taken from: How to always show vertical scroll bar in SWT table?

Table in the ScrolledComposite scrolls only by moving vertical scrollbar by Mouse or by putting Mouse directly onto vertical scrollbar and scroll the Wheel. Could you advise, please, how to make table scrollable by Mouse Wheel by putting Mouse onto the Table?

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());

    final ScrolledComposite composite = new ScrolledComposite(shell, SWT.V_SCROLL);
    composite.setLayout(new GridLayout());
    composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    final Table table = new Table(composite, SWT.NO_SCROLL | SWT.FULL_SELECTION);
    table.setHeaderVisible(true);

    composite.setContent(table);
    composite.setExpandHorizontal(true);
    composite.setExpandVertical(true);
    composite.setAlwaysShowScrollBars(true);
    composite.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));

    Button fillTable = new Button(shell, SWT.PUSH);
    fillTable.setText("Fill table");
    fillTable.setLayoutData(new GridData(SWT.FILL, SWT.END, true, false));

    fillTable.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            if (table.getColumnCount() < 1)
            {
                for (int col = 0; col < 4; col++)
                {
                    TableColumn column = new TableColumn(table, SWT.NONE);
                    column.setText("Column " + col);
                }
            }

            for (int row = 0; row < 20; row++)
            {
                TableItem item = new TableItem(table, SWT.NONE);

                for (int col = 0; col < table.getColumnCount(); col++)
                {
                    item.setText(col, "Item " + row + " " + col);
                }
            }

            for (int col = 0; col < table.getColumnCount(); col++)
            {
                table.getColumn(col).pack();
            }

            composite.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        }
    });

    Button clearTable = new Button(shell, SWT.PUSH);
    clearTable.setText("Clear table");
    clearTable.setLayoutData(new GridData(SWT.FILL, SWT.END, true, false));

    clearTable.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            table.removeAll();

            composite.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        }
    });

    shell.pack();
    shell.setSize(400, 300);
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
  • Note: That 'always show scroll bars' option of ScrolledComposite doesn't work on macOS, the OS hides scroll bars all the time regardless. This example code doesn't scroll at all on macOS (and works fine without the ScrolledComposite). Mixing ScrolledComposite and Table always seems to cause problems. – greg-449 Feb 18 '21 at 15:50
  • @greg-449, thank you. This line `composite.setAlwaysShowScrollBars(true);` can be removed in my case - it contributes nothing to the root issue. What I want to clarify how to scroll `Table` inside the `ScrolledComposite` (when the Vertical Scroll Bar exists) by putting Mouse Wheel directly onto the `Table`. – Volodymyr Patriiuk Feb 18 '21 at 15:57
  • By the way, the solution https://stackoverflow.com/a/28343170/6863550 doesn't work in my case (I'm working under Windows 10) – Volodymyr Patriiuk Feb 18 '21 at 16:13

1 Answers1

0

The table is receiving the mouse wheel events which it is ignoring because the Table control is large enough to show all the rows.

I don't see a way to just pass on the wheel events to the scrolled composite.

You could try listening to the wheel events in the table and adjusting the scrolled composite origin - something like:

table.addListener(SWT.MouseVerticalWheel, event ->
   {
     Point origin = scrolled.getOrigin();
     
     origin.y -= event.count;
     
     scrolled.setOrigin(origin);
   });

The count field in the wheel event is 1/-1 depending on the scrolling direction.

greg-449
  • 109,219
  • 232
  • 102
  • 145