Solved

how to use jFileChooser from netbeans

Posted on 2010-11-15
8
1,288 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

932 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now