comparison org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet289.d @ 28:69b1fa94a4a8

Added SWT snippets
author Frank Benoit <benoit@tionex.de>
date Sun, 22 Mar 2009 15:17:04 +0100
parents
children 536e43f63c81
comparison
equal deleted inserted replaced
27:1bf55a6eb092 28:69b1fa94a4a8
1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * D Port:
11 * Thomas Demmer <t_demmer AT web DOT de>
12 *******************************************************************************/
13 module org.eclipse.swt.snippets.Snippet289;
14
15 /*
16 * Combo example snippet: add an item to a combo box
17 *
18 * For a list of all SWT example snippets see
19 * http://www.eclipse.org/swt/snippets/
20 */
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.TraverseListener;
23 import org.eclipse.swt.events.VerifyListener;
24 import org.eclipse.swt.graphics.Point;
25 import org.eclipse.swt.layout.FillLayout;
26 import org.eclipse.swt.widgets.Combo;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.swt.widgets.Shell;
29 import java.lang.all;
30
31 void main(String[] args) {
32 Display display = new Display();
33 Shell shell = new Shell(display);
34 shell.setLayout(new FillLayout());
35 Combo combo = new Combo(shell, SWT.NONE);
36 combo.setItems(["1111", "2222", "3333", "4444"]);
37 combo.setText(combo.getItem(0));
38 combo.addVerifyListener(new class() VerifyListener{
39 public void verifyText(VerifyEvent e) {
40 String newText = e.text;
41 try {
42 Integer.parseInt(newText);
43 } catch (NumberFormatException ex) {
44 e.doit = false;
45 }
46 }
47 });
48 combo.addTraverseListener(new class() TraverseListener {
49 public void keyTraversed(TraverseEvent e) {
50 if (e.detail == SWT.TRAVERSE_RETURN) {
51 e.doit = false;
52 e.detail = SWT.TRAVERSE_NONE;
53 String newText = combo.getText();
54 try {
55 Integer.parseInt(newText);
56 combo.add(newText);
57 combo.setSelection(new Point(0, newText.length));
58 } catch (NumberFormatException ex) {
59 }
60 }
61 }
62 });
63
64 shell.pack();
65 shell.open();
66 while (!shell.isDisposed()) {
67 if (!display.readAndDispatch())
68 display.sleep();
69 }
70 display.dispose();
71 }