swt controls里的控件list

时间:2024-04-11 00:06:46

swt controls里的控件list,怎么显示滚动条,并且滚动条自动移动到最下边时,显示最新内容

 package com.jokul;

 import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.ScrollBar; public class ListTest { protected Shell shell;
protected static List list; /**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
ListTest window = new ListTest();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} /**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application"); list = new List(shell, SWT.BORDER | SWT.V_SCROLL);
list.setItems(new String[] {});
list.setBounds(10, 35, 414, 217); Label label = new Label(shell, SWT.NONE);
label.setBounds(10, 10, 343, 17);
label.setText("\u6F14\u793A\u6EDA\u52A8\u6761\u600E\u4E48\u79FB\u52A8\u5230\u6700\u4E0B\u8FB9\u7684"); for(int i = 0; i < 100; i++) {
showInList("info:" + i);
}
} /**
* 在swt的list上显示信息
* @param str
*/
private static void showInList(final String str){
Display.getDefault().asyncExec(new Runnable() {
public void run() {
list.add(str); int count = list.getItemCount();
list.setSelection(count - 1); ScrollBar sb = list.getVerticalBar();
if(sb !=null && sb.isVisible()) {
sb.setSelection(sb.getMaximum());
}
}
}); }
}