焦点获得和焦点丢失的事件 [英] Focus Gained and Focus Lost event
问题描述
如果通过JTextField2获得的焦点意味着需要选择JTextField2中的内容。我不知道如何做这个PLZ把你的正确的代码关联这个问题
这可能有点复杂
你必须包装并延迟你的 invokeLater()
,并放在里面(最安全的方法是设置下面的代码行)
$ ul
JTextField2.setText(JTextField2.getText);
/ b>
-
JTextField2.selectAll();
($)
编辑到@Andrew Thompson
$ b
private FocusListener fcsListener = new FocusListener {
@Override
public void focusGained(FocusEvent e){
dumpInfo(e);
}
@Override
public void focusLost(FocusEvent e){
// dumpInfo(e);
$ b $ 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());
Component c = e.getComponent(); //用于编辑JComboBox太多
if(c instanceof JFormattedTextField){
((JFormattedTextField)c).selectAll();
} else if(c instanceof JTextField){
((JTextField)c).selectAll();
} //这两个方法不正确setText(getText())内部invokeLater
私有字符串名称(组件c){
return(c ==空值) ? null:c.getName();
}
};
I have 4 JTextfields in my java swing form. The problem is I need to move the Focus from one JTextField to other through java code not by using tab key.
If the Focus gained by JTextField2 means the content in the JTextField2 need to be selected. I don't know how to do this plz put your proper code associate with this issue
that could be little bit complicated
you have to wrap and delay your Action or ActionListener into invokeLater()
, and put inside (most safiest way is to set there follows code lines)
JTextField2.setText(JTextField2.getText);
and
JTextField2.selectAll();
edit to @Andrew Thompson
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());
Component c = e.getComponent();//works for editable JComboBox too
if (c instanceof JFormattedTextField) {
((JFormattedTextField) c).selectAll();
} else if (c instanceof JTextField) {
((JTextField) c).selectAll();
}//both methods not correct required setText(getText()) inside invokeLater
}
private String name(Component c) {
return (c == null) ? null : c.getName();
}
};
这篇关于焦点获得和焦点丢失的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!