50,527
社区成员
发帖
与我相关
我的任务
分享
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class TestList {
protected Shell shell;
/**
* Launch the application
*
* @param args
*/
public static void main(String[] args) {
try {
TestList window = new TestList();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
/**
* Create contents of the window
*/
protected void createContents() {
shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
final List list = new List(shell, SWT.BORDER | SWT.V_SCROLL);
// list.select(0);
list.setItems(new String[] { "第0", "第1", "第2", "第3", "第4" });
list.setBounds(0, 0, 50, 200);
list.setSelection(0);
list.addKeyListener(new KeyAdapter() {
int lastIndex = -1;
public void keyReleased(KeyEvent e) {
int curIndex = list.getSelectionIndex();
System.out.println("c=" + curIndex);
int total = list.getItemCount();
if (e.keyCode == SWT.ARROW_UP) {
if (lastIndex == 0) {
list.select(total - 1);
curIndex = total-1;
list.showSelection();
int c = list.getSelectionIndex();
System.out.println(c);
}
} else if (e.keyCode == SWT.ARROW_DOWN) {
if (lastIndex == total - 1) {
list.select(0);
curIndex = 0;
list.showSelection();
int c = list.getSelectionIndex();
System.out.println(c);
}
}
lastIndex = curIndex;
}
});
//
}
}
list.select(-1);