Unfortunately I did not learn Java, I know other programming languages with the exception of Java.
I know how to create a Menu and add items to the menu. I figured that out from my simple basic java books. However I have access to a piece of open source software and need to make some tweaks to it, to make it work for me and I am stuck. So I will provide code of what is going on and provide code of what I have and then hopefully someone can fill in the missing pieces and explain as well as to how it worked they way it worked.
The code below is part of the file that calls TranslatorAction.java It calls it on the line when creating a new menu item. I completely understand this part. I only posted this code so you can see that the import statement is not being imported for JMenuItem and that the menu item is being added indirectly from the menu.add call. The adminMenu.add is creating the menu item by creating a object / class?? with the paremeters new TranslatorAction
javax.swing.JMenu;
import javax.swing.JMenuBar;
import com.bo.actions.TranslatorAction;
private void createAdminMenu(JMenuBar menuBar) {
JMenu adminMenu = new JMenu(com.POSConstants.ADMIN);
adminMenu.add(new TranslatorAction());
menuBar.add(adminMenu);
}
When new TranslatorAction() is called from the above for creating the new menu item. I am having an issue with accessing the setting the text for the new JMenuItem. I know from playing with the code that the line super("test"); is setting the new JMenuItem text. I want to be able to have this set to a variable, so that way it can be changed on the fly. Below is the whole file that gets called from above with adminMenu.add(New TranslatorAction());
TranslatorAction java file code
package com.bo.actions;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JTabbedPane;
import com.POSConstants;
import com.bo.ui.BackOfficeWindow;
public class TranslatorAction extends AbstractAction {
public TranslatorAction(){
//I know this sets the JMenuItem to Test. Again no importing of
//JMenuItem at all what so ever. But I need to set the JmenuItem
//to something on the fly instead of hard coding it. Can anyone
//show and explain how to dynamically create / change super("test"),
// so that way it is not hard coded?
super("Test");
}
public TranslatorAction(String name) {
super(name);
}
public TranslatorAction(String name, Icon icon) {
super(name, icon);
}
public void actionPerformed(ActionEvent e) {
}
}
Thanks for every ones' time for helping me. Shawn
You can dynamically set a JMenuItem
s text with the JMenuItem#setText(String)
method.
A simpler solution without using separate classes would be to create your own JMenuItem
, set its action using anonymous classes, and add it to the JMenu
.
final JMenu menu = new JMenu("Test JMenu");
final JMenuItem menuItem = new JMenuItem("Initial Text");
menuItem.setAction(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
menu.setText("Different Text");
}
});
menu.add(menuItem);
Note that if you are planning on implementing the MVC pattern (Which @MadProgrammer and I highly recommend), your method call to the controller would be in the actionPerformed(...)
method. More information on the MVC pattern can be found in this answer.
If you are really set on adding an AbstractAction
to your JMenu
, you're going to need to add a way to give
your action access to an instance of the JMenuItem
it's going to be added to.
You can get this JMenuItem
by using myJMenu.getItem(menu.getItemCount() - 1)
. This gets the last JMenuItem
in the JMenu
. JMenuItem
s that are added with the JMenu#add(Action a)
are added to the end of the JMenu
, as stated in the documentation.
Since there is no way to get the JMenuItem
created until after the call to add(Action a)
, to give your custom AbstractAction
the JMenuItem
instance you can add a setter method (setJMenuItem(JMenuItem item)
) to your custom action.
My reworked AbstractAction
implementation looks like this:
public class CustomAction extends AbstractAction {
private JMenuItem item;
public CustomAction() {
super("Initial Text");
}
public void setJMenuItem(JMenuItem item) {
this.item = item;
}
@Override
public void actionPerformed(ActionEvent e) {
item.setText("Different Text");
}
}
Make sure to set the JMenuItem
after you add the action because in this case if the actionPerformed(...)
method is called and item
is null
a NullPointerException
will be thrown.
final JMenu menu = new JMenu("Test JMenu");
final CustomAction action = new CustomAction();
menu.add(action);
action.setJMenuItem(menu.getItem(menu.getItemCount() - 1));