atarw / material-ui-swing

A modern, Material Design UI for Java Swing
MIT License
653 stars 86 forks source link

JFileChooser not honouring Enter key press #85

Closed klana001 closed 4 years ago

klana001 commented 5 years ago

I have noticed that using:

The expected behaviour when pressing "Enter" in the text field, which is to effectively click the default button (i.e. save/open button), is not performed.

I have made a work around by extending JFileChooser and iterating over the components to find the text field component and then adding a keylistener to it:

public class JFileChooserEnterAware extends JFileChooser { static private Component findTextField(Component comp) { if (comp instanceof JTextField) return comp; if (comp instanceof Container) { Component[] components = ((Container)comp).getComponents(); for(int i = 0; i < components.length; i++) { Component child = findTextField(components[i]); if (child != null) return child; } } return null; }

public JFileChooserEnterAware(String path)
{
    super(path);

    JTextField textField = (JTextField) findTextField(this);
    JButton defaultButton = this.getUI().getDefaultButton(this);

    textField.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyReleased(KeyEvent arg0) {
            if (arg0.getKeyCode() == KeyEvent.VK_ENTER)
            {
                defaultButton.doClick();
            }
        }

        @Override
        public void keyPressed(KeyEvent arg0) {
            // TODO Auto-generated method stub

        }
    });
}

}

vincenzopalazzo commented 5 years ago

Hi @klana001,

If can you give me a simple example runnable it will is fantastic, also yesterday I have released a new version beta in this branch, with this version work or have same problems?

klana001 commented 5 years ago

@vincenzopalazzo It is readily repeatable... just call JFileChooser.showSaveDialog().

e.g.

class test extends JFrame { public static void main (String[] args) { new test(); }

public test() { JFileChooser.showSaveDialog() } }

Your beta branch seems to have corrected the issue. Cheers

vincenzopalazzo commented 5 years ago

@klana001 thanks, it isn't only my version beta but is the version beta for this project.

When the version1.0 is stable the branch will join on this project, for the moment I suggest you used the branch beta it introduced the new style of the component, an example to JComboBox and JDialog.

If you want to try the JDialog beta you can add this code JDialog.setDefaultLookAndFeelDecorated(true);

Thanks for your help

vincenzopalazzo commented 5 years ago

@atarw can you close this issue, please? because of this PR fix it