Caret
并且selectAll
最好包装成invokeLater
,但是对于JTextComponent
(您也可以从中派生JComboBox
)您必须决定是否要使用
或使用
编辑
1) 可编辑JComboBox
Runnable doRun = new Runnable() {
@Override
public void run() {
myComboBox.getEditor().setItem(0);
myComboBox.getEditor().selectAll();
myComboBox.requestFocus();
}
};
SwingUtilities.invokeLater(doRun);
2) 或派生JTextField
或JFormattedTextField
源自可编辑JComboBox
((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:
SSCCE 用于可编辑的 JComboBox,
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);