在swt中按下按钮时如何在列表中搜索字符串
Posted
技术标签:
【中文标题】在swt中按下按钮时如何在列表中搜索字符串【英文标题】:How to search for a string in the list when the button is pressed in swt 【发布时间】:2015-02-21 06:02:57 【问题描述】:我创建了一个选项卡文件夹并创建了一些选项卡,在选项卡中我有一个包含列表的组合。所以我有一个文本框,所以当在文本框中键入字符串并按下查找按钮时,列表应该显示包含字符串,那么如何做到这一点。
创建文本框和查找按钮的代码如下
public void postConstruct (Composite parentComposite)
parentComposite.setLayout(new GridLayout(1, true));
Composite topComposite = new Composite(parentComposite, SWT.NONE);
topComposite.setLayout(new GridLayout(2, false));
topComposite.setLayoutData(new GridData(SWT.FILL, GridData.FILL, true, true));
Composite bottomComposite = new Composite(parentComposite, SWT.NONE);
GridData bottomCompositeGD = new GridData(SWT.FILL, SWT.BOTTOM, true, false);
bottomCompositeGD.heightHint = 100;
bottomComposite.setLayout(new GridLayout(2, false));
bottomComposite.setLayoutData(bottomCompositeGD);
Composite topLeftComposite = new Composite(topComposite, SWT.NONE);
topLeftComposite.setLayout(new GridLayout(1, false));
topLeftComposite.setLayoutData(new GridData(SWT.FILL, GridData.FILL, false, true));
Composite topRightComposite = new Composite(topComposite, SWT.NONE);
topRightComposite.setLayout(new GridLayout(1, false));
topRightComposite.setLayoutData(new GridData(SWT.FILL, GridData.FILL, true, true));
Composite findTCComposite = new Composite(topRightComposite, SWT.NONE);
findTCComposite.setLayout(new GridLayout(2, true));
findTCComposite.setLayoutData(new GridData(SWT.FILL, GridData.FILL, true, false));
txtFindTC = new Text(findTCComposite, SWT.BORDER | SWT.WRAP | SWT.SINGLE);
txtFindTC.setLayoutData(new GridData(SWT.FILL, GridData.FILL, true, false));
Button btnFindTC = new Button(findTCComposite, SWT.NONE);
btnFindTC.setLayoutData(new GridData(SWT.BEGINNING, GridData.BEGINNING, false, false));
btnFindTC.setText("Find TC");
TabFolder tabFolder = new TabFolder(topRightComposite, SWT.NONE);
tabFolder.setLayoutData(new GridData(SWT.FILL, GridData.FILL, true, true));
TabItem itemBasicTC = new TabItem(tabFolder, SWT.NONE);
itemBasicTC.setText("Basic TCs");
createListViewBasicTc(tabFolder,itemBasicTC);
the the tab is created is below
private void createListViewBasicTc(Composite composite,TabItem itemBasicTC)
final List list = new List(composite, SWT.BORDER);
itemBasicTC.setControl(list);
creatingDragSource(list);
//final java.util.List<String> listSort = new ArrayList<String>();
//for(int i = 1; i <=10 ; i++)
list.add("TestCase
list.add("Test_)");
list.add("something");
所以现在当用户在文本框中输入“some”并按下选项卡中的查找按钮时,它应该只在选项卡中显示“something”。那么如何做到这一点。
【问题讨论】:
【参考方案1】:试试这样的
button.addListener (SWT.Selection, new Listener()
public void handleEvent(Event e)
//This depends on your search criteria
String search = "A";
Boolean found = false;
for (String str : list)
if (str.trim().contains(search))
return true;
if (!found)
return false;
);
【讨论】:
以上是关于在swt中按下按钮时如何在列表中搜索字符串的主要内容,如果未能解决你的问题,请参考以下文章
在Android中按下提交按钮时使用自定义适配器从列表视图中获取选定项目
如何防止在android片段中按下后退按钮时调用onCreateView