我想改变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两个微调器和文本字段(不是在同一时间虽然)。
多,你所面临的问题与微调如何验证微调中的任何值的焦点事件(和其他几个州的事件),这将规避亮点后做。
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
不知道的是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上选择文本。