Java – is there any way to prevent the action listener from triggering when calling setselecteditem()?

I have a program that contains multiple combo boxes. Each combo box has its own action listener Selecting an item from any combo box will change the items in one or more other combo boxes The problem I encountered is that calling setselecteditem() for one combo box will trigger the action listener of another combo box, and the combo box will trigger the action listener of other combo boxes, etc

Is there any way to avoid this, either by allowing the action listener to be triggered only from user input, or by detecting that the action is not triggered from user input? Assuming you don't use setselecteditem () is not an option, because I want the program to be able to com@R_985_2419 @@R_ 985_ 2419 @ set the currently selected item Thank you for your help

Solution

for example

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class Combo@R_985_2419@Two extends JFrame implements ActionListener,ItemListener {

    private static final long serialVersionUID = 1L;
    private JCombo@R_985_2419@ mainCombo@R_985_2419@;
    private JCombo@R_985_2419@ subCombo@R_985_2419@;
    private Hashtable<Object,Object> subItems = new Hashtable<Object,Object>();

    public Combo@R_985_2419@Two() {
        String[] items = {"Select Item","Color","Shape","Fruit"};
        mainCombo@R_985_2419@ = new JCombo@R_985_2419@(items);
        mainCombo@R_985_2419@.addActionListener(this);
        mainCombo@R_985_2419@.addItemListener(this);
        //prevent action events from being fired when the up/down arrow keys are used
        //mainCombo@R_985_2419@.putClientProperty("JCombo@R_985_2419@.isTableCellEditor",Boolean.TRUE);
        getContentPane().add(mainCombo@R_985_2419@,BorderLayout.WEST);
        subCombo@R_985_2419@ = new JCombo@R_985_2419@();//  Create sub combo @R_985_2419@ with multiple models
        subCombo@R_985_2419@.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
        subCombo@R_985_2419@.addItemListener(this);
        getContentPane().add(subCombo@R_985_2419@,BorderLayout.EAST);
        String[] subItems1 = {"Select Color","Red","Blue","Green"};
        subItems.put(items[1],subItems1);
        String[] subItems2 = {"Select Shape","Circle","Square","Triangle"};
        subItems.put(items[2],subItems2);
        String[] subItems3 = {"Select Fruit","Apple","Orange","Banana"};
        subItems.put(items[3],subItems3);
//      mainCombo@R_985_2419@.setSelectedIndex(1);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String item = (String) mainCombo@R_985_2419@.getSelectedItem();
        Object o = subItems.get(item);
        if (o == null) {
            subCombo@R_985_2419@.setModel(new DefaultCombo@R_985_2419@Model());
        } else {
            subCombo@R_985_2419@.setModel(new DefaultCombo@R_985_2419@Model((String[]) o));
        }
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
            if (e.getSource() == mainCombo@R_985_2419@) {
                if (mainCombo@R_985_2419@.getSelectedIndex() != 0) {
                    FirstDialog firstDialog = new FirstDialog(Combo@R_985_2419@Two.this,mainCombo@R_985_2419@.getSelectedItem().toString(),"Please wait,Searching for ..... ");
                }
            } 
        }
    }

    private class FirstDialog extends JDialog {

        private static final long serialVersionUID = 1L;

        FirstDialog(final Frame parent,String winTitle,String msgString) {
            super(parent,winTitle);
            setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
            JLabel myLabel = new JLabel(msgString);
            JButton bNext = new JButton("Stop Processes");
            add(myLabel,BorderLayout.CENTER);
            add(bNext,BorderLayout.soUTH);
            bNext.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    setVisible(false);
                }
            });
            javax.swing.Timer t = new javax.swing.Timer(1000,new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                }
            });
            t.setRepeats(false);
            t.start();
            setLocationRelativeTo(parent);
            setSize(new Dimension(400,100));
            setVisible(true);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new Combo@R_985_2419@Two();
        frame.setDefaultCloSEOperation(EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>