3

首先:抱歉,我无法提供 SSCCE。我试图在一个小项目上重新创建这个问题,但没有成功,或者我应该说成功,因为它在那里工作!

所以这是我的问题:我有一个可编辑的 JCombobox,它应该通过单击编辑器组件来监听焦点事件。但事实并非如此。

这是我附加侦听器的代码片段:

cmbZoom.setToolTipText(locale.getString("GUI_ZoomFactor"));
cmbZoom.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
cmbZoom.setPreferredSize(new Dimension(88, 29));
cmbZoom.setEditable(true);
((JTextField)cmbZoom.getEditor().getEditorComponent()).setHorizontalAlignment(JTextField.CENTER);
cmbZoom.getEditor().getEditorComponent().addFocusListener(
  new FocusListener(){
    public void focusGained(FocusEvent arg0) {
      System.out.println("GAINED");
    }

    public void focusLost(FocusEvent arg0) {
      System.out.println("LOST");
    }
  });

我已经尝试删除组合框上的所有其他侦听器,删除观察者模式,使组合框受到保护并将侦听器附加到每个孩子,...

我还尝试重新实现它,就像在这篇文章中所写的那样: JCombobox focusLost 没有触发 - 为什么会这样?

有没有调试事件的好方法?

整个项目可以在 Github 上查看: https ://github.com/nexxx/Database-Analyzer

工具栏类可以在这里找到(代码从第 98 行开始): https ://github.com/nexxx/Database-Analyzer/blob/master/src/dba/gui/auxClasses/toolBars/ToolBar.java

4

2 回答 2

3

相同的代码在某些情况下会导致问题,但在其他情况下不会导致问题,这一事实使我怀疑您的代码可能没有在AWT 事件调度线程上执行。

于 2013-03-21T11:25:49.353 回答
1
  • 我建议不要混合FocusListener添加Item & DocumentListenerJComboBox'es Editor, 可以创建无限循环

例如,

import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Vector;
import javax.swing.*;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.MetalComboBoxButton;

public class MyComboBox {

    private Vector<String> listSomeString = new Vector<String>();
    private JComboBox someComboBox = new JComboBox(listSomeString);
    private JComboBox editableComboBox = new JComboBox(listSomeString);
    private JComboBox non_EditableComboBox = new JComboBox(listSomeString);
    private JFrame frame;

    public MyComboBox() {
        listSomeString.add("-");
        listSomeString.add("Snowboarding");
        listSomeString.add("Rowing");
        listSomeString.add("Knitting");
        listSomeString.add("Speed reading");
//
        someComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        someComboBox.setFont(new Font("Serif", Font.BOLD, 16));
        someComboBox.setEditable(true);
        someComboBox.getEditor().getEditorComponent().setBackground(Color.YELLOW);
        ((JTextField) someComboBox.getEditor().getEditorComponent()).setBackground(Color.YELLOW);
        someComboBox.getEditor().getEditorComponent().addFocusListener(fcsListener);
//
        editableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        editableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
        editableComboBox.setEditable(true);
        JTextField text = ((JTextField) editableComboBox.getEditor().getEditorComponent());
        text.setBackground(Color.YELLOW);
        JComboBox coloredArrowsCombo = editableComboBox;
        Component[] comp = coloredArrowsCombo.getComponents();
        for (int i = 0; i < comp.length; i++) {
            if (comp[i] instanceof MetalComboBoxButton) {
                MetalComboBoxButton coloredArrowsButton = (MetalComboBoxButton) comp[i];
                coloredArrowsButton.setBackground(null);
                break;
            }
        }
        editableComboBox.getEditor().getEditorComponent().addFocusListener(fcsListener);
//
        non_EditableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        non_EditableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
        non_EditableComboBox.addFocusListener(fcsListener);
//
        frame = new JFrame();
        frame.setLayout(new GridLayout(0, 1, 10, 10));
        frame.add(someComboBox);
        frame.add(editableComboBox);
        frame.add(non_EditableComboBox);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100, 100);
        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) {
            final Component c = e.getComponent();
            System.out.println("Source  : " + name(e.getComponent()));
            System.out.println("Opposite : " + name(e.getOppositeComponent()));
            System.out.println("Temporary: " + e.isTemporary());
            if (c instanceof JFormattedTextField) {//works for editable JComboBox too
                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();
                    }
                });
            } else if (c instanceof JTextField) {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        ((JComboBox) c).getEditor().selectAll();
                    }
                });
            }
        }

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

    public static void main(String[] args) {
        UIManager.put("ComboBox.background", new ColorUIResource(Color.yellow));
        UIManager.put("JTextField.background", new ColorUIResource(Color.yellow));
        UIManager.put("ComboBox.selectionBackground", new ColorUIResource(Color.magenta));
        UIManager.put("ComboBox.selectionForeground", new ColorUIResource(Color.blue));
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                MyComboBox aCTF = new MyComboBox();
            }
        });
    }
}
于 2013-03-21T08:34:47.137 回答