Question

I am trying to compile a program that uses the JFileChooser class in Java from command prompt. My question is that if there is any type of a generic argument I can use for showOpenDialog() as a component to open a dialogue box. Here's a sample code:

  public class FileChooser{
      public static void main(String args[]) {
              JFileChooser chooser = new JFileChooser();
          FileReader reader= null;
          Scanner scanner= null;

          int result = chooser.showOpenDialog(null);
          if (result == JFileChooser.APPROVE_OPTION){
              File file = chooser.getSelectedFile();
              try{
              reader = new FileReader(file);
              } catch (IOException io){
                  System.out.println("File not found");
              }
              scanner = new Scanner (reader);   
              scanner.useDelimiter("\\Z");
              String s = scanner.next();
              System.out.println(s);

              }

              scanner.close();
              }
          }
Was it helpful?

Solution

I assume you are asking for showOpenDialog() argument (not JFileChooser constructor argument). You can use null. See JFileChooser.showDialog() for more details. Here's the code you need

JFileChooser chooser = new JFileChooser();
int result = chooser.showOpenDialog(null);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top