SWT的滚动条为什么不能自动滚动? 谢谢
以下是我做的SWT的一个滚动控件,但是滚动条为什么不自动滚动到底部呢?
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Test extends Dialog {
public Text text;
protected Object result;
protected Shell shell;
int i = 0;
/**
* Create the dialog
* @param parent
* @param style
*/
public Test(Shell parent, int style) {
super(parent, style);
}
/**
* Create the dialog
* @param parent
*/
public Test(Shell parent) {
this(parent, SWT.NONE);
}
/**
* Open the dialog
* @return the result
*/
public Object open() {
createContents();
shell.open();
shell.layout();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return result;
}
/**
* Create contents of the dialog
*/
protected void createContents() {
shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
shell.setLayout(new FillLayout());
shell.setSize(500, 375);
shell.setText("SWT Dialog");
final List list = new List(shell, SWT.V_SCROLL);
new Thread(){
public void run(){
try {
Thread.sleep(4*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
Display.getDefault().asyncExec(new Runnable(){
public void run(){if(list.getItemCount()<50){
list.add("sdfsdf fsdfasd ffsdfasdf fsdfsdf sfsdf sd"+ i++ +" \r\n");
}else{
list.remove(0);
list.add("sdfsdf fsdfasd ffsdfasdf fsdfsdf sfsdf sd"+ i++ +" \r\n");
}
System.out.println("Increment:" + list.getVerticalBar().getIncrement());
System.out.println("getMaximum:" + list.getVerticalBar().getMaximum());
System.out.println("getMinimum:" + list.getVerticalBar().getMinimum());
System.out.println("getPageIncrement:" + list.getVerticalBar().getPageIncrement());
System.out.println("getThumb:" + list.getVerticalBar().getThumb());
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
//
}
public static void main(String args[]){
final Test test = new Test(new Shell(Display.getDefault()));
test.open();
}
}
[解决办法]
ScrollBar sb = list.getVerticalBar();
if(sb != null && sb.isVisible()){
sb.setSelection(sb.getMaximum());
}
[解决办法]
一楼二楼的答案我试过了,都可以的