I tried to implements programmatic panelmenu using defaultmenumodel and panelmenu with PrimeFaces 4.0. The problem is when I implement a simple model, one or more submenus that contains one o more menuitems runs ok. But when I implement submenus that contains menuitmes and more submenus that containts another menuitems, the defaultmenumodel not shows all levels.
Menu level one
...MenuItem one.one *
...MenuItem one.two *
...SubMenu one.one
......MenuItem one.one.one
......MenuItem one.one.two
...SubMenu one.two
......MenuItem one.two.one
Menu lebel two
... And so on
The MenuItem with * not shown when page is rendered
How can I implement these model of menu using DefaultMenuModel and ??
I want to use pojo to save the menu structure in DataBase for managing.
Thanks
(Added / Edited)
I have run next code suggested but not work using p:panelMenu. With p:menuBAr works well showing an Item and a SubMenu with item inside.
//create the first menu item It is not SubMenu, It's a simple MenuItem
//This item not shows in <p:panelmenu>
DefaultMenuItem accueil = new DefaultMenuItem();
accueil.setStyleClass("only simple menuItem");
accueil.setUrl("/accueil.jsf");
this.menumodel.addElement(accueil);
//This work properly ans shows in <p:panelMenu>
DefaultSubMenu submenu = new DefaultSubMenu();
submenu.setIcon(null);
submenu.setLabel("submenu 01");
this.menumodel.addElement(submenu);
//Add items to submenu
DefaultMenuItem item = new DefaultMenuItem();
item.setValue("Administrar Usuarios");
item.setUrl("/clientapp/modules/admin/manage_users.xhtml");
submenu.addElement(item);
I create my menu like this :
Bean:
private MenuModel menumodel = new DefaultMenuModel();
//create the first menu item
DefaultMenuItem accueil = new DefaultMenuItem("Accueil");
accueil.setStyleClass("accueil");
accueil.setUrl("/accueil.jsf");
this.menumodel.addElement(accueil);
//Start here i create submenu with personal access for all user
for (Autorisation auto : this.permList) {
if (auto.getRessource().getSousMenu() != null) {
if (auto.getRessource().getSousMenu().size() != 0) {
//Create submenu
DefaultSubMenu submenu = new DefaultSubMenu();
submenu.setIcon(null);
submenu.setLabel(auto.getRessource().getMenu());
this.menumodel.addElement(submenu);
for (Ressource r : auto.getRessource().getSousMenu()) {
//Feed submenu with menu item
DefaultMenuItem item = new DefaultMenuItem();
item.setValue(r.getMenu());
item.setUrl(r.getPath());
submenu.addElement(item);
}
}
}
}
XHTML :
<p:menubar model="#{SessionUser.menumodel}"/>
I respond myself.
To use menumodel you need a submenu that wraps (or contains) all menuitems or submenus with their own menuitems.
Create Menumodel.
Create Submenu mySubmenu.
Create one or more Menuitems and then add them to mySubmenu.
Create one or more Submenu objects (that can contains Menuitems
itself) an add to mySubMenu.
finally add mySubmenu that contains all MenuItem and Submenu items to
Menumodel that created initially.
Thanks Lamq, you help me to active my main. (And sorry for my english ::)
Related
I'm designing a project about cataloging something. In this project user must be able to create his own table as he wish. Therefore I do not have any static class and instance of it.
I'm creating a diaglog pane and I can create textfields for user inputs according to column names of database table dynamically but how can i add those user's inputs into the tableView ?
As I can add any String input into the ListView can I add user String inputs into tableView columns?
ListView<String> listView = new ListView();
public ObservableList<String> listCatalogNames = FXCollections.observableArrayList();
listCatalogNames.add("Books");
More details with an example;
There is listview that contains all catalog names and according to lisview selection tableview will be created dynamically center of borderpane.
User have books(name, author, page) and movies(name, year, director, genree) catalogs.
Lets say user selected movies and tableView appeared with 4 columns and clicked add button. Diaglog pane created with 4 textfield. I built everything until that point but I cannot add user's input into the tableView because i dont have any static class for Movies or Books etc.
Is there any way to create dynamic class ?
Please give me an idea and help me about that situation.
here is the github link of our project
Just use String[] storing the Strings for every column of a row (or a similar data structure) as item type for the TableView. It should be simple enough to create a String[] from the inputs and add it to this TableView:
static TableView<String[]> createTable(String... columnNames) {
TableView<String[]> table = new TableView<>();
for (int i = 0; i < columnNames.length; i++) {
final int index = i;
TableColumn<String[], String> column = new TableColumn<>(columnNames[i]);
column.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue()[index]));
table.getColumns().add(column);
}
return table;
}
Adding a String[] userInputs to a TableView<String[]> table would be done like this:
table.getItems().add(userInputs);
A similar issue (creating a TableView based on the metadata of a ResultSet) can be found here: How to fill up a TableView with database data
Easiest solution that comes to my mind is to make use of polymorphism. You can create a super class of both Book and Movie, let's call it Item. Then you can declare your table to contain Item and cast to one of the concrete classes when you need to.
i have a model with an id and a name.
I put the model object into a combobox. From the model i take the name atribute and make that the visual part, the only problem is. When you start up the program the combobox it is empty.
You have to click on it and select the second row to see the item. I would like to see the item straight away. is this possible?
public class ItemCell extends ListCell<Model> {
#Override
public void updateItem(Model person, boolean empty) {
super.updateItem(person, empty);
setText(person == null ? "" : person.getFirstName());
}
}
in my view class i have:
ComboBox<Model> comboBox = new ComboBox<>();
comboBox.setCellFactory(lv -> new ItemCell());
comboBox.setButtonCell(new ItemCell());
comboBox.valueProperty().addListener((o, oldValue, newValue) -> {
personModelFromCombobox = otherObject.getPerson();
});
as you see it does everything correct except you have to click on it and select the second row to see the item.
this happens because you do not select an item in the comboBox
comboBox.getSelectionModel().select(index);
where index is the integer position of the item to select in the selection model, or a value from and of the same type as the arrayList.
I am trying to make a Swing GUI in Netbeans. I create a jcombobox and I bind it (using a query component, a list component and a renderer) to an entity called 'Item', so that the combobox shows the names of the items that currently exist in the table "Item" and so far it works fine. However, I need to add an "All Items" field to the combobox. Does anybody have any hints on where I should start?
Try
List<String> listItems = classDAO.findElement();
DefaultComboBoxModel<String> comboModel = new DefaultComboBoxModel();
for(String string : listItems)
{
comboModel.addElement(string);
}
comboModel.addElement("All items");
JComboBox<String> comboBox = new JComboBox<>(comboModel);
You can manually add an item to the combo box after the items from the table have been added to the combo box:
comboBox.addItemAt("All Items", 0);
will insert a new item at the top of the combo box.
Here's the thing:
I created a 'cocktailbar' software, and I have the following classes:
Cocktail,
CocktailBar,
CreateNewCPanel,
HelloPanel,
SearchCPanel,
ShowAllCPanel,
CocktailMixerGUI,
Ingredients.
Now: When adding a new Cocktail in the CreateNewCPanel, I add the cocktail to a List in the CocktailBar class.
Box buttonBox = Box.createHorizontalBox();
JButton speicherButton = new JButton("Speichern");
speicherButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
neuerC.setCName(cName.getText());
neuerC.fuegeZubereitungHinzu(zubereitungTextArea.getText());
CocktailBar.addCocktail(neuerC);
Now I need to see all created cocktails in a 'dropdown' menu in the ShowAllCPanel. I've got the following:
//Adding the DropDown Menu, first a Box, then a ComboBox inside.
Box cDropDownBox = Box.createHorizontalBox();
cDropDownBox.add(Box.createHorizontalGlue());
JComboBox cChoose = new JComboBox();
groesseEinsetzen(cChoose, 500, 20);
cChoose.setAlignmentX(SwingConstants.LEFT);
cDropDownBox.add(cChoose);
But now I am wondering how do I get my List from the CocktailBar class into the ShowAllCPanel?
edit: forgot to mention: i have a getter in the CocktailBar class, and i already tried:
cChoose.addItem(CocktailBar.getCocktails());
within the comboBox in the ShowAllCPanel, but it doesnt show up anything in the dropdown.
thanks to #Do Re, i inserted this:
//Adding the DropDown Menu, first a Box, then a ComboBox inside.
Box cDropDownBox = Box.createHorizontalBox();
cDropDownBox.add(Box.createHorizontalGlue());
JComboBox cChoose = new JComboBox();
if (CocktailBar.getCocktails() != null){
for (Cocktail c : CocktailBar.getCocktails())
cChoose.addItem(c);
}
but still - when running, the dropdown list stays empty.
As you mentioned in the comments, you created a getter for the cocktails.
Try something like this
for (Coctail c : CocktailBar.getCocktails())
cChoose.addItem(c);
This iterates over the list of cocktails and adds each item seperately rather than adding a list of Cocktails at once.
Edit
try
cDropDownBox.revalidate();
cDropDownBox.repaint();
or
cChoose.revalidate();
cChoose.repaint();
I am building a Spring Rest application for a food store in which the admin can create new Menu everyday by adding, editing or removing menu items as per the occasion. When the admin tries to update the menu, at the server side I am fetching first the menu item list that is associated with the current menu passed and then converting it to hash map so as to find the menu items currently passed with menu. Following is the code snippet to convert the list to map:
private Map<Long, MenuItem> convertIdToMenu(List<MenuItemMappingWrapper> menuItemMappingWrapper){
List<Long> menuItemIdList = new ArrayList<Long>();
for (MenuItemMappingWrapper menuItemWrapper : menuItemMappingWrapper) {
menuItemIdList.add(menuItemWrapper.getMenuItemId());
}
List<MenuItem> menuItemList = menuDao.getMenuItemById(menuItemIdList);
Map<Long, MenuItem> menuItemMap = new HashMap<Long, MenuItem>();
for (MenuItem menuItem : menuItemList) {
menuItemMap.put(menuItem.getId(), menuItem);
}
return menuItemMap;
}
So I can find the menu item by passing its "id" as key to map rather than iterating over the large list of items. Is there a more better to improve the performance on the server side other than converting list to map ?