3

I want user to select files from existing files only and don't want to let him type the file name.
So how can we disable the file input field in JFileChooser?
Thank you.

Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
Supereme
  • 2,379
  • 12
  • 46
  • 67

4 Answers4

8

I think what you really want is for the user to select an existing file.

You would write an action listener for the JFileChooser to check and make sure the file exists.

You still want the user to be able to type in the file input field so he can limit what's displayed in the JFileChooser,

For example, if the user types *.txt, only .txt files are displayed. If the user types one*, only files that start with the letters o-n-e are displayed.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
7

Completely agree with Gilbert. But in the odd case if you actually want what you asked for: That text field defined inside plaf implementation, and you can use, let say reflection, to get it and change it, or you can try to iterate trough child components hoping that the only JTextArea is the one you looking for. The following example uses reflection and works for Metal look and feel:

import java.awt.Frame;
import java.lang.reflect.Field;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.plaf.metal.MetalFileChooserUI;

public class FileChooser {

public static void main(String[] args) throws Exception{ Frame f = new JFrame(); JFileChooser jFileChooser = new JFileChooser(); MetalFileChooserUI ui = (MetalFileChooserUI)jFileChooser.getUI(); Field field = MetalFileChooserUI.class.getDeclaredField("fileNameTextField"); field.setAccessible(true); JTextField tf = (JTextField) field.get(ui); tf.setEditable(false); tf.setEnabled(false); jFileChooser.showDialog(f, "Select"); f.dispose(); }

}

Yuriy
  • 151
  • 1
  • Brilliant! That's exactly what I wanted to do (I have this option called "export all", which correspond to items on a list, so they already have their names, and I just want to select the folder). – Ericson Willians Sep 28 '16 at 13:47
3

You can go through children components hierarchy (like Yuriy mentioned). Here is the function to find JTextField in components hierarchy. It finds 1st JTextField (hoping that needed text field is the only one in FileChooser).

{
    JFileChooser fc = new JFileChooser();
    disableTF(fc);
}

public boolean disableTF(Container c) {
    Component[] cmps = c.getComponents();
    for (Component cmp : cmps) {
        if (cmp instanceof JTextField) {
            ((JTextField)cmp).setEnabled(false);
            return true;
        }
        if (cmp instanceof Container) {
            if(disableTF((Container) cmp)) return true;
        }
    }
    return false;
}
maslovalex
  • 339
  • 1
  • 6
2

This is an extension to a solution posted at http://www.mikepot.com/1493.html.

The difference is that single clicks are converted to double clicks. This seems to work better under Ubuntu where double clicks seem to get translated into single clicks.

package com.troyware.inventoryItemManager;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JList;

/** 
 * This file chooser prevents users from changing file names and thus should facilitate navigating to the correct file 
 * by precluding the user from getting stuck when single clicking a folder name
 * 
 * @author http://www.mikepot.com/1493.html
 * slightly modified by Phil Troy, www.PhilTroy.com, to convert single click into double click
 */
public class MyFileChooser extends JFileChooser
{

    public MyFileChooser()
      { JList list = findFileList(this);
            for (MouseListener l : list.getMouseListeners())
            {   if (l.getClass().getName().indexOf("FilePane") >= 0)
                {   list.removeMouseListener(l);
                    list.addMouseListener(new MyMouseListener(l));
                }
            }
      }

      private JList findFileList(Component comp)
      { if (comp instanceof JList) return (JList)comp;
            if (comp instanceof Container)
            {   for (Component child : ((Container)comp).getComponents())
                {   JList list = findFileList(child);
                    if (list != null) return list;
                }
            }
            return null;
      }

      private class MyMouseListener extends MouseAdapter
      {
        MyMouseListener(MouseListener listenerChain)
        {   m_listenerChain = listenerChain;
        }


        public void mouseClicked(MouseEvent event)
        {   if (event.getClickCount() > 0)
            {   m_listenerChain.mouseClicked(new MouseEvent(event.getComponent(),  event.getID(), event.getWhen(), event.getModifiers(), event.getX(), event.getY(), 2, false)); 
            }
        }

        private MouseListener m_listenerChain;
      }

    private static final long serialVersionUID = 1L;
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Phil Troy
  • 36
  • 1