Solved

how to use jFileChooser from netbeans

Posted on 2010-11-15
8
1,299 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rhino JavaScript import, load 25 84
Why my table column Id is not passed to java object? 4 39
use lov values 2 50
Error in @AspectJ Based AOP with Spring 2 13
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

810 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