java - Can JavaFX 8 set a default menu bar? -
a trivial toy application screen menu bar can written in java fx 8:
import javafx.application.application; import javafx.scene.scene; import javafx.scene.control.menu; import javafx.scene.control.menubar; import javafx.scene.control.menuitem; import javafx.scene.input.keycombination; import javafx.scene.layout.vbox; import javafx.stage.stage; public class main extends application { public static void main(final string[] args) throws exception { launch(main.class, args); } @override public void start(stage stage) throws exception { menubar menubar = new menubar(); menu filemenu = new menu("file"); menuitem newnotebookmenuitem = new menuitem("new notebook..."); newnotebookmenuitem.setaccelerator(keycombination.keycombination("meta+n")); newnotebookmenuitem.setonaction(event -> { system.out.println("action fired"); }); filemenu.getitems().add(newnotebookmenuitem); menubar.getmenus().add(filemenu); menubar.setusesystemmenubar(true); vbox root = new vbox(); root.getchildren().add(menubar); scene scene = new scene(root, 400, 350); stage.setscene(scene); stage.show(); } }
this might ok making toy apps calculators there 1 window , application closes when last window closes, application document-based. when document-based application has no documents open, there no windows open menu should remain visible user can open new document.
the usual way setdefaultmenubar
in com.apple.eawt
apis, not great because:
- setting menu bar there hangs application (possibly kind of incompatibility between awt , javafx - doesn't happen same application running pure swing.)
- it requires me build javafx menubar real windows jmenubar default menu bar , javafx doesn't let me reuse action classes, awful adapters end being written bridge gap.
is there proper way set haven't found yet? expected find such method on application (some mailing list posts hinting might end there) looks it's still missing.
currently trick follows:
pane pane = new pane(); pane.maxwidth(1); pane.maxheight(1); pane.resize(1, 1); pane.getchildren().add(menubar); scene scene = new scene(pane,1,1); scene.setfill(null); primarystage.initstyle(stagestyle.transparent); primarystage.setwidth(1); primarystage.setheight(1); primarystage.setscene(scene); primarystage.show();
i don't use stage anything. should appear invisible well. annoying & ugly, works.
the other sad fact must set window have same menubar.... may or may not work you.
Comments
Post a Comment