JSpinner: autoselect onFocus

南楼画角 提交于 2019-12-24 03:13:08

问题


I want to implement autoselect when a user tabs through the JTextFields and JSpinners in my forms. For this I am using this Listener:

public class AutoSelect implements FocusListener {

    @Override
    public void focusGained(final FocusEvent e) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                if (e.getSource() instanceof JTextField) {
                    try {
                        JTextField t = (JTextField) e.getComponent();
                        t.selectAll();
                    } catch (ClassCastException ex) {
                        // 
                    }
                }else if (e.getSource() instanceof JSpinner){
                    JSpinner spinner = (JSpinner)e.getComponent();
                    JTextField tf = ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField();  
                    tf.selectAll();
                }
            }
        });
    }

    @Override
    public void focusLost(FocusEvent e) {
        // 
    }
}

Only it doesn´t work for my spinners. The event is beeing fired, the correct lines are executed, only nothing happens. I suspect I am not using the .getTextField() correctly. Does anyone have a working solution for this ?


回答1:


  • tf.selectAll(); should be wrapped into invokeLater, everything with Focus is pretty asynchronous (more in Oracle tutorial How to use Focus, FocusSubsystem),

  • then invokeLater (not true in all cases for all JComponents, but by default) for JTextComponents move this event to the end of queue, works for me quite correctly

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComboBoxTwo extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTextField text = new JTextField(15);
    private JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 15, 1));

    public ComboBoxTwo() {
        text.setText("Something selectable");
        text.addFocusListener(fcsListener);
        JFormattedTextField format = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
        // or JTextField tf = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
        format.addFocusListener(fcsListener);
        //or tf.addFocusListener(fcsListener); // depends of type for SpinnerXxxModel 
        add(text, BorderLayout.NORTH);
        add(spinner, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new ComboBoxTwo();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
//
    private FocusListener fcsListener = new FocusListener() {
        @Override
        public void focusGained(FocusEvent e) {
            dumpInfo(e);
        }

        @Override
        public void focusLost(FocusEvent e) {
            dumpInfo(e);
        }

        private void dumpInfo(FocusEvent e) {
            System.out.println("Source  : " + name(e.getComponent()));
            System.out.println("Opposite : " + name(e.getOppositeComponent()));
            System.out.println("Temporary: " + e.isTemporary());
            final Component c = e.getComponent();//works for editable JComboBox too
            if (c instanceof JFormattedTextField) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        ((JFormattedTextField) c).setText(((JFormattedTextField) c).getText());
                        ((JFormattedTextField) c).selectAll();
                    }
                });
            } else if (c instanceof JTextField) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        ((JTextField) c).setText(((JTextField) c).getText());
                        ((JTextField) c).selectAll();
                    }
                });
            }
        }

        private String name(Component c) {
            return (c == null) ? null : c.getName();
        }
    };
}


来源:https://stackoverflow.com/questions/20971050/jspinner-autoselect-onfocus

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