Solved

Using JFileChooser

Posted on 2004-09-09
5
258 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
Comment Utility
0
 
LVL 7

Expert Comment

by:bvanderveen
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
cool, exactly what I wanted. Thanks!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 54
topping1 challenge 7 47
Eclipse Neon and jdk 1.8.0 11 63
Python Assistance 7 30
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

763 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

13 Experts available now in Live!

Get 1:1 Help Now