1. ContextMenu Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import javafx.scene.control.ContextMenu; import javafx.scene.control.Menu; import javafx.scene.control.MenuItem; import javafx.scene.control.SeparatorMenuItem; public class FxContextMenu { ContextMenu rootMenu; Menu currMenu; int fontSize = 12; public FxContextMenu(int fontsize) { rootMenu = new ContextMenu(); currMenu = null; fontSize = fontsize; } public FxContextMenu() { this(12); } public ContextMenu getMenu() { return rootMenu; } public void addMenu(String text) { currMenu = new Menu(text); currMenu.setStyle("-fx-font-size: " + fontSize + " pt;"); rootMenu.getItems().add(currMenu); } public void addMenuItem(Menu menu, String text, Runnable runnable) { MenuItem item; if( text != null ) { item = new MenuItem(text); if( runnable != null ) { item.setOnAction(e -> runnable.run()); } } else { item = new SeparatorMenuItem(); } if( fontSize > 0 ) { item.setStyle("-fx-font-size: " + fontSize + " pt;"); } if( menu != null ) { menu.getItems().add(item); } else { rootMenu.getItems().add(item); } } public void addMenuItem(String text, Runnable runnable) { addMenuItem( currMenu, text, runnable ); } public void addRootMenuItem(String text, Runnable runnable) { currMenu = null; addMenuItem( currMenu, text, runnable ); } } |
댓글 없음:
댓글 쓰기