I want to add a JavaFX Menu Bar to a Stage, but have it use the System Menubar for Mac.
My problem is that using:
menuBar.useSystemMenuBarProperty().set(true);
does not work. I believe that the problem is because my main method isn't part of a JavaFX Application. My main method looks like this:
public class SURPG_Launcher {
public static com.apple.eawt.Application macApp;
public static void main(String[] args) {
if(Toolbox.isMac()) {
initMac(args);
} else {
Application.launch(SURPG_Main.class, args);
}
}
private static void initMac(String[] args) {
System.out.println("MacOS System detected!");
macApp = com.apple.eawt.Application.getApplication();
macApp.setPreferencesHandler(new PreferencesHandler(){
#Override
public void handlePreferences(PreferencesEvent arg0) {
Platform.runLater(() -> {
Stage prefs = new Stage();
prefs.setMinHeight(200);
prefs.setMinWidth(200);
prefs.show();
});
}
});
Application.launch(SURPG_Mac.class, args);
}
}
SURPG_Mac.class and SURPG_Main.class are classes that extend the JavaFX Application.
I have another class that sets the GUI, a stage with a BorderPane. I have another class with public static methods that can be called to set the Menubars, as such:
public class MenuControl {
public static MenuBar menuBar;
public static Menu menuFile;
public static Menu menuGame;
public static Menu menuTools;
public static MenuItem save;
public static void initMenusMac() {
menuBar = new MenuBar();
Platform.runLater(() -> {
menuBar.useSystemMenuBarProperty().set(true);
});
menuFile = new Menu("File");
menuGame = new Menu("Game");
menuTools = new Menu("Tools");
save = new MenuItem("Save");
menuFile.getItems().add(save);
menuBar.getMenus().addAll(menuFile, menuGame, menuTools);
GUI_Main.totalLay.setTop(menuBar);
}
public static void initMenus() {
menuBar = new MenuBar();
menuFile = new Menu("File");
menuGame = new Menu("Game");
menuTools = new Menu("Tools");
save = new MenuItem("Save");
menuFile.getItems().add(save);
menuBar.getMenus().addAll(menuFile, menuGame, menuTools);
GUI_Main.totalLay.setTop(menuBar);
}
}
My final point is that I CANNOT change it so the main method is in either SURPG_Mac or SURPG_Main, due to a different compatibility problem with Mac integration.
Can anyone help me with this?
Thank you so much in advance!
Have a look at this probject: https://github.com/codecentric/NSMenuFX It allows you to have a more mac-like menu bar. But you will probably have to clean up your somewhat strange project setup as well before you can use it.
Related
Consider a non-fx existing application, let's call it Business.
Business exposes a Model object, which in turn exposes some properties. Model also accepts listeners to those properties.
My question is about adding JavaFx gui to such application. The GuiApp obviously extends javafx.application.Application and will need a reference to a Model object.
Searching for a solution for passing a non-String parameter to GuiApp I found several different approaches:
Static approach : for example have Business initialize a static reference to Model in GuiApp. One example of the use of statics can be seen here .
JavaFx 9 approach: as demonstrated here you can launch JavaFx application without extending Application.
Change workflow approach: change the existing workflow to have GuiApp initialize BusinessApp . One example of such workflow can be seen here.
Are there another viable approaches ? Best practice ?
I'll try to demonstrate some different approaches for passing a reference between a java program, and a java-fx program.
I post it in hope it will help some future readers having similar need. I also hope it may encourage other answers with additional solutions.
The posted code should not be considered proper implementation, but rather a short code aiming to clarify the different approaches. For this purpose I'll introduce a simple listening interface :
interface Observe{ void update(int i); }
A java class, that represents an exiting business application :
public class JavaApp {
private Observe observer; private int counter = 0;
JavaApp(Observe observer){ //not null safe
this.observer = observer;
}
void process() {
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
observer.update(counter >=100 ? 0 : ++counter);
}
}, 1000,1000);
}
}
A java-fx application that should be added to the existing business application, listen to it and serve as view:
public class JavaFxApp extends Application implements Observe{
private Label label;
#Override public void start(Stage stage) {
label = new Label("waiting");
BorderPane pane = new BorderPane(label);
Scene scene = new Scene(pane, 100, 100);
stage.setScene(scene);
stage.show();
}
#Override public void update(int i) {
Platform.runLater(()-> label.setText(String.valueOf(i)));
}
}
How do we share a reference, in this case a reference to Observe instance, between the two applications ?
Approach 1: Consider the start() method as the entry point to the application (see James_D answer)
This is simple and straight forward if you want to tie the existing java application with java-fx and use java-fx Application as the entry point:
public class JavaFxApp extends Application implements Observe{
private Label label;
#Override public void start(Stage stage) {
JavaApp main = new JavaApp(this);
label = new Label("waiting");
BorderPane pane = new BorderPane(label);
Scene scene = new Scene(pane, 100, 100);
stage.setScene(scene);
stage.show();
new Thread(()-> { main.process();}).start(); //launch the business process
}
#Override public void update(int i) {
Platform.runLater(()-> label.setText(String.valueOf(i)));
}
public static void main(String[] args) { launch(); }
}
Approach 2: Use JavaFX 9 Platform#startup
This is the best solution I found, when you can not use the Application#start method as the entry point to the application.
As demonstrated in fabians answer, as off java-fx 9 you can launch without extending Application. All you have to do is modify the main of the java application:
public class JavaApp {
private Observe observer; private int counter = 0;
JavaApp(Observe observer){//not null safe
this.observer = observer;
}
void process() {
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override public void run() {
observer.update(counter >=100 ? 0 : ++counter);
}
}, 1000,1000);
}
public static void main(String[] args) {
JavaFxApp view = new JavaFxApp(); //initialize JavaFx application
JavaApp main = new JavaApp(view);
Platform.startup(() -> {//launch JavaFx application
Stage stage = new Stage();
try {
view.start(stage);
} catch (Exception ex) {ex.printStackTrace();}
});
main.process(); //run business process
}
}
Approach 3: Use Static members
For example introduce a static getter in the java-fx application :
public class JavaFxApp extends Application {
private static Label label = new Label("waiting");
#Override public void start(Stage stage) {
BorderPane pane = new BorderPane(label);
Scene scene = new Scene(pane, 100, 100);
stage.setScene(scene);
stage.show();
}
static Observe getObserver() {
return JavaFxApp::update;
}
private static void update(int i) {
Platform.runLater(()-> label.setText(String.valueOf(i)));
}
}
and use it in the java application:
public class JavaApp {
private Observe observer; private int counter = 0;
JavaApp(Observe observer){//not null safe
this.observer = observer;
}
void process() {
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
observer.update(counter >=100 ? 0 : ++counter);
}
}, 1000,1000);
}
public static void main(String[] args){
new Thread(()-> Application.launch(JavaFxApp.class)).start();
Observe observer = JavaFxApp.getObserver(); //get static observer reference
JavaApp main = new JavaApp(observer);
main.process();
}
}
A better approach to get a static reference might be (based on this answer) :
public class JavaFxApp extends Application implements Observe{
private static final CountDownLatch latch = new CountDownLatch(1);
private static Observe observer = null;
private Label label;
#Override public void init() {
observer = this;
latch.countDown();
}
#Override public void start(Stage stage){
label = new Label("waiting");
BorderPane pane = new BorderPane(label);
Scene scene = new Scene(pane, 100, 100);
stage.setScene(scene);
stage.show();
}
#Override public void update(int i) {
Platform.runLater(()-> label.setText(String.valueOf(i)));
}
static Observe getObserver() {
try {
latch.await();
} catch (InterruptedException e) { e.printStackTrace(); }
return observer;
}
}
how can I refactor the following code so that only the code in deleteButton.setOnAction(deleteEvent -> {//only this code varies} changes. Everything else will stay the same but the block of code in the lambda expression varies from time to time when I call the class from another class. The block of code that goes through the lambda expression is supposed to be a void method.
public class A {
public void test() {
// ensure that user can't close the alert
Stage primaryStage = (Stage) RootLayoutController.getRootLayout().getScene().getWindow();
JFXAlert<javafx.scene.control.ButtonType> alert = new JFXAlert<>(primaryStage);
alert.initModality(Modality.APPLICATION_MODAL);
alert.setOverlayClose(false);
//create font awesome icon
String ICON = "\uf071";
Label labelIcon = new Label(ICON);
labelIcon.setStyle("-fx-font-family: 'FontAwesome'; -fx-font-size: 60px; -fx-text-fill: #D34336;");
labelIcon.setPadding(new Insets(0,5,0,0));
// Create the content of the JFXAlert with JFXDialogLayout
JFXDialogLayout layout = new JFXDialogLayout();
Label labelHeading = new Label("Alert Notification");
Label labelBody = new Label("Are you sure you want to delete this?");
layout.setHeading(labelHeading);
layout.setBody(new VBox(new HBox(labelIcon, labelBody)));
// Buttons get added into the actions section of the layout.
JFXButton deleteButton = new JFXButton("Delete");
deleteButton.setDefaultButton(true);
deleteButton.setOnAction(deleteEvent -> {
//only this block of code changes
alert.hideWithAnimation();
});
JFXButton cancelButton = new JFXButton("Cancel");
cancelButton.setCancelButton(true);
cancelButton.setOnAction(closeEvent -> alert.hideWithAnimation());
layout.setActions(deleteButton, cancelButton);
alert.setContent(layout);
alert.showAndWait();
}
}
It is not entirely clear from your question what you are trying to accomplish, but I will take a wild stab at it.
If you are looking to be able to pass a code block to the deleteButton.setOnAction() method, you could use an Interface and pass implementations of that interface to the A class. Then just pass that reference to an internal method for the onAction lambda.
Here is a very quick example of how you could do something like this:
Main.java:
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
// Action button
Button btnDoSomething = new Button("Do something...");
btnDoSomething.setOnAction(e -> doTheThings(new ImplDoSomething()));
Button btnDoSomethingElse = new Button("Do something else...");
btnDoSomethingElse.setOnAction(e -> doTheThings(new ImplDoSomethingElse()));
VBox mainPane = new VBox(5);
mainPane.setAlignment(Pos.CENTER);
mainPane.setPadding(new Insets(10));
mainPane.getChildren().addAll(btnDoSomething, btnDoSomethingElse);
primaryStage.setScene(new Scene(mainPane));
primaryStage.show();
}
private void doTheThings(IParameterMethod parameterMethod) {
parameterMethod.call();
}
}
The IParameterMethod.java Interface:
public interface IParameterMethod {
void call();
}
Then you can create as many classes as you like that implement that interface, each with their own call() method, allowing you to execute different code.
ImplDoSomething.java
public class ImplDoSomething implements IParameterMethod {
#Override
public void call() {
System.out.println("Doing something!");
}
}
ImplDoSomethingElse.java:
public class ImplDoSomethingElse implements IParameterMethod {
#Override
public void call() {
System.out.println("Doing something else!");
}
}
This should be easily adapted for your project.
I have 2 Java files, the first one is the main java code behind the program. And second is a jfx.Webview. I've been trying for forever to include the jfx.Webview in a JPanel that I have on the first Java file. I'm new to Java, and it's definitely not as easy as i thought. Please, if someone with a better understanding could explain to me the proper way to get this done, that would be of great help.
Here are the 2 Java files after some cleaning up:
public class Xzibit07 {
public static void main(String[] args) {
generateUI();
}
private static void generateUI(){
XzibitUI program = new XzibitUI();
program.setVisible(true);
program.setTitle("Xzibit");
ImageIcon logoIcon = new ImageIcon(new ImageIcon("Data/Images/Logo.jpg").getImage().getScaledInstance(program.logoLabel.getWidth(), program.logoLabel.getHeight(), Image.SCALE_DEFAULT));
program.logoLabel.setIcon(logoIcon);
program.logoLabel.setBounds((program.logoPanel.getWidth()/2 - program.logoLabel.getWidth()/2), 0, 0, 0);
ImageIcon settingsIcon = new ImageIcon(new ImageIcon("Data/Images/Settings.png").getImage().getScaledInstance((program.logoPanel.getHeight()/4), (program.logoPanel.getHeight()/4), Image.SCALE_DEFAULT));
program.settingsLabel.setIcon(settingsIcon);
program.getContentPane().setBackground(Color.WHITE);
program.logoPanel.setBackground(Color.WHITE);
}
}
And the second:
public class XzibitWeb implements Runnable {
public String webPage = "http://www.example.com";
public static void main(String[] args) {
SwingUtilities.invokeLater(new XzibitWeb());
}
#Override
public void run() {
JFXPanel jfxPanel = new JFXPanel();
Platform.runLater(() -> {
WebView view = new WebView();
jfxPanel.setScene(new Scene(view, 1024, 400));
view.getEngine().load(webPage);
});
}
}
I think you are trying to do this
https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/swing-fx-interoperability.htm
From your code i think you are not adding the jfxPanel to the frame.
I am crazy about the feature of JavaFX, in Swing, I could do,
#Override
public void onPluginRegistered(final GamePlugin plugin) {
JRadioButtonMenuItem gameMenuItem = new JRadioButtonMenuItem(plugin.getGameName());
gameMenuItem.setSelected(false);
gameMenuItem.addActionListener(event -> {
if (core.getPlayers().isEmpty()) {
// Can't start a game with no players.
showErrorDialog(frame, ERROR_NO_PLAYERS_TITLE, ERROR_NO_PLAYERS_MSG);
gameGroup.clearSelection();
} else {
core.startNewGame(plugin);
}
});
gameGroup.add(gameMenuItem);
newGameMenu.add(gameMenuItem);
}
if I want to add a radio item whenever a plugin has registered.
However in JavaFX, it seems, you can't declare any global item of JavaFX, because once the start() is called, it starts a new constructor and everything you've done before is nothing (there is no variable share to me).
Here is my Javafx code.
#Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
Scene scene = new Scene(root, 500, 500);
scene.getStylesheets().add("./Buttons.css");
Region spacer = new Region();
spacer.setMinWidth(10);
primaryStage.setScene(scene);
primaryStage.show();
TabPane tabPane = new TabPane();
Tab tabData = new Tab("Get your data");
tabPane.getTabs().add(tabData);
Tab tabDisplay = new Tab("Visualize your data");
tabPane.getTabs().add(tabDisplay);
pluginGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
#Override
public void changed(ObservableValue<? extends Toggle> ov,
Toggle old_toggle, Toggle new_toggle) {
if (pluginGroup.getSelectedToggle() != null) {
RadioButton chk = (RadioButton) new_toggle.getToggleGroup().getSelectedToggle();
chk.getText();
}
}
});
root.setCenter(tabPane);
FlowPane inputPanel = new FlowPane();
TextField source = new TextField ();
Button confirmButton = new Button("Get Your Resource!");
confirmButton.getStyleClass().add("GREEN");
inputPanel.getChildren().addAll(new Label("Input your source:"),
spacer, source, confirmButton);
root.setBottom(inputPanel);
RadioButton defaultBtn = new RadioButton("No data plugin are registered");
FlowPane pane = new FlowPane();
pane.getChildren().addAll(new Label("Select your data source"), spacer);
if (radioButtonBox != null) {
pane.getChildren().add(radioButtonBox);
}
tabData.setContent(pane);
}
#Override
public void onPluginRegistered(DataPlugin plugin) {
RadioButton button = new RadioButton(plugin.getName());
button.setToggleGroup(pluginGroup);
radioButtonBox.getChildren().add(button);
}
public void caller(String[] args) {
launch(args);
}
I want to initialize the javafx program from,
public static void main(String[] args) throws Exception {
DataFramework core = new ConcreteDataFramework();
GuiFramework gui = new GuiFramework(core);
core.addGuiListener(gui);
gui.caller(args);
core.registerPlugin(new CsvData());
}
It is weird that I can't add any radio button to the existing radioButtonBox every time I call onPluginRegistered(DataPlugin plugin) (The new radiobutton does not show up)
You should consider the start() method as the replacement for the main method. If your application needs access to some kind of service or model, create it in the start() (or init()) method. I would actually recommend making the Application subclass (which is inherently not reusable) as minimal as possible - it should just do the startup work - and factoring the remaining GUI code into a separate class. (If you use FXML, the FXML file can define the UI, and the Application subclass is then already pretty minimal: it just loads and displays the FXML.)
You haven't really provided enough context to make it clear what's going on here, but I'm guessing GuiFramework is the Application subclass you've shown part of, and DataFramework is an interface of some kind. I also assume GuiFramework is implementing some interface that defines the onPluginRegistered method.
So I would do:
public class GuiFramework implements PluginAware {
private final BorderPane root ;
private final DataFramework dataFramework ;
public GuiFramework(DataFramework dataFramework) {
this.dataframework = dataFramework ;
this.root = new BorderPane();
TabPane tabPane = new TabPane();
Tab tabData = new Tab("Get your data");
tabPane.getTabs().add(tabData);
// etc etc (remaining code from your start() method)
}
public Parent getView() {
return root ;
}
#Override
public void onPluginRegistered(DataPlugin plugin) {
RadioButton button = new RadioButton(plugin.getName());
button.setToggleGroup(pluginGroup);
radioButtonBox.getChildren().add(button);
}
}
and define a Main class for starting the application:
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
DataFramework core = new ConcreteDataFramework();
GuiFramework gui = new GuiFramework(core);
core.addGuiListener(gui);
Scene scene = new Scene(gui.getView(), 500, 500);
scene.getStylesheets().add("./Buttons.css");
primaryStage.setScene(scene);
primaryStage.show();
core.registerPlugin(new CsvData());
}
// for environments not supporting JavaFX launch automatically:
public static void main(String[] args) {
launch(args);
}
}
I have a menubar in my vaadin application and want to add an item to open a pdf-file is a new tab of the browser. I found some solutions to open files with a button, but I have to use an MenuItem...
MenuBar.Command commandHandler = new MenuBar.Command() {
#Override
public void menuSelected(MenuItem selectedItem) {
if (selectedItem.equals(menu_help)) {
openHelp();
}
}
};
...
menu_help = menuBar
.addItem("", WebImageList.getImage(ImageList.gc_helpIcon),
commandHandler);
...
private void openHelp() {
// open pdf-file in new window
}
Thanks for help!
SOLUTION:
private void openHelp() {
final String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
Resource pdf = new FileResource(new File(basepath + "/WEB-INF/datafiles/help.pdf"));
setResource("help", pdf);
ResourceReference rr = ResourceReference.create(pdf, this, "help");
Page.getCurrent().open(rr.getURL(), "blank_");
}
Attention: This code works, but the the structure of code is not perfect ;-) Better is to store "basepath" and "pdf" as attribute...
There is a similar problem described here: How to specify a button to open an URL?
One possible solution:
public class MyMenuBar extends MenuBar {
ResourceReference rr;
public MyMenuBar() {
Resource pdf = new FileResource(new File("C:/temp/temp.pdf"));
setResource("help", pdf);
rr = ResourceReference.create(pdf, this, "help");
}
private void openHelp() {
Page.getCurrent().open(rr.getURL(), "blank_");
}
...
}
The setResource method of AbstractClientConnector is protected, so this is you need to extend some Vaadin component to make it work. This is why Im creating the class MyMenuBar here. If you are using an external resource you don't need to attach it to any component with setResource and then this is not nessecary.
I used the following code to do something similar:
private Component buildUserMenu() {
final MenuBar settings = new MenuBar();
settings.addStyleName("user-menu");
final User user = getCurrentUser();
settingsItem = settings.addItem("", new ThemeResource(
"img/logo.png"), null);
updateUserName(null);
settingsItem.addItem(Lang.getMessage("menu.edit"), new Command() {
#Override
public void menuSelected(final MenuItem selectedItem) {
ProfilePreferencesWindow.open(user, false);
}
});
settingsItem.addSeparator();
settingsItem.addItem(Lang.getMessage("menu.help"), new Command() {
#Override
public void menuSelected(final MenuItem selectedItem) {
Window help = new Window();
help.setWidth("90%");
help.setHeight("90%");
BrowserFrame e = new BrowserFrame("PDF File", new ThemeResource("pdf/ayuda.pdf"));
e.setWidth("100%");
e.setHeight("100%");
help.setContent(e);
help.center();
help.setModal(true);
UI.getCurrent().addWindow(help);
}
});
settingsItem.addSeparator();
settingsItem.addItem(Lang.getMessage("menu.logout"), new Command() {
#Override
public void menuSelected(final MenuItem selectedItem) {
BarsEventBus.post(new UserLoggedOutEvent());
}
});
return settings;
}