如何检测Java SWT表的垂直ScrollBar何时可见?我需要该信息来重新计算列的宽度。似乎没有事件(除了Selection)在ScrollBars上被触发。
答案 0 :(得分:3)
我认为您已经正确地发现没有简单的方法来检测when the vertical ScrollBar is visible
。无论如何,这里提供的解决方案是一种黑客攻击。
我正在使用此SWT代码段compute the visible rows in a table中提供的概念。除此之外,我还使用SWT Paint Event
。
基本概念如下:
SWT Paint Event
<强>&GT;&GT;代码强>
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();
}
}
<强>&GT;&GT;输出强>
itemcount&lt; numberofvisible行
itemcount&gt; = numberofvisible行
注意 - 如果您要使用绘制事件,请尝试将计算保持在最小值,因为它经常被调用。
希望这会有所帮助。
答案 1 :(得分:2)
这对我有用:
boolean isScrollVisible = table.getVerticalBar().getVisible();
Point vBarSize = table.getVerticalBar().getSize();
int width_diff =
current_width.x - totalColumnWidth - (isScrollVisible ? vBarSize.x : 0 );
答案 2 :(得分:0)
我发现,在调整大小通知时,您可以获取Scrollable的边界和客户区之间的差异。任何一个维度的差异都应该表明存在滚动条。