Opening Text with jFileChooser


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Opening Text with jFileChooser

  1. #1
    Join Date
    Oct 2004
    Posts
    2

    Exclamation Opening Text with jFileChooser

    Hello can anyone help me out here i'm new at java programming, i'm using NetBeans3.6.
    Look I want to open a text file using jFileChooser but I don't know how to, i've already opened a text file but without the jFileChooser.

    Here's the code:

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    jTextArea1.setText("");
    String record = null;

    try {

    FileReader fr = new FileReader("/home/gap/Desktop/text.txt");
    BufferedReader br = new BufferedReader(fr);

    record = new String();
    while ((record = br.readLine()) != null) {

    jTextArea1.append(record);
    jTextArea1.append("\n");

    }
    } catch (IOException e) {
    // catch possible io errors from readLine()
    System.out.println("IOException error!");
    e.printStackTrace();
    }
    }

  2. #2
    Join Date
    Sep 2004
    Posts
    223
    here is some code from the java API that may help


    The following code pops up a file chooser for the user's home directory that sees only .jpg and .gif images:
    Code:
        JFileChooser chooser = new JFileChooser();
        // Note: source for ExampleFileFilter can be found in FileChooserDemo,
        // under the demo/jfc directory in the Java 2 SDK, Standard Edition.
        ExampleFileFilter filter = new ExampleFileFilter();
        filter.addExtension("jpg");
        filter.addExtension("gif");
        filter.setDescription("JPG & GIF Images");
        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(parent);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
           System.out.println("You chose to open this file: " +
                chooser.getSelectedFile().getName());
        }
    A kram a day keeps the doctor......guessing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center