zoukankan      html  css  js  c++  java
  • swt controls里的控件list

    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("u6F14u793Au6EDAu52A8u6761u600Eu4E48u79FBu52A8u5230u6700u4E0Bu8FB9u7684");
    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 }
  • 相关阅读:
    18 | 为什么这些SQL语句逻辑相同,性能却差异巨大?
    17 | 如何正确地显示随机消息?
    16 | “order by”是怎么工作的?
    15 | 答疑文章(一):日志和索引相关问题
    14 | count(*)这么慢,我该怎么办?
    13 | 为什么表数据删掉一半,表文件大小不变?
    12 | 为什么我的MySQL会“抖”一下?
    11 | 怎么给字符串字段加索引?
    10 | MySQL为什么有时候会选错索引?
    2016 Multi-University Training Contest 4
  • 原文地址:https://www.cnblogs.com/jokulfox/p/4112035.html
Copyright © 2011-2022 走看看