1

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

Community
  • 1
  • 1
JAN
  • 21,236
  • 66
  • 181
  • 318
  • 1) A GUI class should typically not extend `JFrame` instead the code should just retain an instance of one. 2) The frame in the code shown in the accepted answer will be Metal PLAF (it is created before the PLAF is changed), which might look odd in your application. 3) What is the use-case here? It seems you want a `JFileChooser` to appear before the main GUI, and that the main GUI will never need to open a 2nd XML. Is that right? 4) For better help sooner, post an [SSCCE](http://sscce.org/) (as I posted, and as alluded to by akf). – Andrew Thompson Jun 06 '12 at 06:31

2 Answers2

2

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();
  }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
akf
  • 38,619
  • 8
  • 86
  • 96
  • Thanks . Is there a way to make the browser to show only `.xml` files ? – JAN Jun 06 '12 at 04:09
  • You can create a subclass of `FileFilter` and take care of it yourself. Take a look at this link: http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html#filters – akf Jun 06 '12 at 04:18
  • it is best to add a new question instead of add on to this one. (and when doing so, please expand on what 'the code crashes' means - are there exceptions? – akf Jun 06 '12 at 04:37
  • Yes , `xception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: File cannot be null` , and after hitting `open file` , the program shows that exception in `console` , while my program is running , then I pick my `xml` file , and then nothing happens , meaning the code doesn't parse the `xml` file as it usually does . – JAN Jun 06 '12 at 04:42
  • Note that frame itself will still be metal PLAF unless `updateComponentTree..` is called. Might or might not be a problem here, I'm still not quite sure of the end result required. +1 for noting *"I am sure that code was simply a self contained example"*. Spot on. :) – Andrew Thompson Jun 06 '12 at 06:36
1

Declare any Other class or use the JFrame class and delegate the Call to JFileChooser for there either by declaring new class/interface or over there itself , there is actually no sense of calling JFileChooser from Main method

Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128
  • If I remove the `main` from the code that was posted then it doesn't compile . – JAN Jun 06 '12 at 03:28
  • dont remove the code , I am just saying of not adding JFileChooser inside the Main class , create a new class from the UI Class and there you do the needful , why your main class should be responsible for creating FileCHooser – Abhishek Choudhary Jun 06 '12 at 03:30