Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c48901a68942b61aad9ad1256e06867e0acb6770 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*******************************************************************************
 * Copyright (c) 2011, 2013 IBM Corporation and others.
 * All rights reserved. This Example Content is intended to demonstrate
 * usage of Eclipse technology. It is provided to you under the terms and
 * conditions of the Eclipse Distribution License v1.0 which is available
 * at http://www.eclipse.org/org/documents/edl-v10.php
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.snippets;

/*
 * Combo getCaretLocation and getCaretPosition example
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Snippet359 {
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		final Combo combo = new Combo(shell, SWT.DROP_DOWN);
		combo.addKeyListener(new KeyListener() {
			@Override
			public void keyReleased(KeyEvent e) {
			   if (e.keyCode == SWT.CR) {
				   combo.add(combo.getText());
			   }
			}
			@Override
			public void keyPressed(KeyEvent e) {
				System.out.println("caret position: " + combo.getCaretPosition());
				System.out.println("caret location: " + combo.getCaretLocation());
			}
		});
	  shell.pack();
	  shell.open ();
	  while (!shell.isDisposed()) {
	   if (!display.readAndDispatch ()) display.sleep ();
	  }
	  display.dispose ();
	}
}

Back to the top