[英]JAVA: How to set a string value to be returned, when a button is pressed?
[英]How to search for a string in the list when the button is pressed in swt
我创建了一个Tabfolder并创建了一些选项卡,并且在选项卡中我有一个包含列表的组合。所以我有一个文本框,因此当在文本框中键入字符串并按下查找按钮时,列表应显示为包含字符串,因此如何才能做到这一点。
创建文本框和查找按钮的代码如下
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”。那么该怎么做。
试试这个
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;
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.