Use JFileChooser without using a main declaration

时光怂恿深爱的人放手 提交于 2019-12-11 05:50:04

问题


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:


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();
  }
}



回答2:


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



来源:https://stackoverflow.com/questions/10907738/use-jfilechooser-without-using-a-main-declaration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!