Hello! Made a simple window containing one button. She opens the dialogue. Each time you press, another line appears in the "File Type" list. Tell me please, what's the problem? enter image description here

//ImageFilter1.java public class ImageFilter1 extends FileFilter { private String description; private String[] extensions=null; private String extension=null; //... public String getDescription(){ return description; } } //JChooserTest public class JChooserTest{ //..... JFileChooser choose = new JFileChooser(); show.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String[] exts = new String[]{".png",".jpg"}; choose.setFileFilter(new ImageFilter1("Images: *.png *.jpg", exts)); int returnVal = choose.showDialog(frame, "Attach"); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = choose.getSelectedFile(); System.out.println("Opening: " + file.getName() + "."); } else { System.out.println("Open command cancelled by user."); } } }); //..... } } 
  • Show the ImageFilter1 class completely - Denis

1 answer 1

And so?

 JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter( "JPG & GIF Images", "jpg", "gif"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(parent); if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); } 

see javax.swing.JFileChooser

  • Thank you for another option of applying the filter. I was guided by this: docs.oracle.com/javase/tutorial/uiswing/components ... I was prompted at another forum, there was a common mistake. The filter is applied when the button is pressed as it is in the ActionListener, instead of outside the event. - Romeon0