如何检测SWT Table的滚动条可见性变化

发布于 2024-11-01 20:41:56 字数 86 浏览 3 评论 0原文

如何检测 Java SWT Table 的垂直 ScrollBar 何时变得可见?我需要这些信息来重新计算列的宽度。似乎滚动条上没有触发任何事件(除了选择)。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞花火° 2024-11-08 20:41:56

我认为您已经正确地发现,没有简单的方法可以检测垂直 ScrollBar 何时可见。无论如何,这里提供的解决方案是一种 hack。

我正在使用此 SWT 代码段 计算表中的可见行。除此之外,我还使用 SWT Paint Event

基本概念如下:

  1. 计算可见行(项目)的数量。
  2. 将其与总行数(项目数)进行比较。
  3. 在添加行(项目)时发生的某些事件中执行所有这些操作。我选择了 SWT Paint Event

>>>代码

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();
    }

}

>>输出

对于itemcount <可见行数

sample 1

对于 itemcount >= 可见行数

sample 2

注意- 如果您要使用绘制事件,请尝试在调用时保持最小计算量频繁地。

希望这会有所帮助。

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.

心舞飞扬 2024-11-08 20:41:56

这对我有用:

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

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 );
木格 2024-11-08 20:41:56

我发现,在调整大小通知后,您可以获取可滚动的边界和客户区域之间的差异。任一维度的差异都表明存在滚动条。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文