swt controls里的控件list,怎么显示滚动条,并且滚动条自动移动到最下边时,显示最新内容
1 package com.jokul; 2 3 import org.eclipse.swt.widgets.Display; 4 import org.eclipse.swt.widgets.Shell; 5 import org.eclipse.swt.SWT; 6 import org.eclipse.swt.widgets.Label; 7 import org.eclipse.swt.widgets.List; 8 import org.eclipse.swt.widgets.ScrollBar; 9 10 public class ListTest { 11 12 protected Shell shell; 13 protected static List list; 14 15 /** 16 * Launch the application. 17 * @param args 18 */ 19 public static void main(String[] args) { 20 try { 21 ListTest window = new ListTest(); 22 window.open(); 23 } catch (Exception e) { 24 e.printStackTrace(); 25 } 26 } 27 28 /** 29 * Open the window. 30 */ 31 public void open() { 32 Display display = Display.getDefault(); 33 createContents(); 34 shell.open(); 35 shell.layout(); 36 while (!shell.isDisposed()) { 37 if (!display.readAndDispatch()) { 38 display.sleep(); 39 } 40 } 41 } 42 43 /** 44 * Create contents of the window. 45 */ 46 protected void createContents() { 47 shell = new Shell(); 48 shell.setSize(450, 300); 49 shell.setText("SWT Application"); 50 51 list = new List(shell, SWT.BORDER | SWT.V_SCROLL); 52 list.setItems(new String[] {}); 53 list.setBounds(10, 35, 414, 217); 54 55 Label label = new Label(shell, SWT.NONE); 56 label.setBounds(10, 10, 343, 17); 57 label.setText("\u6F14\u793A\u6EDA\u52A8\u6761\u600E\u4E48\u79FB\u52A8\u5230\u6700\u4E0B\u8FB9\u7684"); 58 59 for(int i = 0; i < 100; i++) { 60 showInList("info:" + i); 61 } 62 } 63 64 /** 65 * 在swt的list上显示信息 66 * @param str 67 */ 68 private static void showInList(final String str){ 69 Display.getDefault().asyncExec(new Runnable() { 70 public void run() { 71 list.add(str); 72 73 int count = list.getItemCount(); 74 list.setSelection(count - 1); 75 76 ScrollBar sb = list.getVerticalBar(); 77 if(sb !=null && sb.isVisible()) { 78 sb.setSelection(sb.getMaximum()); 79 } 80 } 81 }); 82 83 } 84 }
时间: 2024-11-06 21:33:12