Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

FileFilter--more SAME Files of Type comes out  RSS feed

 
dejie lin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I got a nice code about file filter from a book, but there's a bug inside, the code looks like:
import javax.swing.filechooser.FileFilter;
import java.io.File;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SaveAction extends JFrame {
public SaveAction() {
initComponents ();
pack();
}
private void initComponents(){
Container f = getContentPane();
f.setLayout(new BorderLayout());
jMenuBar = new JMenuBar();
File = new JMenu("File");
JMenuItem save = new JMenuItem("Save as Image");
File.add(save);

jMenuBar.add(File);
setJMenuBar(jMenuBar);
files = new JFileChooser(DEFAULT_DIRECTORY);
DEFAULT_DIRECTORY = new File("D:/");
save.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
File file =showDialog("Save BOM Diagram As Image","Save","Save",'s',new File(DEFAULT_DIRECTORY,"defaultname"));
if(file!=null){
saveImage(file);
}
}
});
}

private File showDialog(String dialogTitle,
String approveButtonText,
String as,
char approveButtonMnemonic,
File file)
{

files.setDialogTitle(dialogTitle);
files.setApproveButtonText(approveButtonText);
files.setApproveButtonToolTipText(as);
files.setApproveButtonMnemonic(approveButtonMnemonic);
files.setFileSelectionMode(files.FILES_ONLY);
files.rescanCurrentDirectory();
files.setSelectedFile(file);
ExtensionFilter imageFilter=new ExtensionFilter(".gif","Graphics Interchange Format(*.gif)");
files.addChoosableFileFilter(imageFilter);
files.setFileFilter(imageFilter);

int result = files.showDialog(SaveAction.this,null);
return (result == files.APPROVE_OPTION) ? files.getSelectedFile() : null;
}

public static void main (String args[]) {
JFrame saveaction =new SaveAction();
saveaction.show ();
}
public void saveImage(File file){}
private File DEFAULT_DIRECTORY;
private JFileChooser files;
private JMenuBar jMenuBar;
private JMenu File;
}

class ExtensionFilter extends FileFilter
{
public ExtensionFilter(String ext, String descr)
{
extension = ext.toLowerCase(); // Store the extension as lower case
description = descr; // Store the description
}
public boolean accept(File file)
{
return (file.isDirectory() || file.getName().toLowerCase().endsWith(extension));
}
public String getDescription()
{
return description;
}
private String description; // Filter description
private String extension; // File extension, including .
}
The problem is: there will be one more "Graphics Interchange Format(*.gif)" in the field Files of Type comes out after each time the FileChooser dialog shows, another word is there will be 10 more "Graphics Interchage Format(*.gif)" comes out if you press 10 times of File\Save as Image, maybe you can run above the programming and have a look how's the effect.
please give me some suggestion to correct it, thanks a lot!
dejie
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!