Solved

how to use jFileChooser from netbeans

Posted on 2010-11-15
8
1,317 Views
Last Modified: 2012-06-27
im using netbeans jFileChooser to select a file or multiple files. I want to save this selection in an dynamic array. when i drag the jFileChooser onto the jFrame it has save_dialog button and cancel button. How do i make the save_dialog button to save the selected file in an dynamic array.
0
Comment
Question by:siddharthkool
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 25 total points
ID: 34137053
0
 
LVL 92

Assisted Solution

by:objects
objects earned 25 total points
ID: 34140728


If you class has an array name files

public class MyClass
{
    File[] files;


then in your buttons actionPerformed method add the follwoing


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

   files = chooser.getSelectedFiles();
}

Or if using the show methods

        int returnVal = fc.showSaveDialog(...

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            files = chooser.getSelectedFiles();


Let me know if you have any questions
0
 

Author Comment

by:siddharthkool
ID: 34189728
hi this is my source code. im adding the selected .java file from jFileChooser to the list. Then im trying to run the .java file from a event of button and print in text area. here is my source code:

JfilechooserActionPerformed:
 private void jFileChooser1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
           
            try{
            File file = jFileChooser1.getSelectedFile();
            list.add(file.getName());
            System.out.println(list);
            }catch(Exception e){
                System.err.print(e);
            }
        }      

Here is source code for the button:
SwingWorker worker = new SwingWorker<Void, String>() {
                @Override
                public Void doInBackground() {
                    int exitValue = -999;
               try{
                   System.out.println("Executing.....");
                    p = Runtime.getRuntime().exec("cmd.exe -c 'cd \"C:/JDBCRegressionBugs/New folder\";  javac list '");
                   p = Runtime.getRuntime().exec("cmd.exe -c 'cd \"C:/JDBCRegressionBugs/New folder\"; java list '");
                   System.out.println("Running.....");
                   BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
                   String line = null;
                   publish("is64bit = "+is64bit);
                   while ((line = in.readLine()) != null) {
                   System.out.println(line);
                   publish(line);
                   }
                    // clean up if any output in stderr
                   InputStream stderr = null;
                   stderr = p.getErrorStream ();
                   BufferedReader    brCleanUp = new BufferedReader (new InputStreamReader (stderr));
                   while ((line = brCleanUp.readLine ()) != null)
                   {
                       System.out.println ("[Stderr] " + line);
                       publish("[Stderr] " + line);
                   }
                   brCleanUp.close();

                   }catch (IOException e) {
                   e.printStackTrace();
                }
                try
                {
                  p.waitFor();
                  exitValue = p.exitValue();
                  System.out.println("ExitValue = "+exitValue);
                } catch (Exception err)
                {
                  err.printStackTrace();
                }

                 return null;
              }

            @Override
              protected void process(List<String> chunks) {
               for (String chunk : chunks) {
                        System.out.println(chunk);
                        jTextArea5.append(chunk + "\n");
                }
              }
                };
                worker.execute();

        }                        
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:siddharthkool
ID: 34190397
but this button does not seem to work.. the
 p = Runtime.getRuntime().exec("cmd.exe -c 'cd \"C:/JDBCRegressionBugs/New folder\";  javac list '");
                   p = Runtime.getRuntime().exec("cmd.exe -c 'cd \"C:/JDBCRegressionBugs/New folder\"; java list '");
does not seem to work....
0
 
LVL 92

Expert Comment

by:objects
ID: 34192875
> but this button does not seem to work.. the

that looks like a different problem, open a new question and I'll have a look at it for you
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34193182
>>p = Runtime.getRuntime().exec("cmd.exe -c 'cd \"C:/JDBCRegressionBugs/New folder\";  javac list '");

Looks wrong. For one thing, javac takes a file with a .java extension. Make sure you can execute each command at the command line first
0
 

Author Closing Comment

by:siddharthkool
ID: 34197113
solved
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34198695
>>solved

Please say how for the benefit of future visitors. I can see that the last quotation i posted is not a valid command line...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
web application structure 18 111
Unhandled exception type Exception 18 52
Running JavaFX on JDeveloper 12C 1 76
Java pass by reference 3 16
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question