• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wildcard characters (**) when selecting files

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a file chooser in my program that is only supposed to pull in .htm and .html files. When using the (**) trick and hitting enter, you can see all the files in the directory and select any file to import. Well, this will cause the program to crash because it's not supposed to pull in files of different type. I have exceptions to handle it, so the program won't necessarily crash but I don't want the user to even be able to do this. Is there a line of code I can include to prevent the users from viewing all the files? It's hard to search for this topic on the internet so I figured I'd post it and see if someone knows a simple way to handle this.

Screenshot included to see what I'm talking about if you have questions.


Thanks.
fileChooser.JPG
[Thumbnail for fileChooser.JPG]
screenShot
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using the Swing FileChooser? If you are look at tutorial It has an example that shows how to restrict the files
 
Greenhorn
Posts: 22
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friend,

First i understand your problem, then i found solution for crash as wel as trick (**).

1)when user type ** then one dailog box will be appear front then give message to you , your are using trick ** and dnot use this


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;






public class Test implements ActionListener {

public static void main(String args[]){

JFrame jf = new JFrame("FileChooser");

final JFileChooser jfc = new JFileChooser();
jfc.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter(".html & .htm", ".html");
jfc.setFileFilter(filter);
jf.add(jfc);
//File f = jfc.getSelectedFile();
jfc.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{

try
{
File file = jfc.getSelectedFile();
String type = jfc.getTypeDescription(file);
System.out.println("File youfile chose type is"+ type);

if(type.equals(".html"))
{

}
else

{

JOptionPane.showMessageDialog(null, "please dont use trick **");

}
}
catch(Exception ioe)
{
}
}
});
//
//System.out.println("File you chose type is"+ type);
jf.setSize(300, 300);

jf.setVisible(true) ;





}

@Override
public void actionPerformed(ActionEvent e) {



// TODO Auto-generated method stub

}}
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sourav jain wrote:First i understand your problem, then i found solution for crash as wel as trick (**).


This doesn't solve the problem as the user can still enter "*" and see and select any file. All it does is display a warning about the selection if the selected file isn't of type HTML and it does this even if Cancel is pressed.

To be fair to sourav I'm not aware of any easy method of stopping "*" displaying all files. If there isn't a way of doing it through JFileChooser there may be a third party File Chooser type dialog that does handle this or you could just check the selected file and if it doesn't have the correct extension redisplay the file chooser.
 
Joe Niksa
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see. Yes, basically my code will let someone add any file type but I have Exceptions that print out in my log that will give an error message saying that this file could not be converted. Good idea Tony, I didn't think about checking the file before it gets put into the array before my program even begins to even try to perform the commands on the files. This is what I will work on next.

On second thought. I just tested Sourav's code and this should suffice! It prevents the user from adding a file not of type .html which is what I'm looking for. And it keeps the file chooser open. I will probably have it redisplay a new file chooser afterwards like you said though. I'm sure that no one using my program will use "*" and actually try to look for an incompatible file but I want to make it error proof before I send it out for verification from my work's IT team.

Thanks everyone!
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I just tested Sourav's code and this should suffice! It prevents the user from adding a file not of type .html which is what I'm looking for. And it keeps the file chooser open


No it doesn't. It appears to do this because the JFileChooser is embedded into a JFrame but if you extract it from the JFrame and have it popup as a dialog ie by calling showOpenDialog() then it doesn't prevent the wrong file being chosen and it doesn't keep the dialog open.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm sure that no one using my program will use "*" and actually try to look for an incompatible file but I want to make it error proof before I send it out ...


To quote Douglass Adams "A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools"
 
Joe Niksa
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rats! Yes I see what you are saying now. Well it's back to the lab again.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been looking through the sources and I'd guess the way to go is to write a custom UI delegate extending the UI delegate of your LaF (e.g. MetalFileChooserUI) and override getApproveSelectionAction() to return an Action that doesn't add and select the GlobFilter. Possibly even an Action that extends ApproveSelectionAction with a do-nothing return for the conditions that lead to the GlobFilter being added, followed by a call to the super implementation.

Yeah that's a wall of text. What I mean is something on the lines of (typed/copy/pasted here, may have typos or other absurdities)
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice one Darryl.
 
reply
    Bookmark Topic Watch Topic
  • New Topic