[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: open combobox' dropdown programmatically?

Hi Arne,

if Win32-only is an option for you, you might try out this little snippet.

Regards,

Boris

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;


public class CComboDemo {

/**
* Open a Combo programatically (Win32 only)
* * @param combo
* Combo
* @param show
* (boolean) popped up or not?
*/
private static void showPopup(Combo combo, boolean show) {
if (!combo.isDisposed()) {
OS.SendMessage(combo.handle, OS.CB_SHOWDROPDOWN, show ? 1 : 0, 0);
}
}


/**
* Has the Combo already popped up? (Win32 only)
* * @param combo
* Combo
* @return (boolean) popped up or not?
*/
private static boolean isPopupShowing(Combo combo) {
boolean result = false;
if (!combo.isDisposed()) {
result = (OS.SendMessage(combo.handle, OS.CB_GETDROPPEDSTATE, 0, 0) != 0);
}
return result;
}


	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("Demonstrates drop down on focus in");
		
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		shell.setLayout(gridLayout);
		
		Text text = new Text(shell, SWT.BORDER);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		text.setLayoutData(gridData);
		
		final Combo combo = new Combo(shell, SWT.READ_ONLY);
		for(int i = 0; i < 10; i++){
			combo.add("ComboItem " + i);
		};
		combo.select(0);
		combo.setLayoutData(gridData);
		combo.addListener(SWT.FocusIn, new Listener(){
			public void handleEvent(Event e){
				showPopup(combo, !isPopupShowing(combo));
			}
		});
		
		Button button = new Button(shell, SWT.PUSH);
		button.setText("Push Me");
		button.setLayoutData(gridData);
		
		shell.setSize(330, 100);
		
		Monitor primary = Display.getDefault().getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shell.getBounds();
		Point pt = new Point(bounds.x + (bounds.width - rect.width) / 2,
				bounds.y + (bounds.height - rect.height) / 2);
		shell.setLocation(pt);
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}