I have a vaadin application that redirect after login to a view with header / left menu and a main panel.
how can I set the menu or any link to switch the main panel according to a specific contents
If I click contact It set ContactLayout in the main panel.
PS: I know how to set a menu like in vaadin documentation but I want to know what to set as command for the menu item.
thanks
I suggest you to keep a Map<MenuItem,AbstractLayout> and when a MenuItem is clicked, remove all the components of your Panel, and add the layout get from the Map.
Visually :
public class TestApplication extends Application {
private VerticalLayout contactLayout;
private Panel mainPanel;
Map<MenuItem, AbstractLayout> swapContentMap;
#Override
public void init() {
Window mainWindow = new Window("Test Application");
mainPanel = new Panel("Main Panel");
mainWindow.addComponent(mainPanel);
// Create all of your layout
// For now, I just create a fake contact layout
contactLayout = new VerticalLayout();
// Here add your default layout to the right panel
mainPanel.addComponent(contactLayout);
Command myCommand = new MyCommand();
MenuBar menuBar = new MenuBar();
MenuItem menuItem = menuBar.addItem("Contact", myCommand);
//add your other menu item
swapContentMap = new HashMap<MenuBar.MenuItem, AbstractLayout>();
swapContentMap.put(menuItem, contactLayout);
//add your other menu item to the map.
setMainWindow(mainWindow);
}
private class MyCommand implements Command
{
public void menuSelected(MenuItem selectedItem)
{
TestApplication.this.mainPanel.removeAllComponents();
TestApplication.this.mainPanel.addComponent(swapContentMap.get(selectedItem));
}
}
}
Hope it will work.
Regards
Éric
Related
I want to add the popup to an existing functionality of backoffice. When a user click on the icon a popup will be populated with a text box and submit button.
I have tried many things but still can't find any proper solution. Help me in that to resolve the issue.
Create a new class that extends org.zkoss.zul.Window Class :
public class CustomWindow extends Window {
}
Then create a render method and add your components :
public class CustomWindow extends Window {
public void render(WidgetInstanceManager wim) {
initComponent(wim);
final Vlayout container = new Vlayout();
final Labeltext =new Label("text");
final Button button = new Button("button");
container.appendChild(button);
container.appendChild(text);
this.appendChild(container);
setClosable(true);
}
}
Then you can open your custom window with :
CustomWindow customWindow = new CustomWindow ();
customWindow.render(getWidgetInstanceManager());
customWindow.setParent("add parent component here");
customWindow.doModal();
Adapt the code to correspond more to your needs.
Hope this helps.
I'm unable to make a JavaFX MenuBar show as a standard OS X menu bar, at the top of the screen.
Here's what I've tried in my subclass of Application:
public void start(Stage primaryStage) throws Exception {
final Menu menu1 = new Menu("File");
final Menu menu2 = new Menu("Options");
final Menu menu3 = new Menu("Help");
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(menu1, menu2, menu3);
menuBar.setUseSystemMenuBar(true);
primaryStage.setTitle("Creating Menus with JavaFX 2.0");
final Group rootGroup = new Group();
final Scene scene = new Scene(rootGroup, 800, 400, Color.WHEAT);
rootGroup.getChildren().add(menuBar);
primaryStage.setScene(scene);
primaryStage.show();
}
I assumed that the use of
menuBar.setUseSystemMenuBar(true);
would do the trick, but actually it makes the menuBar disappear altogether.
I'm using Java 1.8.0-b132 on OS X 10.9
I've had success with this code:
MenuBar menuBar = new MenuBar();
final String os = System.getProperty("os.name");
if (os != null && os.startsWith("Mac"))
menuBar.useSystemMenuBarProperty().set(true);
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
primaryStage.setScene(new Scene(borderPane));
It looks like OS X only displays the Menus if they have MenuItems inside them (which is a bit weird, as you can attach functionality to empty Menus).
I created a little project that gives you access to the auto-generated menu bar on OS X: NSMenuFX
Update: With the new pure JavaFX version, the API has slightly changed
It allows you to replace the default Mac OS menu bar items, so you can to something like this:
// Get the toolkit
MenuToolkit tk = MenuToolkit.toolkit();
// Create default application menu with app name "test"
Menu defaultApplicationMenu = tk.createDefaultApplicationMenu("test");
// Replace the autogenerated application menu
tk.setApplicationMenu(defaultApplicationMenu);
// Since we now have a reference to the menu, we can rename items
defaultApplicationMenu.getItems().get(1).setText("Hide all the otters");
You can of course also add new menu items as you do in your example above.
I just ran into this issue myself - I noticed that the system menubar wouldn't initially appear in OSX until I switched to another application and back.
Wrapping the setUseSystemMenuBar call in a runLater did the trick, so I unscientifically concluded there's more window setup required before OSX can successfully register an application menu.
Platform.runLater(() -> menuBar.setUseSystemMenuBar(true));
Credits to this tutorial that I have followed with success:
https://blog.codecentric.de/en/2015/04/tweaking-the-menu-bar-of-javafx-8-applications-on-os-x/
Below I paste the most important part to get an OS X menu bar compatible with Win classic menu bar:
#Override
public void start(Stage primaryStage) throws Exception {
MenuBar menuBar = new MenuBar();
menuBar.useSystemMenuBarProperty().set(true);
Menu menu = new Menu("java");
MenuItem item = new MenuItem("Test");
menu.getItems().add(item);
menuBar.getMenus().add(menu);
primaryStage.setScene(new Scene(new Pane(menuBar)));
primaryStage.show();
}
Building on dmolony with some corrections:
MenuBar menuBar = new MenuBar ();
if( System.getProperty("os.name","UNKNOWN").equals("Mac OS X")) {
menuBar.setUseSystemMenuBar(true);
}
BorderPane borderPane = new BorderPane ();
borderPane.setTop (menuBar);
primaryStage.setScene (new Scene (borderPane));
I am creating an iPad app using MonoTouch 2.10.11 and I want to MonoTouch.Dialog to create some of the editable fields on a form. One of the fields will use a RadioGroup to allow the user to select from a list of options. The default behavior of M.T.D is display the selection list table over the existing table. This works great for the iPhone layout, but on this iPad form, the table is only on a small area of the form and the navigation bar looks odd on the middle of the form. I want to display the selection as a full screen modal and the user will hit a "back" button to go back the previous form with the selected item.
I created a new RootElement descendant class like this:
public class ModalRootElement : RootElement
{
public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
{
tableView.DeselectRow (path, false);
UIViewController uIViewController = this.MakeViewController ();
this.PrepareDialogViewController (uIViewController);
dvc.PresentViewController (uIViewController, true, null);
}
protected override void PrepareDialogViewController(UIViewController dvc)
{
base.PrepareDialogViewController(dvc);
UIButton button = UIButton.FromType (UIButtonType.RoundedRect);
button.Frame = new RectangleF (5, 5, 80, 20);
button.SetTitle ("back", UIControlState.Normal);
button.TouchUpInside += delegate {
DialogViewController d = dvc as DialogViewController;
(d.Root as ModalRootElement).TableView.ReloadData ();
d.DeactivateController(true);
};
dvc.View.AddSubview (button);
}
}
The table is implemented with the following code:
var _status = new ModalRootElement("Status", new RadioGroup("status", -1)) {
(new Section() {
new RadioElement("New", "status"),
new RadioElement("In process", "status"),
new RadioElement("Rejected", "status"),
new RadioElement("Deferred", "status"),
new RadioElement("Transferred", "status"),
new RadioElement("Unknown", "status"),
new RadioElement("Complete", "status")
})
};
var _odom = new EntryElement ("Odometer", "current odom", "");
_odom.KeyboardType = UIKeyboardType.DecimalPad;
_odom.TextAlignment = UITextAlignment.Right;
var root = new RootElement ("back") {
new Section("") {
_status,
_odom
}
};
_dvc = new DialogViewController(root);
_nav = new UINavigationController (_dvc);
_nav.SetNavigationBarHidden (true, false);
When I run the app, I can drill into the RadioGroup and make a selection. When I click the back button that I added to the view, the modal view closes and the RadioSelected properted of the ModalRootElement object is set correctly, but the text is not displayed.
If I change Selected() method to call dvc.ActivateController instead of PresentViewController, the ModalRootElement displays the correct text, but the RadioGroup table has the wrong size. Is there a way to get the RootElement to display the correct text when you use PresentViewController instead of ActivateController?
I think you need a Root.Reload() call.
I'm making a MenuBar, and I wan't the functionality to press a Menu like: "File" and then execute a action. Such like opening an other fxml, or an example where some output is written.
I want the functionality of a MenuItem (lie "About") in a Menu like "File".
package model;
import static java.lang.System.out;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.geometry.Side;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/**
* Example of creating menus in JavaFX.
*
* #author Dustin
*/
public class JavaFxMenus extends Application
{
/**
* Build menu bar with included menus for this demonstration.
*
* #param menuWidthProperty Width to be bound to menu bar width.
* #return Menu Bar with menus included.
*/
private MenuBar buildMenuBarWithMenus(final ReadOnlyDoubleProperty menuWidthProperty)
{
final MenuBar menuBar = new MenuBar();
// Prepare left-most 'File' drop-down menu
final Menu fileMenu = new Menu("File");
menuBar.getMenus().add(fileMenu);
//menuBar.getOnMouseClicked().handle(this);
// Prepare 'Examples' drop-down menu
final Menu examplesMenu = new Menu("JavaFX 2.0 Examples");
examplesMenu.getItems().add(new MenuItem("Text Example"));
examplesMenu.getItems().add(new MenuItem("Objects Example"));
examplesMenu.getItems().add(new MenuItem("Animation Example"));
menuBar.getMenus().add(examplesMenu);
// Prepare 'Help' drop-down menu
final Menu helpMenu = new Menu("Help");
helpMenu.setOnAction(null);
final MenuItem searchMenuItem = new MenuItem("Search");
searchMenuItem.setDisable(true);
helpMenu.getItems().add(searchMenuItem);
final MenuItem onlineManualMenuItem = new MenuItem("Online Manual");
onlineManualMenuItem.setVisible(false);
helpMenu.getItems().add(onlineManualMenuItem);
helpMenu.getItems().add(new SeparatorMenuItem());
final MenuItem aboutMenuItem =
MenuItemBuilder.create()
.text("About")
.onAction(
new EventHandler<ActionEvent>()
{
#Override public void handle(ActionEvent e)
{
out.println("You clicked on About!");
}
})
.accelerator(
new KeyCodeCombination(
KeyCode.A, KeyCombination.CONTROL_DOWN))
.build();
helpMenu.getItems().add(aboutMenuItem);
menuBar.getMenus().add(helpMenu);
// bind width of menu bar to width of associated stage
menuBar.prefWidthProperty().bind(menuWidthProperty);
return menuBar;
}
/**
* Start of JavaFX application demonstrating menu support.
*
* #param stage Primary stage.
*/
#Override
public void start(final Stage stage)
{
stage.setTitle("Creating Menus with JavaFX 2.0");
final Group rootGroup = new Group();
final Scene scene = new Scene(rootGroup, 800, 400, Color.WHEAT);
final MenuBar menuBar = buildMenuBarWithMenus(stage.widthProperty());
rootGroup.getChildren().add(menuBar);
stage.setScene(scene);
stage.show();
}
/**
* Main executable function for running examples.
*
* #param arguments Command-line arguments: none expected.
*/
public static void main(final String[] arguments)
{
Application.launch(arguments);
}
}
AFAIK, A Menu, if has not any added submenu or Menuitems, does not fire events neither on click, on shown nor on hide. However the workaround is to set its graphic where this graphic node will handle mouse clicks for example,
Label menuLabel = new Label("File");
menuLabel.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
Stage myDialog = new Stage();
myDialog.initModality(Modality.WINDOW_MODAL);
Scene myDialogScene = new Scene(VBoxBuilder.create()
.children(new Text("Hello! it's My Dialog."))
.alignment(Pos.CENTER)
.padding(new Insets(10))
.build());
myDialog.setScene(myDialogScene);
myDialog.show();
}
});
Menu fileMenuButton = new Menu();
fileMenuButton.setGraphic(menuLabel);
menuBar.getMenus().add(fileMenuButton);
A drawback of this approach is that the label do not cover all spaces of the menu resulting clicking on edges of menu is not triggering the mouse event. See this by uncommenting menuLabel.setStyle line above. But this can be resolved by playing with CSS styles I think.
Code is partially taken from Create Dialog using Stage. You can also load an FXML file into the myDialog stage using the FXMLLoader. There are lots of examples about it on the net.
Recently i had the same problem, this is what i did
#FXML private Menu myMenu;
#Override
public void initialize(URL url, ResourceBundle rb) {
myMenu.setGraphic(
ButtonBuilder.create()
.text("btnText")
.onAction(new EventHandler<ActionEvent>(){
#Override public void handle(ActionEvent t) {
//TODO
} })
.build()
);
}
Combining with the answer from our friend #Dota2, i built a helper class to trigger the Menu's onAction(Menu menu) event even if it does not have any MenuItem inside. Here is the static helper method:
public static void onAction(Menu menu)
{
final MenuItem menuItem = new MenuItem();
menu.getItems().add(menuItem);
menu.addEventHandler(Menu.ON_SHOWN, event -> menu.hide());
menu.addEventHandler(Menu.ON_SHOWING, event -> menu.fire());
}
Then you call:
YourHelperClass.onAction(myMenu);
And ready! I hope this helps.
Recently I faced the same issue, this was my way out:
I had a menuItem in the menu, which was to behave as if the menuItem is clicked (in your case File menu). So what you can do is have a menuItem Dummy_menuItem
final Menu fileMenu = new Menu("File");
fileMenu.getItems().add(new MenuItem("Dummy_menuItem"));
menuBar.getMenus().add(fileMenu);
and then on click of File menu, fire the Dummy_menuItem menuItem or any functionality you wish to have. To identify which menu should have this property, I used numberOfMenuItems to get the number of menuItems in the menus in menubar
if (numberOfMenuItems == 1) {
menu.showingProperty().addListener(
(observableValue, oldValue, newValue) -> {
if (newValue) {
// the first menuItem is triggered
menu.getItems().get(0).fire();
}
}
);
}
the outcome is that the Dummy_menuItem is triggered without the context displaying the menuItem on click of File menu or any menu that has one menuItem. So it appears as if you clicked the File menu and were redirected to another page or whatever.
Hope this helps!!
I think you can't allow any action on the main Menu label.
However, you can create a stackpane, and fill it with text and a menu bar.
I'm making a MenuBar, and I wan't the functionality to press a Menu like: "File" and then execute a action. Such like opening an other fxml, or an example where some output is written.
I want the functionality of a MenuItem (lie "About") in a Menu like "File".
package model;
import static java.lang.System.out;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.geometry.Side;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/**
* Example of creating menus in JavaFX.
*
* #author Dustin
*/
public class JavaFxMenus extends Application
{
/**
* Build menu bar with included menus for this demonstration.
*
* #param menuWidthProperty Width to be bound to menu bar width.
* #return Menu Bar with menus included.
*/
private MenuBar buildMenuBarWithMenus(final ReadOnlyDoubleProperty menuWidthProperty)
{
final MenuBar menuBar = new MenuBar();
// Prepare left-most 'File' drop-down menu
final Menu fileMenu = new Menu("File");
menuBar.getMenus().add(fileMenu);
//menuBar.getOnMouseClicked().handle(this);
// Prepare 'Examples' drop-down menu
final Menu examplesMenu = new Menu("JavaFX 2.0 Examples");
examplesMenu.getItems().add(new MenuItem("Text Example"));
examplesMenu.getItems().add(new MenuItem("Objects Example"));
examplesMenu.getItems().add(new MenuItem("Animation Example"));
menuBar.getMenus().add(examplesMenu);
// Prepare 'Help' drop-down menu
final Menu helpMenu = new Menu("Help");
helpMenu.setOnAction(null);
final MenuItem searchMenuItem = new MenuItem("Search");
searchMenuItem.setDisable(true);
helpMenu.getItems().add(searchMenuItem);
final MenuItem onlineManualMenuItem = new MenuItem("Online Manual");
onlineManualMenuItem.setVisible(false);
helpMenu.getItems().add(onlineManualMenuItem);
helpMenu.getItems().add(new SeparatorMenuItem());
final MenuItem aboutMenuItem =
MenuItemBuilder.create()
.text("About")
.onAction(
new EventHandler<ActionEvent>()
{
#Override public void handle(ActionEvent e)
{
out.println("You clicked on About!");
}
})
.accelerator(
new KeyCodeCombination(
KeyCode.A, KeyCombination.CONTROL_DOWN))
.build();
helpMenu.getItems().add(aboutMenuItem);
menuBar.getMenus().add(helpMenu);
// bind width of menu bar to width of associated stage
menuBar.prefWidthProperty().bind(menuWidthProperty);
return menuBar;
}
/**
* Start of JavaFX application demonstrating menu support.
*
* #param stage Primary stage.
*/
#Override
public void start(final Stage stage)
{
stage.setTitle("Creating Menus with JavaFX 2.0");
final Group rootGroup = new Group();
final Scene scene = new Scene(rootGroup, 800, 400, Color.WHEAT);
final MenuBar menuBar = buildMenuBarWithMenus(stage.widthProperty());
rootGroup.getChildren().add(menuBar);
stage.setScene(scene);
stage.show();
}
/**
* Main executable function for running examples.
*
* #param arguments Command-line arguments: none expected.
*/
public static void main(final String[] arguments)
{
Application.launch(arguments);
}
}
AFAIK, A Menu, if has not any added submenu or Menuitems, does not fire events neither on click, on shown nor on hide. However the workaround is to set its graphic where this graphic node will handle mouse clicks for example,
Label menuLabel = new Label("File");
menuLabel.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
Stage myDialog = new Stage();
myDialog.initModality(Modality.WINDOW_MODAL);
Scene myDialogScene = new Scene(VBoxBuilder.create()
.children(new Text("Hello! it's My Dialog."))
.alignment(Pos.CENTER)
.padding(new Insets(10))
.build());
myDialog.setScene(myDialogScene);
myDialog.show();
}
});
Menu fileMenuButton = new Menu();
fileMenuButton.setGraphic(menuLabel);
menuBar.getMenus().add(fileMenuButton);
A drawback of this approach is that the label do not cover all spaces of the menu resulting clicking on edges of menu is not triggering the mouse event. See this by uncommenting menuLabel.setStyle line above. But this can be resolved by playing with CSS styles I think.
Code is partially taken from Create Dialog using Stage. You can also load an FXML file into the myDialog stage using the FXMLLoader. There are lots of examples about it on the net.
Recently i had the same problem, this is what i did
#FXML private Menu myMenu;
#Override
public void initialize(URL url, ResourceBundle rb) {
myMenu.setGraphic(
ButtonBuilder.create()
.text("btnText")
.onAction(new EventHandler<ActionEvent>(){
#Override public void handle(ActionEvent t) {
//TODO
} })
.build()
);
}
Combining with the answer from our friend #Dota2, i built a helper class to trigger the Menu's onAction(Menu menu) event even if it does not have any MenuItem inside. Here is the static helper method:
public static void onAction(Menu menu)
{
final MenuItem menuItem = new MenuItem();
menu.getItems().add(menuItem);
menu.addEventHandler(Menu.ON_SHOWN, event -> menu.hide());
menu.addEventHandler(Menu.ON_SHOWING, event -> menu.fire());
}
Then you call:
YourHelperClass.onAction(myMenu);
And ready! I hope this helps.
Recently I faced the same issue, this was my way out:
I had a menuItem in the menu, which was to behave as if the menuItem is clicked (in your case File menu). So what you can do is have a menuItem Dummy_menuItem
final Menu fileMenu = new Menu("File");
fileMenu.getItems().add(new MenuItem("Dummy_menuItem"));
menuBar.getMenus().add(fileMenu);
and then on click of File menu, fire the Dummy_menuItem menuItem or any functionality you wish to have. To identify which menu should have this property, I used numberOfMenuItems to get the number of menuItems in the menus in menubar
if (numberOfMenuItems == 1) {
menu.showingProperty().addListener(
(observableValue, oldValue, newValue) -> {
if (newValue) {
// the first menuItem is triggered
menu.getItems().get(0).fire();
}
}
);
}
the outcome is that the Dummy_menuItem is triggered without the context displaying the menuItem on click of File menu or any menu that has one menuItem. So it appears as if you clicked the File menu and were redirected to another page or whatever.
Hope this helps!!
I think you can't allow any action on the main Menu label.
However, you can create a stackpane, and fill it with text and a menu bar.