-1

I have a minimum Java Swing App to show the problem : when I opened a JFileChooser and tried to double click on a file on the right side column, it will try to move to the left column, no matter how fast I double clik on it, it won't be selected, until it's in the left column position, is this a bug in Java Swing, or am I doing something wrong ? How to fix it ?

enter image description here

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.filechooser.FileFilter;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Demo_File_Chooser_Show_Txt_Only extends JFrame
{
  private JButton buttonBrowse;

  public Demo_File_Chooser_Show_Txt_Only()
  {
    super("Demo File Type Filter");
    setLayout(new FlowLayout());
    buttonBrowse=new JButton("Browse...");
    buttonBrowse.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { showOpenFileDialog(); } });
    getContentPane().add(buttonBrowse);
    setSize(300,100);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  }

  public static void main(String[] args)
  {
    try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
    catch (Exception e) { }
    SwingUtilities.invokeLater(new Runnable() { public void run() { new Demo_File_Chooser_Show_Txt_Only(); } });
  }

  private void showOpenFileDialog()
  {
    JFileChooser fileChooser=new JFileChooser(".");
    fileChooser.setControlButtonsAreShown(false);
    fileChooser.setFileFilter(new FileFilter()
    {
      @Override
      public boolean accept(File file) { return file.getName().toLowerCase().endsWith(".txt"); }

      @Override
      public String getDescription() { return ".txt files"; }
    });
//    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fileChooser.showOpenDialog(null);
  }
}
Frank
  • 30,590
  • 58
  • 161
  • 244
  • `or am I doing something wrong ?` - well first do the test without any custom code. That is get rid of the FileFilter or the set of the control buttons to false. I would say something is weird since you are only trying to accept text files and none of your files are text files. When I run your code I only see the text files. (1-) So there is something out of sync between your posted code and your image. So maybe you aren't even executing the code you think you are testing. – camickr May 25 '19 at 16:21
  • That was the code that ran the program, but I switched "File of type" value to [All Files] at the bottom of the app after opening the app window. – Frank May 25 '19 at 20:07
  • Well then do as I suggested. The point of an [mcve] is to post the minimal code that demonstrated the problem. If the FileFilter is irrelevant to the problem then don't post that code. We are not here to guess the steps that are need to replicate the problem. – camickr May 25 '19 at 20:52

1 Answers1

0

I added the following line.

fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

After adding the above line, you will be able to double click.

Just to check, I also added the following lines to print.

int someValue = fileChooser.showOpenDialog(null);
    if (someValue == JFileChooser.APPROVE_OPTION) {
      File selectedFile = fileChooser.getSelectedFile();
      System.out.println("Selected File : " + selectedFile.getAbsolutePath());
   }
Sambit
  • 7,625
  • 7
  • 34
  • 65
  • I tried it, still didn't work right, the issue was/is : if the file you try to select is not on the left most column, it will "jump", when it's in the left most column, then you can double click and select, so to reproduce the problem, you can double click on a file that is on the right side column, see if it will let you select it. – Frank May 25 '19 at 20:11
  • This is normal windows behaviour. You can try it. Simply open Paint and open a picture folder which has lot of files, also make the folder as list not as details type. – Sambit May 25 '19 at 20:16