aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Wildcard characters (**) when selecting files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Wildcard characters (**) when selecting files" Watch "Wildcard characters (**) when selecting files" New topic
Author

Wildcard characters (**) when selecting files

Joe Niksa
Greenhorn

Joined: Feb 24, 2012
Posts: 12
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.



[Thumbnail for fileChooser.JPG]

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2409
    
  28

Are you using the Swing FileChooser? If you are look at tutorial It has an example that shows how to restrict the files
sourav jain
Greenhorn

Joined: Nov 19, 2012
Posts: 22
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

}}
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2318
    
  49
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

Joined: Feb 24, 2012
Posts: 12
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

Joined: Aug 07, 2007
Posts: 2318
    
  49
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

Joined: Aug 07, 2007
Posts: 2318
    
  49
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

Joined: Feb 24, 2012
Posts: 12
Rats! Yes I see what you are saying now. Well it's back to the lab again.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

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)


luck, db
There are no new questions, but there may be new answers.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2318
    
  49
Nice one Darryl.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wildcard characters (**) when selecting files