?
Solved

Using JFileChooser

Posted on 2004-09-09
5
Medium Priority
?
266 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
[X]
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
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

764 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