Solved

how to use jFileChooser from netbeans

Posted on 2010-11-15
8
1,337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
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.

691 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