9

我想更改 JSpinner 的行为,以便当您单击文本时,它会选择它。这使得用您想要的值替换字段变得更容易。不幸的是,我无法让该行为起作用,相反,它只是将光标插入文本中,而不选择已经存在的内容。

我已经尝试通过 向 JSpinner 本身和文本区域本身添加一个焦点监听器((DefaultEditor) this.getEditor()).getTextField(),但这些似乎都没有预期的效果。我的代码(对于 JSpinner 本身)如下:

spinner.addFocusListener(new FocusAdapter(){
            @Override
            public void focusGained(FocusEvent e) {
                ((DefaultEditor) ((JSpinner) e.getSource()).getEditor()).getTextField().selectAll();
            }
        }); 

我不确定问题是什么。如果重要的话,我正在运行 Mac OS 10.7.5 和 Java 6u43。

编辑:我System.out.println在 focusGained 方法的开头放了一个权利,发现它从未被调用过。所以看起来专注于 JSpinner 并没有注册。同样,我尝试将 focusAdpater 放在微调器和文本字段上(虽然不是同时)。

4

3 回答 3

10

不了解 Mac,但我在 Windows 上使用过以下代码:

JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor)spinner.getEditor();
JTextField textField = editor.getTextField();
textField.addFocusListener( new FocusAdapter()
{
    public void focusGained(final FocusEvent e)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                JTextField tf = (JTextField)e.getSource();
                tf.selectAll();
            }
        });

    }

});

我还使用此代码在 JFormattedTextField 上选择文本。

于 2013-03-11T01:15:07.870 回答
8

您面临的大部分问题与微调器如何在焦点事件(和其他几个状态事件)之后验证微调器中的任何值有关,这将绕过高亮。

MacOS 更糟糕。

我最终做的是开始Thread等待很短的时间(大约 25 毫秒),然后用来SwingUtilities.invokeLater实际执行选择......

更新了一个例子

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.JTextComponent;

public class AutoFocusSpinner {

    public static void main(String[] args) {
        new AutoFocusSpinner();
    }

    public static final SelectOnFocusGainedHandler SHARED_INSTANCE = new SelectOnFocusGainedHandler();

    public AutoFocusSpinner() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JSpinner spinner = new JSpinner(new SpinnerNumberModel(1, 0, 100, 1));
                installFocusListener(spinner);

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(spinner);
                frame.add(new JButton("Here for testing"));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public void installFocusListener(JSpinner spinner) {

        JComponent spinnerEditor = spinner.getEditor();

        if (spinnerEditor != null) {

            // This is me spending a few days trying to make this work and 
            // eventually throwing a hissy fit and just grabbing all the 
            // JTextComponent components contained within the editor....
            List<JTextComponent> lstChildren = findAllChildren(spinner, JTextComponent.class);
            if (lstChildren != null && lstChildren.size() > 0) {

                JTextComponent editor = lstChildren.get(0);
                editor.addFocusListener(SHARED_INSTANCE);

            }

        }

    }

    public static <T extends Component> List<T> findAllChildren(JComponent component, Class<T> clazz) {

        List<T> lstChildren = new ArrayList<T>(5);
        for (Component comp : component.getComponents()) {

            if (clazz.isInstance(comp)) {

                lstChildren.add((T) comp);

            } else if (comp instanceof JComponent) {

                lstChildren.addAll(findAllChildren((JComponent) comp, clazz));

            }

        }

        return Collections.unmodifiableList(lstChildren);

    }

    public static class SelectOnFocusGainedHandler extends FocusAdapter {

        @Override
        public void focusGained(FocusEvent e) {

            Component comp = e.getComponent();
            if (comp instanceof JTextComponent) {
                final JTextComponent textComponent = (JTextComponent) comp;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(25);
                        } catch (InterruptedException ex) {
                        }
                        SwingUtilities.invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                textComponent.selectAll();
                            }
                        });
                    }
                }).start();
            }            
        }        
    }
}

现在,现在,我正在祈祷我们可以设置一些非常好的、简单的、未记录的属性,这意味着我们不需要做所有这些:P

于 2013-03-10T22:21:44.700 回答
0

对我来说,@MadProgrammer 和@camickr 的上述解决方案都能够将焦点放在微调器上,但不能放在选定的文本上。我可以增加或减少我的微调器的值。tab 键也对我有用,所以我可以在按 tab 键后选择我的微调器的 JTextField。

为了模拟 Tab 键按下,以下解决方案对我有用。这是我在@MadProgrammer 和@camickr 提供的解决方案之后立即调用的。 使 JSpinner 聚焦时选择文本

于 2022-03-03T13:12:45.060 回答