简体   繁体   中英

How can we add Menu to Label in swt

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?

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM