Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 531387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:17:45+00:00 2026-05-13T09:17:45+00:00

I have a Java GUI project containing a JMenuBar and I just added a

  • 0

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 !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:17:46+00:00Added an answer on May 13, 2026 at 9:17 am

    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! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 265k
  • Answers 265k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer <ul> <li>Menu Item 1</li> <% if(Roles.IsUserInRole("Administrators")) { %> <li>Menu Item… May 13, 2026 at 12:22 pm
  • Editorial Team
    Editorial Team added an answer Any .NET code, including Windows Forms, uses Unicode encoded as… May 13, 2026 at 12:22 pm
  • Editorial Team
    Editorial Team added an answer One way to do this is to expose the logic… May 13, 2026 at 12:22 pm

Related Questions

I am trying to implement some sort of MVC in Java. Actually it's more
I'm writing a Java application with GUI using Swing. One of the GUI components
I have a very simple problem. I have an application which is written in
I'm adding functionality to an existing Java application that's already been deployed extensively. (So
I'm a fairly experienced Java user, but I've just started trying to use NetBeans

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.