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
Solved

Using JFileChooser

Posted on 2004-09-09
5
263 Views
Last Modified: 2008-03-17
Hi,

I do not do GUI programming in Java. However, I have a simple class that I run all the time (it simply spits output to System.out) but it works on different files.

I want to implement the ability to choose a file from the file system.

I found some code on JFileChooser at:
http://java.sun.com/j2se/1.3/docs/api/javax/swing/JFileChooser.html

but it won't compile because it requires some parent "Component" object. I think maybe I need to create AWT somehow. But I know nothing about AWT.

So if someone could please show me some code that can open a file from the file system from a regular java class then it would help me a lot.

Actually I would prefer to use the Windows Open File Dialog but I suspect that is more difficult to do.  But if you know how to use that instead of JFileChooser then feel free to demonstrate that approach. Otherwise I would be happy to know how JFileChooser works.

thanks!
0
Comment
Question by:ycomp
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:bvanderveen
ID: 12020012
0
 
LVL 7

Expert Comment

by:bvanderveen
ID: 12020281
JFileChooser will be similar to the Windows Open dialog.  Calling this would involve some JNI programming, which I think is more complex than you want.

Where are you calling from?  It would be pretty unusual to open a dialog from a CLI, so I assume there is some application at work here.  Is this AWT/Swing, or a web app?

I got the demo to work (with a warning - it is looking for a file that doesn't exist on my FS).   What error do you get if you run the demo source directly?  (You are on JDK 1.4.x?)
0
 

Author Comment

by:ycomp
ID: 12020328
I couldn't get it to run because I didn't know what a Compoonent Class was (the parent object it uses as a parameter).

JFileChooser is ok if Windows Open is too difficult. I just need to select a file.

I just have a normal java app. there is no gui stuff in it. No AWT/Swing, etc. It is just a test app that tests part of the library I'm building. I also know nothing about AWT, Swing, etc.

I am using JDK 1.3.1
0
 
LVL 7

Accepted Solution

by:
bvanderveen earned 500 total points
ID: 12020613
Component is a parent AWT class.  JFileChooser is an extension of it.   You do need a parent AWT/Swing object to display your results in.  This code will work.  I am creating a JPanel for the data:

import javax.swing.*;

/*
 * Created on Sep 9, 2004
 *
 * Created by bvanderveen
 * File: MyChooser.java
 */

/**
 * @author bvanderveen
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class MyChooser extends JFileChooser {
   
    public static void main(String[] args) {
        MyChooser chooser = new MyChooser();
        JFrame frame = new JFrame("chooser");
        int returnVal = chooser.showOpenDialog(frame);
        if(returnVal == JFileChooser.APPROVE_OPTION)
            System.out.println("Chose file: " + chooser.getSelectedFile().getPath());
        else
            System.out.println("you did not choose a file");
       
    }

}
0
 

Author Comment

by:ycomp
ID: 12020785
cool, exactly what I wanted. Thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

840 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