Solved

how to use jFileChooser from netbeans

Posted on 2010-11-15
8
1,274 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

708 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

18 Experts available now in Live!

Get 1:1 Help Now