views:

125

answers:

2

Hello,

I have a Java GUI project containing a JMenuBar and I just added a JToolBar. In the previous version, the events were implemented in the same class that extends the JMenuBar. I found it lame and moved the events in another class that extends AbstractAction. My aim is to centralize all the common events to make them react 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 method showOpenDialog(component) in the type JFileChooser is not applicable for the arguments (ActionUsuels).

I guess this is normal because ActionUsuels doesn't extend any JComponent class. But how can I overpass that ? Is what I'm trying to do a bad practice ? My intention is to write the events once and be able to call them from any component.

Just to make you 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]);

Every item is associated to the name of the action, an icon and the suitable event !

Any idea ?

Thanks !

+4  A: 

Usually, the parent class passed to JDialogs is the main JFrame of the application. Among other things, this allows for the dialog to be centered over the app's window.

Hopefully your action class will have access to the main frame and can pass a reference to it. One way to achieve this might be to pass the main frame as an argument to the ActionUsuels constructor.

Failing that, null is also a valid parent specification. Given null, the dialog is centered on the screen but generally works OK anyway.

Bonne chance! :)

Carl Smotricz
Thank you very much, it works fine :)
Amokrane
+1  A: 

This 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 two references are equal, not whether or not the Strings they point to are identical. That's what the equals method for String is written to do. It's the difference between "shallow" and "deep" equals.

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"
duffymo
I take note ! But why ?
Amokrane
Because == and equals aren't the same for Strings. You ought to understand the difference to avoid a nasty surprise.
duffymo
Aha ! To be honest, I'm really new to Java. I did C++ and C# in the past. Good to know anyway ! I'm going to investigate this issue :)
Amokrane
@duffymo ...for Strings, and for Objects in general.
Jerome
@Jerome - equals isn't in general written to provide "deep" behavior for all Objects. Only those that override it.
duffymo