本文介绍了JSpinner:自动选择onFocus的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户通过表单中的JTextFields和JSpinners选项卡时实现自动选择。为此,我正在使用这个Listener:
$ b $ pre $ public class AutoSelect实现了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){
//
}
}

只有它不适合我的纺纱工。事件发生了,正确的线路被执行,只有什么都没有发生。我怀疑我没有正确使用.getTextField()。有没有人有这个工作的解决方案?

解决方案


  • tf.selectAll(); 应该包含到 invokeLater 中,所有内容都是
    Focus 是非常异步的(在Oracle教程中有更多的介绍如何使用Focus,FocusSubsystem ),

  • invokeLater (对于所有 JComponents ,但默认为
    , c> JTextComponents 将这个事件移动到队列的末尾,
    对我来说是相当正确的





  •   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();
    //或JTextField tf =((JSpinner.DefaultEditor)spinner.getEditor())。getTextField();
    format.addFocusListener(fcsListener);
    //或tf.addFocusListener(fcsListener); //取决于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);

    $ b $ 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组件c = e.getComponent(); //可编辑JComboBox也可用
    if(c instanceof JFormattedTextField){
    SwingUtilities.invokeLater(new Runnable(){
    @重写
    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();
    }
    };
    }


    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 ?

    解决方案

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

    • then invokeLater (not true in all cases for all JComponents, butby 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();
            }
        };
    }
    

    这篇关于JSpinner:自动选择onFocus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    10-12 03:44