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

  • SEARCH
  • Home
  • 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 7060783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:23:38+00:00 2026-05-28T04:23:38+00:00

I have a JButton which launches a JFileChooser. However the JFileChooser often takes a

  • 0

I have a JButton which launches a JFileChooser. However the JFileChooser often takes a few seconds to come up, during which time the user might think that nothing is happening. I tried to make the button become disabled until the JFileChooser is finished with, but the disabling of the button doesn’t even happen until the JFileChooser is loaded. Is there something I can do?
My code:

public void actionPerformed(ActionEvent e) {
    System.err.println("clicked");
    ((JButton) e.getSource()).setEnabled(false);
    System.err.println("set");
    JFileChooser b = new JFileChooser("C:\\");
    b.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int res = b.showOpenDialog((Component) e.getSource());
    if (res == JFileChooser.APPROVE_OPTION) {
        try {
            //Blah
        }
        catch (Exception err) {
            JDialog j = new JDialog(window, "An error occured:\n" + err.getMessage());
        }
    }
    ((JButton) e.getSource()).setEnabled(true);
}
  • 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-28T04:23:39+00:00Added an answer on May 28, 2026 at 4:23 am

    Move these lines..

    JFileChooser b = new JFileChooser("C:\\");
    b.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    

    ..from the action performed method to the constructor of the action listener, change them to..

    b = new JFileChooser("C:\\");
    b.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    

    ..then declare..

    private JFileChooser b;
    

    ..as a class attribute (so it is visible to the action performed method).

    You might also want to give it a better name.


    The chooser will be constructed when the class is created, and be ready for use when needed. This has the added benefit that the chooser will remember the position, size, path & file display type, the subsequent times the user activates the button.


    Yet another strategy is to declare the chooser as a class attribute, don’t instantiate it in the constructor, but check in the action performed if it is null, and if so, create and configure it.

    Continued..


    I might want to do something a bit slower like this at some point, that doesn’t quite deserve its own thread.

    With the last strategy I outlined, I was considering adding something that I will add now.


    ..Continued

    ..Of course, that last method will give exactly the same problem you describe, but just once when the user 1st clicks the button. For that situation, you should probably be looking to pop a JOptionPane with an indeterminate JProgressBar from inside a SwingWorker.

    Obviously, a SwingWorker creates a new Thread, but OTOH, Thread objects in Java are cheap. There are a number of them running for any app. with a GUI. A couple more will not hurt.

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

Sidebar

Related Questions

I have a JList, and a JButton, user can click an item in the
Consider the following scenario: I have a JFrame Main which has a jButton caravanDataButton
I have a HashMap of JButtons which represents pictures. this.nahledy.put(s.getName(), new JButton()); this.nahledy.get(s.getName()).addActionListener(); but
I have a JTextComponent in which a user can enter text in two ways:
i have a JFrame that contain a JButton when JFrame shows up, user will
I Have one JButton variable which i used to create different buttons with different
I have a JButton which, when pressed, changes background color from active to normal:
I see that there is a JButton constructor which takes an Icon as a
I have a simple TreeCellRenderer which extracts a JButton from the node, and outputs
I have an array of JButtons which form a keypad interface. After six numbers

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.