Solved

Using JFileChooser

Posted on 2004-09-09
5
260 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now