?
Solved

Using JFileChooser

Posted on 2004-09-09
5
Medium Priority
?
270 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 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