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 8040177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:48:43+00:00 2026-06-05T03:48:43+00:00

Following my previous post in this link I have another problem . Given the

  • 0

Following my previous post in this link I have another problem .

Given the following code :

public class GuiHandler extends javax.swing.JFrame {

// GuiHandler is the class that runs the entire project 
// here are two private fields that I use in the code , I have more but they
// irrelevant for the moment 

final JFileChooser openFiles = new JFileChooser();
private DataParser xmlParser = new DataParser();


// later on I have this method - XMLfilesBrowserActionPerformed


private void XMLfilesBrowserActionPerformed(java.awt.event.ActionEvent evt) {                                                

       //setting the file chooser
    openFiles.setFileSelectionMode(JFileChooser.FILES_ONLY);
    openFiles.setAcceptAllFileFilterUsed(false);
    if (openFiles.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
        //getting the selected file
        File selected = openFiles.getSelectedFile();

// from here I parse the XML file that I opened with JFileChooser 
// with the help of one of my methods getNodeListFromFile
xmlParser.getNodeListFromFile(selected);

The problem is that I cannot use the declaration of main that was suggested before in the link above (and I must add that was a pretty nice one:)), the code that was posted is :

public class NativeFileChooser {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch(Exception e) {
                    e.printStackTrace();
                }
                JFileChooser jfc = new JFileChooser();
                jfc.showOpenDialog(null);
            }
        });
    }
}

And the use of main makes it very difficult for manipulating the XML file later on ,with my
method getNodeListFromFile .

What I need is to use the “simple” browser of JFileChooser , and then use the chosen file , without involving the main into that .

I’d appreciate if someone can explain how can I use the above code (or anything else) with my code .

Best regards

EDIT:

If I use the code like this ;

  public void launchFileChooser() {

     SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(
                        UIManager.getSystemLookAndFeelClassName());
            } catch(Exception e) {
                e.printStackTrace();
            }
            JFileChooser jfc = new JFileChooser();
            jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
            jfc.setAcceptAllFileFilterUsed(false);
            if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
                 newFile = jfc.getSelectedFile();

        }
    });

  }

here newFile is a data member .

then after opening the file , the code crashes .

If I make jfc a data member , the code opens the regular browser . Any idea how to fix it ?

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-06-05T03:48:45+00:00Added an answer on June 5, 2026 at 3:48 am

    I am sure that code was simply a self contained example.

    You should work the JFileChooser into your GuiHandler somehow. If anything, you could add that code to an init method of your GuiHandler

    public class GuiHandler extends javax.swing.JFrame {
    
      //lots of your other code
      public void launchFileChooser() {
         SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch(Exception e) {
                    e.printStackTrace();
                }
                JFileChooser jfc = new JFileChooser();
                jfc.showOpenDialog(null);
            }
        });
      }
    
      public static void main(String[] args) {
         GuiHandler handler = new GuiHandler();
         handler.launchFileChooser();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem following from my previous problem . I also have the
Here's a link to my previous question on this same block of code with
In a previous post ( Don't show this if the url contains the following
This is related to my previous post Problem with downloading multiple files using AsyncTask
I'm experiencing the same problem in this previous stackoverflow.com post . Specifically, I seem
I have the following code, this code was recommended to me by stackoverflow user
I was following a previous post on this that says: For LinkedList get is
This code was established in a previous post. I'm trying to adapt it to
in my previous post I was asking how to generate numbers following a normal
Following on from a previous question , I am creating a symbolic link on

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.