English 中文(简体)
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, 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 !

最佳回答

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! :)

问题回答

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"




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签