Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » MenuBar(Menu Bar in Shell)
MenuBar [message #536012] Wed, 26 May 2010 16:04 Go to next message
Thomas Kratzke is currently offline Thomas KratzkeFriend
Messages: 5
Registered: May 2010
Junior Member
I have a menu bar and can't work around 2 problems:
1. shell.pack() seems to ignore the width of the menu bar. Is that correct and is there a way around that?
2. If I accidentally double-click on one of the items in the menu bar, the program hangs. Is there a way to get a double-click ignored?

Thanks
Re: MenuBar [message #536048 is a reply to message #536012] Wed, 26 May 2010 18:42 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Ludger.Heisterkamp.technotrans.de

> I have a menu bar and can't work around 2 problems:
> 1. shell.pack() seems to ignore the width of the menu bar. Is that
> correct and is there a way around that?

I don't Know a way around this. In my test the menu bar is wrapped into
several lines if there isn't enough space in the underlying shell. I can
imagine this is an OS specific behaviour...

> 2. If I accidentally double-click on one of the items in the menu bar,
> the program hangs. Is there a way to get a double-click ignored?

I wrote a test program and I cannot reproduce your problem. Can you post a
simplified example of your code?

Here is my test code, can you reproduce the problem with it?

public static void main(String[] args) {
Shell shell = new Shell(Display.getDefault());

SelectionListener l = new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent arg0) {
}
public void widgetSelected(SelectionEvent arg0) {
System.out.println("Selected: " + arg0.getSource());
}
};

Menu menu = new Menu(shell,SWT.BAR);
for(int i=0;i<10;i++) {
MenuItem item = new MenuItem(menu, SWT.CASCADE);
item.setText("Item"+i);
item.addSelectionListener(l);
if(i%2==0) {
Menu subMenu = new Menu(shell, SWT.DROP_DOWN);
item.setMenu(subMenu);
MenuItem subItem = new MenuItem(subMenu, SWT.CASCADE);
subItem.setText("subItem");
subItem.addSelectionListener(l);
}
}

shell.setMenuBar(menu);

shell.pack();
shell.open();
while(!shell.isDisposed()) {
if(!Display.getDefault().readAndDispatch())
Display.getDefault().sleep();
}
}
Re: MenuBar [message #536068 is a reply to message #536048] Wed, 26 May 2010 20:48 Go to previous messageGo to next message
Thomas Kratzke is currently offline Thomas KratzkeFriend
Messages: 5
Registered: May 2010
Junior Member
I think the problem I'm having is that I'm dynamically creating the menu using menuShown, and there isn't enough time to do it between the double-clicks. In the main below, the top level item Top2 dynamically creates 500 items. If you run the code, you'll see bizarre behavior when you double-click Top2.


import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class DoubleClick {
public static void main(final String[] args) {
final Shell shell = new Shell(Display.getDefault());
final SelectionListener topSl = new SelectionListener() {
public void widgetDefaultSelected(final SelectionEvent arg0) {
}

public void widgetSelected(final SelectionEvent arg0) {
System.out.println("Selected: Top " + arg0.getSource());
}
};
final SelectionListener lowSl = new SelectionListener() {
public void widgetDefaultSelected(final SelectionEvent arg0) {
}

public void widgetSelected(final SelectionEvent arg0) {
System.out.println("Selected: Sub " + arg0.getSource());
}
};
final Menu menuBar = new Menu(shell, SWT.BAR);
for (int i = 0; i < 3; i++) {
final int finalI = i;
final MenuItem topItem = new MenuItem(menuBar, SWT.CASCADE);
topItem.setText("Top" + i);
topItem.addSelectionListener(topSl);
if (i == 0) {
} else if (i == 1) {
final Menu topDropDown = new Menu(shell, SWT.DROP_DOWN);
topItem.setMenu(topDropDown);
final MenuItem subItem = new MenuItem(topDropDown, SWT.CASCADE);
subItem.setText("subItem0");
subItem.addSelectionListener(lowSl);
} else if (i == 2) {
final Menu topDropDown = new Menu(shell, SWT.DROP_DOWN);
topItem.setMenu(topDropDown);
topDropDown.addMenuListener(new MenuAdapter() {
@Override
public void menuShown(final MenuEvent e) {
/** Get rid of existing menu items */
final MenuItem[] items = topDropDown.getItems();
for (final MenuItem item : items) {
item.dispose();
}
/** Create the new ones. */
for (int k = 0; k < 500; ++k) {
final int finalK = k;
final MenuItem subItem =
new MenuItem(topDropDown, SWT.CASCADE);
subItem.setText("subItem" + finalI + ": " + finalK);
subItem.addSelectionListener(lowSl);
}
}
});
}
}
shell.setMenuBar(menuBar);
shell.setSize(new Point(300, 60));
// shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!Display.getDefault().readAndDispatch())
Display.getDefault().sleep();
}
}

public static void ludgarMain(final String[] args) {
final Shell shell = new Shell(Display.getDefault());
final SelectionListener l = new SelectionListener() {
public void widgetDefaultSelected(final SelectionEvent arg0) {
}

public void widgetSelected(final SelectionEvent arg0) {
System.out.println("Selected: " + arg0.getSource());
}
};
final Menu menu = new Menu(shell, SWT.BAR);
for (int i = 0; i < 3; i++) {
final MenuItem topItem = new MenuItem(menu, SWT.CASCADE);
topItem.setText("Item" + i);
topItem.addSelectionListener(l);
if (i % 2 == 0) {
final Menu topDropDown = new Menu(shell, SWT.DROP_DOWN);
topItem.setMenu(topDropDown);
final MenuItem subItem = new MenuItem(topDropDown, SWT.CASCADE);
subItem.setText("subItem");
subItem.addSelectionListener(l);
}
}
shell.setMenuBar(menu);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!Display.getDefault().readAndDispatch())
Display.getDefault().sleep();
}
}
}
Re: Re: MenuBar [message #536100 is a reply to message #536068] Thu, 27 May 2010 04:37 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Ludger.Heisterkamp.technotrans.de

I tried your code and i did not have any problems.
It takes a few seconds to build the submenu, but a doubleclick does not
cause any unexpected behaviour.
Which platform and SWT version are you using? I tried on WinXP and SWT
3.349

Regards,
Ludger

>>> Thomas Kratzke<kratzke@metsci.com> schrieb am Mittwoch, 26. Mai 2010 um
22:48 in Nachricht <htk1fp$a7i$1@build.eclipse.org>:
> I think the problem I'm having is that I'm dynamically creating the menu
> using menuShown, and there isn't enough time to do it between the
> double-clicks. In the main below, the top level item Top2 dynamically
> creates 500 items. If you run the code, you'll see bizarre behavior when

> you double-click Top2.
Re: Re: MenuBar [message #536265 is a reply to message #536100] Thu, 27 May 2010 13:59 Go to previous messageGo to next message
Thomas Kratzke is currently offline Thomas KratzkeFriend
Messages: 5
Registered: May 2010
Junior Member
I'm running on Windows XP. My machine is 2.79GHz. Perhaps you should change the 500 to 5000 in the code.

The bizarre behavior that I'm seeing is:
The submenu items have a different listener than the top menu items; both are system.out's, but they print different things. When I double-click the "heavy menu," the submenu's listener gets called (after a pause).

I appreciate your looking at this. Thanks

Antw: Re: Re: MenuBar [message #536406 is a reply to message #536265] Fri, 28 May 2010 04:52 Go to previous message
Eclipse UserFriend
Originally posted by: Ludger.Heisterkamp.gds.eu

Hello Thomas,

the behavoir you are describing now is not bizarre to me:
The first click starts generating the menu, the second click event is
processed when generating has finished and clicks on the generated new menu.

This is not only related to a doubleclick, even two single clicks during the
menu generating time cause the same behavior.

In your first post, you were writing "If I accidentally double-click on one
of the items in the menu bar, the program hangs." This sounds like a strange
behaviour, but is not reproducible for me.

The only way around your doubleclick problem would be, that the submenu
generating process has been finished before you click on the main menu item,
e.g. in a background process. This depends on your application logic.

Best regards,
Ludger Heisterkamp


> I'm running on Windows XP. My machine is 2.79GHz. Perhaps you should
> change the 500 to 5000 in the code.
>
> The bizarre behavior that I'm seeing is:
> The submenu items have a different listener than the top menu items;
> both are system.out's, but they print different things. When I
> double-click the "heavy menu," the submenu's listener gets called (after
> a pause).
>
> I appreciate your looking at this. Thanks
Previous Topic:Double Click
Next Topic:Why having a single thread for display is so important?
Goto Forum:
  


Current Time: Thu Apr 25 01:34:32 GMT 2024

Powered by FUDForum. Page generated in 0.03659 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top