OnJava8-Examples/swt/Menus.java
2015-05-05 11:20:13 -07:00

49 lines
1.4 KiB
Java

//: swt/Menus.java
// Fun with menus.
import swt.util.*;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import java.util.*;
import net.mindview.util.*;
public class Menus implements SWTApplication {
private static Shell shell;
@Override
public void createContents(Composite parent) {
shell = parent.getShell();
Menu bar = new Menu(shell, SWT.BAR);
shell.setMenuBar(bar);
Set<String> words = new TreeSet<>(
new TextFile("Menus.java", "\\W+"));
Iterator<String> it = words.iterator();
while(it.next().matches("[0-9]+"))
; // Move past the numbers.
MenuItem[] mItem = new MenuItem[7];
for(int i = 0; i < mItem.length; i++) {
mItem[i] = new MenuItem(bar, SWT.CASCADE);
mItem[i].setText(it.next());
Menu submenu = new Menu(shell, SWT.DROP_DOWN);
mItem[i].setMenu(submenu);
}
int i = 0;
while(it.hasNext()) {
addItem(bar, it, mItem[i]);
i = (i + 1) % mItem.length;
}
}
static Listener listener = new Listener() {
public void handleEvent(Event e) {
System.out.println(e.toString());
}
};
void
addItem(Menu bar, Iterator<String> it, MenuItem mItem) {
MenuItem item = new MenuItem(mItem.getMenu(),SWT.PUSH);
item.addListener(SWT.Selection, listener);
item.setText(it.next());
}
public static void main(String[] args) {
SWTConsole.run(new Menus(), 600, 200);
}
} ///:~