I have a Java GUI project containing a JMenuBar, and I just added a JToolBar. In the previous version, events were implemented in the same class that extends JMenuBar. I found it lame and ported events in another class that extends AbstractAction. My goal is to centralize all common events in order to make them respond to different sources (JMenuBar, JToolBar, etc.). But I have a problem with the JFileChooser.showOpenDialog () method. This method takes as an argument the parent component for the dialog. If I do this:
import java.awt.*; import java.awt.event.*; import java.io.File; import javax.swing.*; import javax.swing.event.*; public class ActionUsuels extends AbstractAction { private String nameAction; private MyFileChooser fc; private OpenSave openSave; ActionUsuels(String inName, String inPathIcon) { nameAction = inName; putValue(Action.NAME, inName); putValue(Action.SMALL_ICON, new ImageIcon(inPathIcon)); putValue(Action.SHORT_DESCRIPTION, inName); this.fc = new MyFileChooser(); this.openSave = new OpenSave(Panneau.getUnivers()); } public void actionPerformed(ActionEvent e) {
I get the following error:
The showOpenDialog method (component) in the JFileChooser type is not applicable for arguments (ActionUsuels).
I assume this is normal because ActionUsuels does not extend to any JComponent class. But how can I overcome this? Am I trying to do bad practice? My intention is to record events once and be able to call them from any component.
Just for you to understand what I'm doing, I have this in the menu class:
actions = new ActionUsuels[nameActions.length]; for(int i = 0; i < nameActions.length; i++) { actions[i] = new ActionUsuels(nameActions[i], pathIcons[i]); } file_menu.add(actions[0]); file_menu.addSeparator(); file_menu.add(actions[1]);
Each element is associated with an action name, icon, and a suitable event!
Any idea?
Thanks!
source share