简体   繁体   English

如何使JSpinner对象的全部内容突出显示,以便在获得焦点时不需要删除用户输入?

[英]How can I make a JSpinner object have its entire contents highlighted so that user input does not require deleting when focus is gained?

Not much to add to the Title question. 没有太多要添加到标题问题。

Here's what tabbing to the first box looks like, with cursor before the first character in the field, so that the user will have to delete the character if he wishes to enter his own month, day, or year number: 这是跳到第一个框的样子,将光标放在该字段的第一个字符之前,以便用户如果希望输入自己的月,日或年,则必须删除该字符:

在此处输入图片说明

Here's what I'd like, when the field is tabbed to (or otherwise selected), so the user doesn't have to delete the character(s) presented if he wishes to enter his own year, etc.: 这是我想要的,当将该字段制表到(或以其他方式选中)时,这样,如果用户希望输入自己的年份,则不必删除显示的字符,等等:

在此处输入图片说明

I can accomplish this for a JTextField like so, for example: 我可以为JTextField这样实现,例如:

txtDateFrom.select(0,99);

But .select() isn't a method for JSpinner . 但是.select()不是JSpinner的方法。

(I realize that this raises the question, "Why use a spinner?" but the obvious answer is that I'd like both methods of selecting to be available, as is common in design.) (我意识到这提出了一个问题,“为什么使用微调器?”,但显而易见的答案是,我希望两种选择方法都可以使用,这在设计中很常见。)

(A much less pressing but related matter... I made an integer array of 100 year-dates [eg, 2014] named years and used SpinnerListModel(years) because when using the SpinnerNumberModel , a year would be displayed as 2,014. I can live with what I've done, but is there a less-brute-force way? There's no method containing "format" that I could find for this method.) (不那么紧迫,但相关的问题……我制作了一个由100年日期[例如2014]组成的整数数组,命名为years并使用了SpinnerListModel(years)因为使用SpinnerNumberModel ,年份将显示为2,014。我可以与我所做的工作一起使用,但是有没有那么简单的方法?没有可以找到的包含“格式”的方法。)

This works in Java 1.7.0_51, in Windows and Linux. 这适用于Windows和Linux中的Java 1.7.0_51。 I don't have the ability to test it in OS X. 我没有能力在OS X中对其进行测试。

JSpinner.DefaultEditor editor =
    (JSpinner.DefaultEditor) spinner.getEditor();

editor.getTextField().addFocusListener(new FocusAdapter() {
    @Override
    public void focusGained(FocusEvent event) {
        final JTextField textField = (JTextField) event.getComponent();
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        textField.selectAll();
                    }
                });
            }
        });
    }
});

Side note: Have you considered replacing your three JSpinners with a single JSpinner like this? 旁注:您是否考虑过用单个JSpinner替换三个JSpinner?

JSpinner spinner = new JSpinner(new SpinnerDateModel());
spinner.setEditor(new JSpinner.DateEditor(spinner, "MM/dd/yyyy"));

The up/down arrow buttons (and arrow keys) will change whichever field contains the text cursor. 向上/向下箭头按钮(和箭头键)将更改包含文本光标的任何字段。

It won't solve your focus issue, but you may decide that the issue is less of an issue. 它不能解决您的重点问题,但您可以确定问题不大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM