Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Runnable SWT / JOGL class demonstrating ability to create SWT Menu over SWT GLCanvas
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import com.jogamp.opengl.swt.GLCanvas;
public class SwtPopupExample
{
public static void main( String[] args )
{
GLProfile glProfile = GLProfile.getMaxFixedFunc( true );
GLCapabilities glCapabilities = new GLCapabilities( glProfile );
final Display display = new Display( );
final Shell shell = new Shell( display );
shell.setLayout( new FillLayout( ) );
final GLCanvas canvas = new GLCanvas( shell, SWT.NO_BACKGROUND, glCapabilities, null );
shell.setSize( 800, 800 );
shell.setLocation( 0, 0 );
shell.open( );
shell.moveAbove( null );
final Menu menu = new Menu( shell, SWT.NONE );
MenuItem m1 = new MenuItem( menu, SWT.PUSH );
MenuItem m2 = new MenuItem( menu, SWT.PUSH );
MenuItem m3 = new MenuItem( menu, SWT.PUSH );
m1.setText( "Option 1" );
m2.setText( "Option 2" );
m3.setText( "Option 3" );
canvas.addMouseListener( new MouseAdapter( )
{
@Override
public void mouseDown( MouseEvent e )
{
System.out.println( "pre mousePressed " + e.x + " " + e.y + " Menu Visible " + menu.isVisible( ) );
// map canvas coordinate system to Display coordinate system to place menu in proper location
Point point = display.map( canvas, null, new Point( e.x, e.y ) );
menu.setLocation( point );
menu.setVisible( true );
System.out.println( "post mousePressed " + point.x + " " + point.y + " Menu Visible " + menu.isVisible( ) );
}
} );
while ( !shell.isDisposed( ) )
if ( !display.readAndDispatch( ) ) display.sleep( );
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment