java - 如何在组合框 Action 监听器中仅包含用户操作?

标签 java actionlistener

我有一个 JCombobox,它在运行时填充,并根据表单上的其他操作重新填充。问题是我只希望 actionListener 由用户鼠标/键盘点击触发。不幸的是,actionListener 是由组合框的编程清除/加载触发的。

我尝试规避此问题是检查控件是否具有焦点,这将是用户可以直接操作它的唯一方法,并且当其他控件清除/加载其内容时永远不会出现这种情况。不幸的是,hasFocus()部分始终返回 null。

这是我的表单的精简示例,仅包含问题:

package newProj;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JTabbedPane;

public class hasFocusTest {

    private JFrame frame;
    public static String DBurl; 
    private JComboBox<?> cmbEquipSpec;

    public static void main(String[] args) {
        hasFocusTest window = new hasFocusTest();
        window.frame.setVisible(true);
    }

    public hasFocusTest() {
        initialize();
    }

    public static void populateSpec(DefaultComboBoxModel<String> Speclist) {

        Speclist.removeAllElements();
        Speclist.addElement("1");
        Speclist.addElement("2");
        Speclist.addElement("3");
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 280, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        this.frame.setFocusable(false);

        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        tabbedPane.setBounds(10, 11, 240, 140);
        frame.getContentPane().add(tabbedPane);

        JLayeredPane p_1 = new JLayeredPane();
        tabbedPane.addTab("Define Function", null, p_1, null);

        DefaultComboBoxModel<String> Speclist = new DefaultComboBoxModel<String>();
        populateSpec(Speclist);

        cmbEquipSpec = new JComboBox<String>(Speclist);
        cmbEquipSpec.addActionListener(new ActionListener()  {
            public void actionPerformed(ActionEvent arg0) {

                boolean sFocus = false;

                try {
                        sFocus = cmbEquipSpec.hasFocus();
                    } catch (NullPointerException e) {

                }

                System.out.println(sFocus);
                System.out.println("cmbEquipSpec: " + cmbEquipSpec);
            }
        });
        cmbEquipSpec.setEditable(true);
        cmbEquipSpec.setBounds(10, 31, 64, 20);
        p_1.add(cmbEquipSpec);

        JLabel lblEquipSpec = new JLabel("Equip Spec");
        lblEquipSpec.setBounds(15, 11, 74, 14);
        p_1.add(lblEquipSpec);
        p_1.setVisible(true);
    }
}

如果我将中间部分更改为:

    Component sFocus = null;

try {
        sFocus = frame.getFocusOwner();
    } catch (NullPointerException e) {

}

然后它告诉我,获得焦点的组件与被单击的组件明显不同。

单击的组件: javax.swing.JComboBox[,10,31,64x20,invalid,layout=javax.swing.plaf.metal.MetalComboBoxUI$MetalComboBoxLayoutManager,alignmentX=0.0,alignmentY=0.0,border=,flags=4194632,maximumSize=,minimumSize=,preferredSize=,isEditable=true,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=3]

具有焦点的组件: javax.swing.plaf.metal.MetalComboBoxEditor$1[,0,0,44x20,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.metal.MetalComboBoxEditor$EditorBorder@1d04923d,flags=8388904,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=9,columnWidth=0,command=,horizontalAlignment=LEADING]

我对此很陌生,为什么我单击的组合框不是焦点,这对我来说毫无意义。但是,重申一下,这是我对解决方案的尝试。如果有一种方法可以从 actionListener 中排除程序操作,那么这也可以实现我的目标。我相信。

最佳答案

Unfortunately, the actionListener is triggered by the programatic clearing/loading of the combobox.

  1. 从组合框中删除 ActionListener
  2. 清除并重新加载组合框的项目
  3. 将 ActionListener 添加回组合框

关于java - 如何在组合框 Action 监听器中仅包含用户操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32752746/

相关文章:

java - FileObserver 无法在 Android 6 上运行,可替代在 Android 上检测屏幕截图

java - 如何让方法返回多个对象?

Java,LDAP : Make it not ignore blank passwords?

java - OSGi Bundle 状态不是 Active(CQ5 Maven 项目)为什么?

java - jButton1ActionPerformed 和 jButton2ActionPerformed 如何共享 BufferedReader/BufferedWriter?

java - Java 中的 source == 按钮

java - ClassCastException 与 JAXB - Websphere jar 与 applicationl jar

java - 如何让 JCheckBox 按我想要的方式工作

java - 从另一个类获取 jtextfield 的值到 actionperformed 方法

jsf-2 - 为什么 commandButton actionListener 不起作用?