Reputation: 321
How can I add Menu to label? I want to show a drop down menu by clicking on label to show some options to user. How is it possible in SWT?
Upvotes: 0
Views: 857
Reputation: 36904
You can add a Menu
to all Control
s by calling Control#setMenu(Menu)
. Here is a small example for a Label
:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
Label label = new Label(shell, SWT.NONE);
label.setText("Right-click here");
Menu menu = new Menu(label);
MenuItem item = new MenuItem(menu, SWT.PUSH);
item.setText("Context-menu item");
item.addListener(SWT.Selection, new Listener()
{
@Override
public void handleEvent(Event event)
{
System.out.println("clicked");
}
});
label.setMenu(menu);
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
Upvotes: 2