SWT左右List移除和增加

使用内部类方式处理的好处是可以再一个类中队不同的事件集中处理!

package com.layotech.www.study;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

public class ListSample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("List示例");
String[] itemLeft = new String[20];
//String[] itemRight = new String[0];
for(int i=0;i<20;i++){
itemLeft[i] = "item" + i;
}
final List left = new List(shell, SWT.MULTI|SWT.V_SCROLL);
left.setBounds(10, 10, 100, 180);
left.setItems(itemLeft);
left.setToolTipText("请设置列表项");
final List right = new List(shell, SWT.MULTI|SWT.V_SCROLL);
right.setBounds(170, 10, 100, 180);
right.setToolTipText("已选择的列表项");
//创建事件监听,为内部类
SelectionAdapter listener = new SelectionAdapter() {
public void widgetSelected(SelectionEvent e){
//取得触发事件的控件对象
Button b = (Button) e.widget;
if(b.getText().equals(">")){
verifyValue(left.getSelection(),left,right);
}else if(b.getText().equals(">>")){
verifyValue(left.getItems(),left,right);
}else if(b.getText().equals("<")){
verifyValue(right.getSelection(),right, left);
}else if(b.getText().equals("<<")){
verifyValue(right.getItems(),right, left);
}else if(b.getText().equals("上")){
//获得当前选择选项的索引值
int index = right.getSelectionIndex();
if(index<=0){
return;
}
//如果选择了,获得当前选项值
String currentValue = right.getItem(index);
//交换
right.setItem(index, right.getItem(index-1));
right.setItem(index-1, currentValue);
right.setSelection(index-1);
}else if(b.getText().equals("下")){
//获得当前选择选项的索引值
int index = right.getSelectionIndex();
if(index >= right.getItemCount()-1){
return;
}
//如果选择了,获得当前选项值
String currentValue = right.getItem(index);
//交换
right.setItem(index, right.getItem(index+1));
right.setItem(index+1, currentValue);
right.setSelection(index+1);
}
}
public void verifyValue(String[] select,List from,List to){
for(int i=0;i<select.length;i++){
//从一个列表移除该选项
from.remove(select[i]);
to.add(select[i]);
}
}
};
Button bt1 = new Button(shell, SWT.NONE);
bt1.setText(">");
bt1.setBounds(130, 20, 25, 20);
bt1.addSelectionListener(listener);
Button bt2 = new Button(shell, SWT.NONE);
bt2.setText(">>");
bt2.setBounds(130, 55, 25, 20);
bt2.addSelectionListener(listener);
Button bt3 = new Button(shell, SWT.NONE);
bt3.setText("<<");
bt3.setBounds(130, 90, 25, 20);
bt3.addSelectionListener(listener);
Button bt4 = new Button(shell, SWT.NONE);
bt4.setText("<");
bt4.setBounds(130, 125, 25, 20);
bt4.addSelectionListener(listener);
Button bt5 = new Button(shell, SWT.NONE);
bt5.setText("上");
bt5.setBounds(300, 70, 25, 20);
bt5.addSelectionListener(listener);
Button bt6 = new Button(shell, SWT.NONE);
bt6.setText("下");
bt6.setBounds(300, 105, 25, 20);
bt6.addSelectionListener(listener);
shell.setSize(350,250);
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值