8

I'd like the text in the file name field of the JFileChooser save dialog to select just the file name and not the extension.

I currently have this:

What it currently looks like

And want it to look like this:

Ideally what it'd look like

This is a simple change, but one that makes saving a file much easier, in my opinion, since the user can start typing the file name right away without erasing the extension accidentally.

I know I can forcefully add the extension if it's missing, but I'd rather not do this because the extension isn't mandatory and I don't feel it should be enforced.

So, is there any way I could achieve this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kat
  • 4,645
  • 4
  • 29
  • 81

3 Answers3

2

The API does not offer that directly but one simple way to do it is to scan the component hierarchy, looking for a JTextField and then change the selection of that textfield.

Here is an example of that solution:

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

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

public class TestJFileChooser {

    public TestJFileChooser() {

    }

    protected void initUI() {
        JFrame frame = new JFrame(TestJFileChooser.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JButton button = new JButton("Click me");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                chooser.setSelectedFile(new File(chooser.getCurrentDirectory(), "save.dat"));
                final JTextField textField = getTexField(chooser);
                if (textField != null) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            String text = textField.getText();
                            if (text != null) {
                                int index = text.lastIndexOf('.');
                                if (index > -1) {
                                    textField.setSelectionStart(0);
                                    textField.setSelectionEnd(index);
                                }
                            }
                        }
                    });
                }
                chooser.showSaveDialog(button);
            }

            private JTextField getTexField(Container container) {
                for (int i = 0; i < container.getComponentCount(); i++) {
                    Component child = container.getComponent(i);
                    if (child instanceof JTextField) {
                        return (JTextField) child;
                    } else if (child instanceof Container) {
                        JTextField field = getTexField((Container) child);
                        if (field != null) {
                            return field;
                        }
                    }
                }
                return null;
            }
        });
        frame.add(button);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestJFileChooser fc = new TestJFileChooser();
                fc.initUI();
            }
        });
    }
}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
0

it seems that it's not possible to configure this on the default component,

i guess you'll have to extend it, this tutorial will help you :

https://today.java.net/pub/a/today/2007/02/22/how-to-write-custom-swing-component.html

gjambet
  • 369
  • 5
  • 13
  • Please avoid posting [Link-only answers](http://meta.stackexchange.com/a/95691/186314). This is not very helpful and your link may get broken at some point, loosing all the relevant information. – Guillaume Polet Jun 14 '13 at 07:56
0

I believe you can write a custom JFileChooser by extending com.sun.java.swing.plaf.windows.WindowsFileChooserUI and override its setFileName() method.

Mubin
  • 4,192
  • 3
  • 26
  • 45
  • But the user seems to be on Linux (from the L&F of his screenshot). Anyway, that's not portable at all. I would not go down that road. – Guillaume Polet Jun 14 '13 at 07:53
  • @GuillaumePolet Yes you are right. Its not portable and will be useful only if on Windows OS. – Mubin Jun 14 '13 at 07:56