comparison snippets/combo/Snippet289.d @ 119:9a1be6ff19a2

More snippets, thanks to Tom D.
author Frank Benoit <benoit@tionex.de>
date Sun, 20 Jul 2008 15:26:06 +0200
parents
children
comparison
equal deleted inserted replaced
118:7b1c122b4128 119:9a1be6ff19a2
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 combo.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 dwt.DWT;
22 import dwt.events.TraverseListener;
23 import dwt.events.VerifyListener;
24 import dwt.graphics.Point;
25 import dwt.layout.FillLayout;
26 import dwt.widgets.Combo;
27 import dwt.widgets.Display;
28 import dwt.widgets.Shell;
29 import dwt.dwthelper.utils;
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, DWT.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 == DWT.TRAVERSE_RETURN) {
51 e.doit = false;
52 e.detail = DWT.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 }