【问题标题】:SWT Table inside ScrolledCompsite: no scrolling by Mouse WheelScrolledCompsite 内的 SWT 表:鼠标滚轮不滚动
【发布时间】:2021-02-18 15:25:40
【问题描述】:

以下代码摘自: How to always show vertical scroll bar in SWT table?

ScrolledComposite 中的Table 只能通过鼠标移动垂直滚动条或将鼠标直接放在垂直滚动条上并滚动滚轮来滚动。 请您建议如何通过将鼠标放在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();
}

【问题讨论】:

  • 注意:ScrolledComposite 的“始终显示滚动条”选项在 macOS 上不起作用,无论如何操作系统都会一直隐藏滚动条。此示例代码在 macOS 上根本不滚动(并且在没有 ScrolledComposite 的情况下也能正常工作)。混合使用 ScrolledComposite 和 Table 似乎总是会导致问题。
  • @greg-449,谢谢。在我的情况下,这条线 composite.setAlwaysShowScrollBars(true); 可以删除 - 它对根本问题没有任何贡献。我想通过将鼠标滚轮直接放在Table 上来阐明如何在ScrolledComposite 内滚动Table(当存在垂直滚动条时)。
  • 顺便说一下,stackoverflow.com/a/28343170/6863550 的解决方案在我的情况下不起作用(我在 Windows 10 下工作)

标签: swt mousewheel scrolledcomposite


【解决方案1】:

表格正在接收它忽略的鼠标滚轮事件,因为表格控件足够大以显示所有行。

我看不到将轮子事件传递给滚动复合体的方法。

您可以尝试收听表格中的轮子事件并调整滚动的复合原点 - 类似于:

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

wheel 事件中的count 字段为 1/-1,具体取决于滚动方向。

【讨论】:

  • 现在它可以正常工作了!这是一个棘手的解决方案。谢谢你,@greg-449!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 2013-04-20
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 2012-06-07
  • 1970-01-01
相关资源
最近更新 更多