How to use JFileChooser.showOpenDialog () in a non-component class?

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; /** Instance de MyFileChooser pour explorer les dossiers/fichiers*/ private MyFileChooser fc; /** Instance d'OpenSave qui contient les algorithmes d'ouverture/sauvegarde*/ 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) { // Evรฉnement nouveau projet if(nameAction == "OPEN_PROJECT") { fc.ContMode(); fc.refresh(); int returnVal = fc.showOpenDialog(ActionUsuels.this); if (returnVal == MyFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); openSave.OpenCont(file); } } static ActionUsuels actionInactive; } 

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!

+4
source share
2 answers

Usually the parent class passed to JDialogs is the main JFrame of the application. Among other things, this allows the dialog box to navigate through the application window.

I hope your action class will have access to the main frame and can pass a link to it. One way to achieve this may be to pass the main frame as an argument to the ActionUsuels constructor.

Otherwise, null also a valid parent specification. Given null , the dialog is centered on the screen, but it works fine anyway.

Bonn's chance! :)

+10
source

It is a bad idea:

 if(nameAction == "OPEN_PROJECT") 

You need to use this instead:

 if("OPEN_PROJECT".equals(nameAction)) 

The == operator only checks to see if the two links match, and not whether the lines they point to are the same. This is what the equals method writes for String. This distinction between shallow and deep is equal.

You can see it in action here:

 String x = new String("foo"); // don't write code like this; just an example String y = new String("foo"); // x and y are different reference values System.out.println(x == y); // prints "false" System.out.println(x.equals(y)); // prints "true" 
+2
source

All Articles