HOWTO 当 JSpinner 获得焦点时选择 JSpinner 日期字段 [英] HOWTO Select JSpinner date field when JSpinner gets focus

查看:36
本文介绍了HOWTO 当 JSpinner 获得焦点时选择 JSpinner 日期字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 JSpinner(包含一个分钟/秒计时器)为一群不关心鼠标但希望每次击键都完全按照他们的方式设置的人工作曾经.它几乎按我的意愿工作,但最后一英里是最难的.

I am trying to make a JSpinner (containing a minute/second timer) work for a bunch of set-in-their-ways folks who don't care beans about the mouse but want every keystroke to do exactly what they are used to. It works almost as I'd like it to but the last mile is the hardest.

这在实践中意味着,当其文本字段之一获得焦点时,微调器的外观和行为应该与用户从这些字段之一按下箭头键后的外观和行为完全相同.

What this means in practice is that the spinner should look and behave when one of its text fields gains focus exactly as it looks and behaves after the user has pressed an arrow key from one of those fields.

Spinner 的创建方式如下:

Spinner is created as follows:

this.countdown = new JSpinner(this.model);
this.editor = new JSpinner.DateEditor(countdown, "m:ss");
JFormattedTextField textField = editor.getTextField();

第 1 步.当我的微调器出现时,它看起来像这样:('|'表示脱字符,粗体表示选择)

step 1. When my spinner comes up it looks like this: ('|' indicates the caret, bold indicates the selction)

|1:00
(什么都没有选择)

|1:00
(Nothing is selected)

第 2 步.如果从这里按下向上箭头它看起来像这样:

step 2. If Up Arrow is pressed from here It looks like this:

2|:00
(选择了 2 分钟字段)

2|:00
(2 in minutes field is selected)

第 3 步.如果从这里按下右箭头,我们会得到:

step 3. If Right Arrow is pressed from here we get:

2:|00
(什么都没有选择)

2:|00
(Nothing is selected)

第 4 步.如果从这里按下向上箭头,我们得到

step 4. If Up Arrow is pressed from here we get

2:01|
(01 in seconds 字段被选中)

2:01|
(01 in seconds field is selected)

我希望它在所有这些情况下都能像在第 2 步和第 4 步中那样工作.当其中一个子字段获得焦点时,应该选择它.

I'd like it to work in all these cases as it does in steps 2 and 4. When one of the subfields gains focus, it should be selected.

有什么办法可以做到这一点吗?

Is there any way to make this happen?

推荐答案

也许你可以使用 FocusListener.

Maybe you can use a FocusListener.

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

public class SpinnerFocusTest {
  private static final String PATTERN = "m:ss";
  public JComponent makeUI() {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(60*1000);
    Date value = c.getTime();

    SpinnerDateModel m = new SpinnerDateModel(
        value, null, null, Calendar.MINUTE);

    JSpinner sp1 = new JSpinner(m);
    sp1.setEditor(new JSpinner.DateEditor(sp1, PATTERN));

    JSpinner sp2 = new JSpinner(m);
    final JSpinner.DateEditor editor =
      new JSpinner.DateEditor(sp2, PATTERN);
    sp2.setEditor(editor);
    editor.getTextField().addFocusListener(new FocusAdapter() {
      @Override public void focusGained(FocusEvent e) {
        EventQueue.invokeLater(new Runnable() {
          @Override public void run() {
            JTextField f = editor.getTextField();
            int i = f.getText().lastIndexOf(":");
            f.select(i+1, i+3);
          }
        });
      }
    });

    JPanel p = new JPanel(new GridLayout(2,1,5,5));
    p.add(sp1);
    p.add(sp2);
    JPanel panel = new JPanel(new BorderLayout());
    panel.add(p, BorderLayout.NORTH);
    panel.setBorder(BorderFactory.createEmptyBorder(8,8,8,8));
    return panel;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame("");
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new SpinnerFocusTest().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

这篇关于HOWTO 当 JSpinner 获得焦点时选择 JSpinner 日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆