Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

A simple question about using the JCombobox

my code has a JCombobox and is editable. When I type text and press RUTUNE, it will print out what I've typed in. But my problemis: ut print out two times, not just one time. How can I get it just print out one time?

My code looks as follow:

...
...
  jComboBoxCommand.addActionListener(new java.awt.event.ActionListener() {
  public void actionPerformed(java.awt.event.ActionEvent e) {    
   System.out.println(jComboBoxCommand.getSelectedItem());                         }
  });
...
...
0
chenwei
Asked:
chenwei
  • 2
1 Solution
 
zzynxSoftware engineerCommented:
Try:

jComboBoxCommand.addActionListener(new java.awt.event.ActionListener() {
  public void actionPerformed(java.awt.event.ActionEvent e) {    
    // Avoid being triggered twice: for comboBoxChanged & comboBoxEdited:
    if ( !e.getActionCommand().equals("comboBoxChanged") ) return;
    System.out.println(jComboBoxCommand.getSelectedItem());                     }
  });
0
 
zzynxSoftware engineerCommented:
Thanks :)
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now