问题描述
在我的代码中有一个 JCombobox
。我添加了 FocusLost事件
。但它并没有解雇。我已经尝试了很多时间,但没有找到解决方案。
jcbItemType.addFocusListener(new java.awt.event.FocusAdapter ){
public void focusLost(java.awt.event.FocusEvent evt){
jcbItemTypeFocusLost(evt);
}
});
private void jcbItemTypeFocusLost(java.awt.event.FocusEvent evt)
{
// TODO在这里添加处理代码:
System.out.println( name =+((Component)evt.getSource())。getName());
System.out.println(index =+ jcbItemType.getSelectedIndex());
$ b $ p
$ b 但是没有在控制台上打印。请告诉我我做错了什么。
解决方案
-
不适合,与另一个可以创建无限循环(特别是可编辑JComboBox),
/tutorial/uiswing/events/focuslistener.html\">FocusListener 是异步的,有时很难捕捉事件是正确的顺序,特别是在JComponents添加了另一个也是如此
示例如何监听从派生 JTextField / JFormattedTextField
import java.awt。*;
import java.awt.event。*;
import javax.swing。*;
public class ComboBoxTwo extends JFrame implements ItemListener {
private static final long serialVersionUID = 1L;
私人JComboBox mainComboBox;
private JComboBox subComboBox;
$ b $ public ComboBoxTwo(){
String [] items = {Select Item,Color,Shape,Fruit};
String [] subItems1 = {Select Color,Red,Blue,Green};
mainComboBox = new JComboBox(items);
mainComboBox.addItemListener(this);
mainComboBox.addFocusListener(fcsListener);
add(mainComboBox,BorderLayout.WEST);
subComboBox = new JComboBox(subItems1);
subComboBox.setPrototypeDisplayValue(XXXXXXXXXXXXXXXXXXX);
subComboBox.addItemListener(this);
add(subComboBox,BorderLayout.EAST);
b
$ b @Override
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()== ItemEvent.SELECTED){
if (e.getSource()== mainComboBox){
System.out.println(Source:mainComboBox);
} else if(e.getSource()== subComboBox){
System.out.println(Source:subComboBox);
$ b 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.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
//
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());
final Component c = e.getComponent(); //用于可编辑的JComboBox too
if(c instanceof JFormattedTextField){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
((JFormattedTextField)c).selectAll();
}
});
} else if(c instanceof JTextField){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
((JTextField)c).selectAll();
}
});
private String name(Component c){
return(c == null)? null:c.getName();
}
};
}
I have a JCombobox
in my code. I have added the FocusLost event
. But it didn't fired anyway. I have tried lots of time but didn't find solution.
jcbItemType.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
jcbItemTypeFocusLost(evt);
}
});
private void jcbItemTypeFocusLost(java.awt.event.FocusEvent evt)
{
// TODO add your handling code here:
System.out.println("name=" + ((Component) evt.getSource()).getName());
System.out.println("index=" + jcbItemType.getSelectedIndex());
}
But nothing is printed in console. Please suggest me what I am doing wrong.
解决方案 FocusListener isn't proper Listener for JComboBox, altogether with another Listener(s) can creating endless loop (especially Editable JComboBox),
FocusListener is asynchronous, sometimes is too hard to catch events is right orders especially in the cases that JComponents has added another Listener(s) too
example how to listening for Focus from derived JTextField / JFormattedTextField
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboBoxTwo extends JFrame implements ItemListener {
private static final long serialVersionUID = 1L;
private JComboBox mainComboBox;
private JComboBox subComboBox;
public ComboBoxTwo() {
String[] items = {"Select Item", "Color", "Shape", "Fruit"};
String[] subItems1 = {"Select Color", "Red", "Blue", "Green"};
mainComboBox = new JComboBox(items);
mainComboBox.addItemListener(this);
mainComboBox.addFocusListener(fcsListener);
add(mainComboBox, BorderLayout.WEST);
subComboBox = new JComboBox(subItems1);
subComboBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXX");
subComboBox.addItemListener(this);
add(subComboBox, BorderLayout.EAST);
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
if (e.getSource() == mainComboBox) {
System.out.println("Source : mainComboBox");
} else if (e.getSource() == subComboBox) {
System.out.println("Source : subComboBox");
}
}
}
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.setLocationRelativeTo(null);
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).selectAll();
}
});
} else if (c instanceof JTextField) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
((JTextField) c).selectAll();
}
});
}
}
private String name(Component c) {
return (c == null) ? null : c.getName();
}
};
}
这篇关于JCombobox focusLost不是射击 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!