Dynamic Menu and Alert Box
From Planet JFX
This example creates a form with a menubar. Items of the menu are added and deleted dynamically in runtime by "Add" and "Remove" buttons. If menu item is selected alert box with its name is shown.
import javafx.ui.*;
import java.lang.System;
//alert data model declaration
class AlertModel {
attribute AlertText:String;
attribute ShowAlert:boolean;
}
var am = AlertModel {
AlertText: "Text"
ShowAlert: false
};
//menu data model
class MenuSource {
attribute items:MenuItem*;
attribute malert:AlertModel;
operation addNewItem(mtext:String,mcommand:String);
operation menuCommand(item:String);
}
trigger on new MenuSource {
for (i in [0..3]) {
addNewItem("Item {i}","{i}");
}
}
operation MenuSource.menuCommand(item:String) {
System.out.println(item);
malert.AlertText="Item {item} clicked";
malert.ShowAlert=true;
}
operation MenuSource.addNewItem(mtext:String , mcommand:String ) {
var mi = MenuItem {
text: mtext
action: operation() {
menuCommand(mcommand);
}
};
insert mi as last into items;
}
var ms = new MenuSource;
ms.malert=am;
var menu = MenuBar {
menus: Menu {
text: "Test"
items: bind ms.items
}
};
var del_button = Button {
text: "delete last menu item"
action: operation() {
var n = sizeof ms.items - 1;
delete ms.items[indexof . == n];
}
};
//delete button
var add_button = Button {
text: "add menu item"
action: operation() {
var n = sizeof ms.items;
ms.addNewItem("Item {n}" , "{n}");
}
};
//add button
var alert = Frame {
visible: bind am.ShowAlert
content: BorderPanel {
top: Label { text: bind am.AlertText }
bottom: Button {
text: "Close"
action: operation() {
am.ShowAlert=false;
}
}
}
};
Frame {
menubar: menu
content: BorderPanel { top: del_button
bottom: add_button }
visible: true
}
