Making a JFileChooser select the text of the file name but not the extension

馋奶兔 提交于 2019-12-04 05:15:16

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();
            }
        });
    }
}

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!