Java, JFileChooser. File filter description is duplicated



  • Hello! I made a simple window where there's one button. She's opening a dialogue. At every pressure, there's another line on the file. Can you tell me what the problem is? введите сюда описание изображения

    //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.");
                }
            }
        });
    //.....
    }
    

    }



  • 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. https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2