JAVA中jspinner设置选中内容_java – 使JSpinner在聚焦时选择文本

您遇到的大部分问题都与微调器在聚焦事件(以及其他几个状态事件)之后验证微调器内的任何值有关,这将绕过突出显示.

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 lstChildren = findAllChildren(spinner, JTextComponent.class);

if (lstChildren != null && lstChildren.size() > 0) {

JTextComponent editor = lstChildren.get(0);

editor.addFocusListener(SHARED_INSTANCE);

}

}

}

public static List findAllChildren(JComponent component, Class clazz) {

List lstChildren = new ArrayList(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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值