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 java.awt.FileDialog

Posted on 1998-05-14
15
340 Views
Last Modified: 2008-02-01
I am using jdk1.1.5 and I want to use the java.awt.FileDialog.
I want the FileDialog to only show directories and not any of the files in those directories.  Is this possible?  If so, how can I accomplish this?
0
Comment
Question by:stepma
  • 5
  • 5
  • 4
  • +1
15 Comments
 
LVL 1

Expert Comment

by:froderik
ID: 1221008
It is possible to show only the directories since you can set a FilenameFilter for your FileDialog object that accepts directories but not files. The user can't pick directories in the FileDialog however which, I guess, is what you want to do.

If you need details about the FilenameFilter implementation I can give you such.
0
 

Author Comment

by:stepma
ID: 1221009
I do need details about the FilenameFilter...
could you show me an implementation of a FilenameFilter as you described above where it will only show directories, and also how to hook it into FileDialog?
0
 
LVL 1

Expert Comment

by:froderik
ID: 1221010
The easiest way is to let your controlling Frame class implement the FilenameFilter. (Adding the words implements FilenameFilter at the class declaration.) Then you have to implement the method accept which in your case would look like this:
boolean accept(File dir, String file)
{
  File someFile = new File( dir, file );
  return someFile.isDirectory();
}

Then somewhere near the creation of your FileDialog you will have to send the setFilenameFilter method to the FileDialog. Like this:
FileDialog fd = new FileDialog( this );
fd.setFilenameFilter( this );
fd.show();

That's about it.

If you would like to place the implementation of FilenameFilter somewhere else you'll have to use such an object as the argument to setFilenameFilter().

Clear enough?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 

Author Comment

by:stepma
ID: 1221011
I did this suggestion and it does not work, when I open the FileDialog, I still see files, not just directories...is there something else that I have to do?
0
 
LVL 1

Expert Comment

by:froderik
ID: 1221012
Hi,

I tried them myself as well and it doesn't seem to work at all to connect a FilenameFilter to a FileDialog. (Have never used this feature myself.) The described way definitely is the way to do it according to documentation available to me. ("Java in a nutshell" and JDK API doc online.) I have used filters with the list method in File though and it works fine all the time.

I will investigate the matter further and I suggest that you reopen the question to other experts in the meantime. Maybe someone already knows about a workaround.
0
 
LVL 4

Expert Comment

by:evijay
ID: 1221013
Hi,

When i reverse engineered the FileDialogPeer source from jdk1.1.6 kit, i found that the method setFilenameFilter is left empty !!.

Vijay

0
 
LVL 4

Expert Comment

by:evijay
ID: 1221014
Hi,

When i reverse engineered the FileDialogPeer source from jdk1.1.6 kit, i found that the method setFilenameFilter is left empty  for both solaris and windows implementation !!.

Vijay

0
 
LVL 1

Expert Comment

by:froderik
ID: 1221015
Which means that the API documentation for FileDialog is erroneous. Too bad.
0
 
LVL 2

Expert Comment

by:webster030697
ID: 1221016
Yes, I seem to remember that the FilenameFilter was left unimplememnted because it couldn't be done in a platform independant way.
0
 

Author Comment

by:stepma
ID: 1221017
Oh well, I guess I am out of luck!!
0
 
LVL 1

Expert Comment

by:froderik
ID: 1221018
Unfortunately, but it shouldn't be that hard to implement a better FileDialog wit the desired functionality if you have plenty of time. Such an implementation could offer the possibility to actually pick directories as well. Some combination of List, TextField and Button on a Panel would do it.

Good luck anyway! :)
0
 
LVL 4

Expert Comment

by:evijay
ID: 1221019
Can you increase the points for i have one implemented by me.


0
 
LVL 4

Accepted Solution

by:
evijay earned 100 total points
ID: 1221020
Use the JFileChooser of swing -1.0.1. it is in com.sun.java.swing.preview package. You can set the file type using
setChoosableFileType(FileType.SharedFolder) and restrict the display. If you are still out of luck, try to change the swing
JFileChooser code (DirectoryModel.java) and see that it gets only files of your choice (directory). Actually, you can implement directory model yourself to see that you will allow it to display directories only.

0
 

Author Comment

by:stepma
ID: 1221021
evijay, Can I see your source code now...
0
 
LVL 4

Expert Comment

by:evijay
ID: 1221022
Did you go thru the jfilechooser component of swing.
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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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.

839 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