Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使JSpinner在聚焦时选择文本_Java_Swing_Jspinner_Focuslistener - Fatal编程技术网

Java 使JSpinner在聚焦时选择文本

Java 使JSpinner在聚焦时选择文本,java,swing,jspinner,focuslistener,Java,Swing,Jspinner,Focuslistener,我想更改JSpinner的行为,这样当您单击文本时,它就会选择它。这样可以更容易地用所需的值替换字段。不幸的是,我无法让行为正常工作,相反,它只是在文本中插入光标,而没有选择已经存在的内容 我曾尝试通过((DefaultEditor)this.getEditor()).getTextField(),将焦点侦听器添加到JSpinner本身和文本区域本身,但这两种方法似乎都没有达到预期效果。我的代码(用于JSpinner本身)如下所示: spinner.addFocusListener(new Fo

我想更改JSpinner的行为,这样当您单击文本时,它就会选择它。这样可以更容易地用所需的值替换字段。不幸的是,我无法让行为正常工作,相反,它只是在文本中插入光标,而没有选择已经存在的内容

我曾尝试通过
((DefaultEditor)this.getEditor()).getTextField()
,将焦点侦听器添加到JSpinner本身和文本区域本身,但这两种方法似乎都没有达到预期效果。我的代码(用于JSpinner本身)如下所示:

spinner.addFocusListener(new FocusAdapter(){
            @Override
            public void focusGained(FocusEvent e) {
                ((DefaultEditor) ((JSpinner) e.getSource()).getEditor()).getTextField().selectAll();
            }
        }); 
我不确定是什么问题。如果重要的话,我正在运行MacOS10.7.5和Java6U43


编辑:我在FocusGain方法的开头放了一个
System.out.println
,发现它从未被调用过。因此,关注JSpinner似乎没有注册。再次,我尝试将FocusAddWater同时放在微调器和文本字段上(但不是同时放在文本字段上)。

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

MacOS更糟糕

我最后做的是启动一个
线程
,它等待了很短的时间(大约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();
            }            
        }        
    }
}
导入java.awt.Component;
导入java.awt.EventQueue;
导入java.awt.GridBagLayout;
导入java.awt.event.FocusAdapter;
导入java.awt.event.FocusEvent;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
导入javax.swing.JButton;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
导入javax.swing.JSpinner;
导入javax.swing.SpinnerNumberModel;
导入javax.swing.SwingUtilities;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
导入javax.swing.text.JTextComponent;
公共级自动对焦器{
公共静态void main(字符串[]args){
新的自动对焦器();
}
公共静态最终SelectOnFocusGainedHandler共享_实例=新建SelectOnFocusGainedHandler();
公共自动对焦器(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}捕获(ClassNotFoundException ex){
}catch(实例化异常){
}捕获(非法访问例外){
}捕获(无支持的LookandFeelexception ex){
}
JSpinner spinner=新JSpinner(新SpinnerNumberModel(1,0,100,1));
installFocusListener(微调器);
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的GridBagLayout());
frame.add(微调器);
frame.add(新的JButton(“此处用于测试”);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
public void installFocusListener(JSpinner微调器){
JComponent spinnerEditor=spinner.getEditor();
if(spinnerEditor!=null){
//这是我花了几天的时间,试图使这项工作和
//最终发出嘶嘶声,抓住所有的
//编辑器中包含的JTextComponent组件。。。。
List lstChildren=findAllChildren(微调器,JTextComponent.class);
if(lstChildren!=null&&lstChildren.size()>0){
JTextComponent editor=lstChildren.get(0);
addFocusListener(共享_实例);
}
}
}
公共静态列表findAllChildren(JComponent组件,类clazz){
列表lstChildren=新的ArrayList(5);
对于(组件comp:Component.getComponents()){
if(类别持续时间(补偿)){
添加((T)comp);
}else if(JComponent的组件实例){
lstChildren.addAll(findAllChildren((JComponent)comp,clazz));
}
}
返回集合。不可修改列表(lstChildren);
}
公共静态类SelectOnFocusGainedHandler扩展了FocusAdapter{
@凌驾
获得公共无效焦点(焦点事件e){
组件组件=e.getComponent();
if(JTextComponent的组件实例){
最终JTextComponent text组件=(JTextComponent)组件;
新线程(newrunnable()){
@凌驾
公开募捐{
试一试{
睡眠(25);
}捕获(中断异常例外){
}
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
textComponent.selectAll();
}
});
}
}).start();
}            
}        
}
}

现在,就在此时此刻,我祈祷我们可以设置一些非常好的、简单的、未记录的属性,这意味着我们不需要做所有这些:p不知道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上选择文本。

这对我不起作用,但我做了一点测试,发现FocusGaven从未被调用。也许还有更大的问题?你没有跟踪变量?啊,事实上,问题是我