gpt4 book ai didi

java - 选择可编辑的 JComboBox 中的所有文本并设置光标位置

转载 作者:搜寻专家 更新时间:2023-10-31 20:05:09 25 4
gpt4 key购买 nike

public class CursorAtStartFocusListener extends FocusAdapter {

@Override
public void focusGained(java.awt.event.FocusEvent evt) {
Object source = evt.getSource();
if (source instanceof JTextComponent) {
JTextComponent comp = (JTextComponent) source;
comp.setCaretPosition(0);
comp.selectAll();
}
} }

jComboBox.getEditor().getEditorComponent().addFocusListener(new CursorAtStartFocusListener());

正如您从上面的代码中看到的,我想选择可编辑的 JComboBox 中的所有文本并将光标位置设置为开头。但是如果我先写 comp.setCaretPosition(0) 然后 comp.selectAll(),我有问题,文本被选中但光标在文本的末尾,否则如果我先写 comp.selectAll() 然后 comp.setCaretPosition(0 ), 我将光标置于我想要的位置,但未选择文本。知道我该怎么做吗?

最佳答案

CaretselectAll 最好包装到 invokeLater 中,但对于 JTextComponent(您可以从JComboBox 也是)你必须决定是否要使用

  • 选择所有文本 == selectAll()

或使用

  • 选择插入符号(来自文档)

编辑

1) 对于可编辑的JComboBox

Runnable doRun = new Runnable() {

@Override
public void run() {
myComboBox.getEditor().setItem(0);
myComboBox.getEditor().selectAll();
myComboBox.requestFocus();
}
};
SwingUtilities.invokeLater(doRun);

2) 或从可编辑的 JComboBox 派生 JTextFieldJFormattedTextField

((JTextField) myComboBox.getEditor().getEditorComponent())

然后添加 FocusListener,例如

    private FocusListener focsListener = 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();
if (c instanceof JFormattedTextField) {
EventQueue.invokeLater(new Runnable() {

public void run() {
((JFormattedTextField) c).requestFocus();
((JFormattedTextField) c).setText(((JFormattedTextField) c).getText());
((JFormattedTextField) c).selectAll();
}
});
} else if (c instanceof JTextField) {
EventQueue.invokeLater(new Runnable() {

public void run() {
((JTextField) c).requestFocus();
((JTextField) c).setText(((JTextField) c).getText());
((JTextField) c).selectAll();
}
});
}
}

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

编辑 2:

可编辑 JComboBox 的 SSCCE,

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import javax.swing.*;

public class ComboRendererTest {

public ComboRendererTest() {
JComboBox comboBox = new JComboBox();
comboBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXX");
comboBox.addItem(new Double(1));
comboBox.addItem(new Double(2.25));
comboBox.addItem(new Double(3.5));
comboBox.setRenderer(new TwoDecimalRenderer(comboBox.getRenderer()));
comboBox.setEditable(true);

JComboBox comboBox1 = new JComboBox();
comboBox1.setPrototypeDisplayValue("XXXXXXXXXXXXXXXX");
comboBox1.addItem(new Double(1));
comboBox1.addItem(new Double(2.25));
comboBox1.addItem(new Double(3.5));
comboBox1.setRenderer(new TwoDecimalRenderer(comboBox.getRenderer()));
comboBox1.setEditable(true);

JFrame frame = new JFrame();
frame.add(comboBox, BorderLayout.NORTH);
frame.add(comboBox1, BorderLayout.SOUTH);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable() {

public void run() {
ComboRendererTest comboRendererTest = new ComboRendererTest();
}
});
}
}

class TwoDecimalRenderer extends DefaultListCellRenderer {

private ListCellRenderer defaultRenderer;

public TwoDecimalRenderer(ListCellRenderer defaultRenderer) {
this.defaultRenderer = defaultRenderer;
}

@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
Component c = defaultRenderer.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus);
if (c instanceof JLabel) {
c.setBackground(Color.red);
} else {
c.setBackground(Color.red);
c = super.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus);
}
return c;
}
}

编辑 3.

肮脏的黑客可能是

JTextComponent editor = ((JTextField) myComboBox.getEditor().getEditorComponent());
editor.setCaretPosition(getLength());
editor.moveCaretPosition(0);

关于java - 选择可编辑的 JComboBox 中的所有文本并设置光标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9956533/

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