On demand population of dropdown menu Java/Swing? - java

When I code a popup menu, I can check the mouse event and then
before calling show() prepare the menu.
Now I want a similar functionality for drop down menus, that
live in the menu bar. For example I have the following menu
bar layout:
Menu 1
MenuItem 1.1
MenuItem 1.1.1
MenuItem 1.1.2
..
MenuItem 1.1.n
Menu 2
Basically I want to generate the list MenuItem 1.1.1, MenuItem 1.1.2,
..., MenuItem 1.1.n dynamically when the drop down menu is invoked and
before it is shown.
How could I do this in Java/Swing? Is there a call back that covers both
L&F specific keyboard/mouse invokations of the menu bar?
Best Regards

You need to use a javax.swing.JMenuBar instead of a java.​awt.MenuBar.
These MenuBars can add javax.​swing.JMenus which have a addMenuListener(MenuListener l) method.
In this MenuListener you can dynamically add and delete the menus.
#Override
public void menuSelected(MenuEvent e) {
// Put your logic here
JMenu dynamicMenu = (JMenu) e.getSource();
dynamicMenu.add(new JMenuItem("MenuItem 1.1.1"));
}
#Override
public void menuDeselected(MenuEvent e) {
// Remove all MenuItems
JMenu dynamicMenu = (JMenu) e.getSource();
dynamicMenu.removeAll();
}
#Override
public void menuCanceled(MenuEvent e) {
// Remove all MenuItems
JMenu dynamicMenu = (JMenu) e.getSource();
dynamicMenu.removeAll();
}
To add the JMenuBar to a JFrame use the setJMenuBar(JMenuBar menubar) method.

Related

Jmenu option onclick get Text

I have the following JMenu:
So, i added the same mouseclicked event in each menu option, so when i click one of them, in the evt, i can get the text. On the event i have the following code:
private void menuMouseClicked(java.awt.event.MouseEvent evt) {
System.out.println(evt.getSource ());
}
Having this, each time i press a option, i get the output:
.....javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=false,paintFocus=false,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Add]
So the thing that i want, is from the evt, get the Text that says in the case of the first option "Add". How can i do this?
Cast it to the JMenuItem and call getText()
private void menuMouseClicked(java.awt.event.MouseEvent evt) {
JMenuItem menuItem = (JMenuItem) evt.getSource();
System.out.println(menuItem.getText());
}
You may want to also look into JMenuItem#addActionListener. You can add an ActionListener to a specific JMenuItem and then use the same logic:
someMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JMenuItem menuItem = (JMenuItem) evt.getSource();
System.out.println(menuItem.getText());
}
});

Change the title text of JMenuItem upon click?

I am adding a JMenuItem (Show History) that will toggle the appearance of a JPanel upon click. But after doing so, I want to change the title of that menu item to state the opposite action (Hide History). Is there a way to change just the text for that menu item, or must I remove the old JMenuItem and add a new one?
JMenuItem history = new JMenuItem("Show History");
history.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//code here to show the history
//history.changeText("Hide History") OR viewMenu.remove(history) and create/add new one
}
});
viewMenu.add(history);
So this is what you do:
history.setText("Hide History");
And make history final.

call actionperformed method on clicking jmenu

can i call actionperformed method with jmenu using swing
i am using the following code
JMenu menu1= new JMenu("File");
MenuBar mb= new MenuBar();
mb.add(menu1);
set JmenuBar(mb)
menu1.addActionListener(this);
public void actionPerformed(ActionEvent ae)
{
JOptionPane.showMessaageDialog(null,"menu clicked");
// but its not working
}
thanks in Advance
The action a JMenu is designed to perform is to open a popup with JMenuItems, it doesn't support doing anything else (and even if it did, it would confuse your users). Custom actions are supposed to be handled by JMenuItems in the popup. Install them with something like:
JMenu menu ..
Action myAction = new AbstractAction("Do XY") {
public void actionPerformed(..) {
// implement doing XY
}
};
menu.add(myAction);

How to disable menu items in desktop application?

I created a menu in the menu bar in which I'd like to create a JCheckBoxMenuItem to set a condition for highlighting remaining menu items.
Something like the following pseudo-code:
if login(true)
then highlight remaining menuitems
else
un-highlight the menuitems
I think for highlighting you mean enable/disable a JMenuItem. That's possible.
Use setEnabled:
JMenuItem item;
item.setEnabled(false); //to disable
Like suggested by kleopatra, the best way of doing that is to implement your own action for each JMenuItem, and let your action to enable/disable the button accordingly to the state:
For example:
public class AMenuAction extends AbstractAction {
#override
public void actionPerformed(ActionEvent e) {
//implement your action behavior here
}
}
Then construct your JMenuItem with such action:
AMenuAction afterLoginAction = new AMenuAction();
JMenuItem item = new JMenuItem(afterLoginAction );
When the user logged in/out call setEnabled method on the desired actions.
void Login()
{
afterLoginAction.setEnabled(true);
}
Enabling and disabling menu items is done in the same way as for any other JComponent by using the setEnabled( boolean ) method
Create a JCheckBoxMenuItem as userlogin menu item
JCheckBoxMenuItem jCheckBoxMenuItem = new JCheckBoxMenuItem();
then
add action listener to it
//unhighlite other menu items before login
jMenuFileOpen.setEnabled(false);
//...
jCheckBoxMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (login(true)){
jCheckBoxMenuItem.setSelected(true);
//highlite other menu items
jMenuFileOpen.setEnabled(true);
//...
} else {
jCheckBoxMenuItem.setSelected(false);
//unhighlite other menu items
jMenuFileOpen.setEnabled(false);
//...
}
}
});
once login(true) is successful the checkbox is checked on menu and other menu items are enabled.

JPopupMenuItem get component that triggered the JPopupMenu

I have got a JRadioButton and set a ComponentPopupMenu to it.
JRadioButton rdbtnTest = new JRadioButton();
rdbtnTest.setComponentPopupMenu(popupMenu);
The popupMenu is a JPopupMenu with one JPopupMenuItem. The JPopupMenuItem has a MouseListener, so that on a mouse click, a webpage is opened.
JPopupMenu popupMenu = new JPopupMenu("GO");
PopupMenuListener popupMenuListener = new MyPopupMenuListener();
popupMenu.addPopupMenuListener(popupMenuListener);
MenuItem openMenuItem = new JMenuItem("Open Webpage");
openMenuItem.addMouseListener(new MouseListener() {
#Override
public void mousePressed(MouseEvent arg0) {
Component comp = arg0.getComponent(); // will only return the JPopupMenuItem
// Determine the right-clicked radio button and open webpage
}
popupMenu.add(openMenuItem);
My question is: if I have another JRadioButton rdbtnTest2 with the same popupMenu, can I determine which of the two radio buttons triggered the JPopupMenu? When opening the webpage, I need to pass a specific parameter according to which radio button was right-clicked.
Whn you show popup menu in a MouseListener just get event source and store it somewhere (radiobutton which calls the menu). In your code just check the popup invoker.

Categories