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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:37:28+00:00 2026-06-05T05:37:28+00:00

Following my previous two posts here and another one here , the following code

  • 0

Following my previous two posts here and another one here, the following code
opens the regular file browser instead of the expanded one:

public class GuiHandler extends javax.swing.JFrame {
    // data members
    private DataParser xmlParser = new DataParser();
    private File newFile;
    JFileChooser jfc = new JFileChooser();

    // more code 

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

    // more code 

    private void XMLfilesBrowserActionPerformed(java.awt.event.ActionEvent evt) {       
        launchFileChooser();
        xmlParser.getNodeListFromFile(newFile);
        // here the code has the below problems 

Problems:

  1. The code opens a regular file browser when I hit a button to open XML file; it still allows me to pick a file.
  2. It throws an exception:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: File cannot be null
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:198)

Why does it open the regular browser if jfc is a data member, and when it’s a local
variable, the expanded one opens?

  • 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-05T05:37:30+00:00Added an answer on June 5, 2026 at 5:37 am

    Concerning the regular versus expanded file chooser, make sure to call UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); before calling new JFileChooser();. Actually, unless you allow users to change the look and feel (L&F) during application execution, set the L&F close to the beginning of application execution, like in the main method, before creating any Swing components. From my experience, not doing so can cause some odd UI behavior.

    When you have JFileChooser as a local variable in launchFileChooser, UIManager.setLookAndFeel is called before new JFileChooser. When JFileChooser is a class member variable (a.k.a. data member), UIManager.setLookAndFeel is called after new JFileChooser; in the latter case, the JFileChooser is created when an instance of GuiHandler is instantiated.


    Concerning the IllegalArgumentException use SwingUtilities.invokeAndWait in launchFileChooser instead of SwingUtilities.invokeLater. Better yet, if you’re sure launchFileChooser will always occur on the event dispatch thread, there’s no need to call either SwingUtilities.invokeAndWait or SwingUtilities.invokeLater.


    You also may want to use a file filter:

    jfc.setFileFilter(new FileNameExtensionFilter("XML files (*.xml)", "xml"));
    

    The following is an SSCE that demonstrates the concepts discussed above:

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.UIManager;
    import javax.swing.filechooser.FileNameExtensionFilter;
    
    public class GuiHandler extends JFrame {
        public static void main(String[] args) throws Exception {
            // call UIManager.setLookAndFeel early in application execution
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            JFrame frame = new GuiHandler();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    
        private final JFileChooser jfc;
    
        public GuiHandler() {
            this.jfc = new JFileChooser();
            this.jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
            this.jfc.setFileFilter(new FileNameExtensionFilter("XML files (*.xml)", "xml"));
    
            final JButton button = new JButton("Open XML file");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    xmlFilesBrowserActionPerformed();
                }
            });
            add(button);
    
            pack();
        }
    
        protected void xmlFilesBrowserActionPerformed() {
            final File xmlFile = getXmlFile();
            if (xmlFile != null) {
                System.out.println(xmlFile); // process XML file
            }
        }
    
        private File getXmlFile() {
            // At this point we should be on the event dispatch thread,
            // so there is no need to call SwingUtilities.invokeLater
            // or SwingUtilities.invokeAndWait.
            if (this.jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                return this.jfc.getSelectedFile();
            }
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code <a class=getty id=1 href=/...>One<./a> <a class=getty id=2 href=/...>Two<./a> <a
Following on from my previous two questions: How to make jQuery autocomplete execute a
take following class and two object definitions: class Rect{ public: enum centimeter; enum meter;
I have the following matching problem: I have two data.frames, one with an observation
I have the following XML: <system-folder> <system-folder> <system-page> <display-name>One</display-name> </system-page> </system-folder> <system-folder> <system-page> <display-name>Two</display-name>
following this previous question Malloc Memory Corruption in C , now i have another
I have the following two buttons in XAML: <Button Content=Previous Margin=10,0,0,10/> <Button Content=Next Margin=0,0,10,10/>
I'm having trouble figuring why the following code isn't producing the expected output. Instead,
Following my previous post in this link I have another problem . Given the
Following my previous question , I need to create a value consisting of a

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.