gpt4 book ai didi

java - JSpinner:自动选择 onFocus

转载 作者:行者123 更新时间:2023-11-30 07:11:39 27 4
gpt4 key购买 nike

我想在用户通过我的表单中的 JTextFields 和 JSpinners 切换时实现自动选择。为此,我正在使用这个监听器:

public class AutoSelect implements FocusListener {

@Override
public void focusGained(final FocusEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (e.getSource() instanceof JTextField) {
try {
JTextField t = (JTextField) e.getComponent();
t.selectAll();
} catch (ClassCastException ex) {
//
}
}else if (e.getSource() instanceof JSpinner){
JSpinner spinner = (JSpinner)e.getComponent();
JTextField tf = ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField();
tf.selectAll();
}
}
});
}

@Override
public void focusLost(FocusEvent e) {
//
}
}

只是它对我的微调器不起作用。事件被触发,正确的行被执行,但什么也没有发生。我怀疑我没有正确使用 .getTextField() 。有人对此有可行的解决方案吗?

最佳答案

  • tf.selectAll(); 应该被包装到 invokeLater 中,一切都包含Focus 是相当异步的(更多内容在 Oracle 教程 How to use Focus, FocusSubsystem),

  • 然后 invokeLater(并非在所有情况下都适用于所有 JComponents,但是默认情况下)对于 JTextComponents 将此事件移动到队列的末尾,非常适合我

enter image description here

enter image description here

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

public class ComboBoxTwo extends JFrame {

private static final long serialVersionUID = 1L;
private JTextField text = new JTextField(15);
private JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 15, 1));

public ComboBoxTwo() {
text.setText("Something selectable");
text.addFocusListener(fcsListener);
JFormattedTextField format = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
// or JTextField tf = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
format.addFocusListener(fcsListener);
//or tf.addFocusListener(fcsListener); // depends of type for SpinnerXxxModel
add(text, BorderLayout.NORTH);
add(spinner, BorderLayout.SOUTH);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new ComboBoxTwo();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
//
private FocusListener fcsListener = new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
dumpInfo(e);
}

@Override
public void focusLost(FocusEvent e) {
dumpInfo(e);
}

private void dumpInfo(FocusEvent e) {
System.out.println("Source : " + name(e.getComponent()));
System.out.println("Opposite : " + name(e.getOppositeComponent()));
System.out.println("Temporary: " + e.isTemporary());
final Component c = e.getComponent();//works for editable JComboBox too
if (c instanceof JFormattedTextField) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
((JFormattedTextField) c).setText(((JFormattedTextField) c).getText());
((JFormattedTextField) c).selectAll();
}
});
} else if (c instanceof JTextField) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
((JTextField) c).setText(((JTextField) c).getText());
((JTextField) c).selectAll();
}
});
}
}

private String name(Component c) {
return (c == null) ? null : c.getName();
}
};
}

关于java - JSpinner:自动选择 onFocus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20971050/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com