Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7827987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:07:17+00:00 2026-06-02T10:07:17+00:00

I have a JCombobox in my code. I have added the FocusLost event .

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:07:19+00:00Added an answer on June 2, 2026 at 10:07 am
    • 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();
            }
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little problem. I had some JComboBox to a JDialog but they
In my code I have 2 ComboBox DropDownLists, created by the code below. The
I have a combobox that is bound to an enum using the following code:
How far is the code for best functionallity? I have two ComboBox, so the
I have a JComboBox and would like to have a separator in the list
I have a JTable with two columns and both are JComboBox, for this purpose
I have a JComboBox on my Panel. One of the popup menu items is
I did the following code. I know it's horribly written, but it's only a
First of all, I looked up this related question in here but the solution
I have this code: public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED) { int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.