栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

聚焦时使JSpinner选择文本

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

聚焦时使JSpinner选择文本

您面临的许多问题与在焦点事件(以及其他一些状态事件)发生后,旋转器如何验证旋转器中的任何值有关,这将避免突出显示。

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(); }         } }}

Now, right about now, I’m praying for some really nice, simple, undocumented
property that we can set that will mean we don’t need to do all this :P



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/483712.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号