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

  • SEARCH
  • Home
  • 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 7969503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:14:48+00:00 2026-06-04T07:14:48+00:00

I´m trying to validate dynamically an item selected by a JComboBox, and I want

  • 0

I´m trying to validate dynamically an item selected by a JComboBox, and I want to cancel the selection change in case the validation is not correct. Is there any way to achieve it?

private ItemListener itemListener = new ItemListener() {
    @Override                                            
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
            if (true) CANCEL_CHANGE;
        }
    }
};

I tried to define a var holding the old value, unregistering listener, and select to previous state manually, but then the problem comes with the first change, because the var is not initialized and there´s no way hold the original value.

I also tried using ActionListener, but found no way to programtically cancel the change, and I don´t need fire event then there´s no change but I am assessing the chance of setSelection manually, so I revert to ItemListener.

  • 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-04T07:14:49+00:00Added an answer on June 4, 2026 at 7:14 am

    I can do that, dunno why you cannot do it. Have a look at this code example, select three times any values, then on the fourth time it will revert back to empty string :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ComboTest {
    
        private JLabel imageLabel;
        private JComboBox comboImage;
    
        private String[] names = {"", "ukIcon","caIcon","unknwon"};
        private boolean flag;
        private int counter;
    
        public ComboTest(){
            flag = false;
            counter = 0;
            initComponents();
        }
    
        public void initComponents(){
            JFrame frame = new JFrame("Test Combo");        
            frame.setSize(320, 160);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new FlowLayout());
    
            comboImage = new JComboBox(names);
            comboImage.addItemListener(new ItemListener(){
                public void itemStateChanged(ItemEvent event){
                    if(event.getStateChange() == ItemEvent.SELECTED){
                        if (flag)
                            comboImage.setSelectedItem("");
                        else
                        {
                            counter++;
                            if (counter == 3)
                                flag = true;
                            System.out.println((String) comboImage.getSelectedItem());
                        }   
                    }
                }
            });
    
            frame.add(comboImage);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            javax.swing.SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new ComboTest();
                }
            });
        }
    }
    

    Code with Previous Value

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ComboTest {
    
        private JLabel imageLabel;
        private JComboBox comboImage;
    
        private String[] names = {"", "ukIcon","caIcon","unknwon"};
        private boolean flag;
        private int counter;
        private String previousValue;
    
        public ComboTest(){
            flag = false;
            counter = 0;
            previousValue = "";
            initComponents();
        }
    
        public void initComponents(){
            JFrame frame = new JFrame("Test Combo");        
            frame.setSize(320, 160);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new FlowLayout());
    
            comboImage = new JComboBox(names);
            comboImage.addItemListener(new ItemListener(){
                public void itemStateChanged(ItemEvent event){
                    if(event.getStateChange() == ItemEvent.SELECTED){
                        if (flag)
                            comboImage.setSelectedItem(previousValue);
                        else
                        {
                            counter++;
                            if (counter == 3)
                                flag = true;
                            previousValue = (String) comboImage.getSelectedItem();  
                            System.out.println((String) comboImage.getSelectedItem());
                        }   
                    }
                }
            });
    
            frame.add(comboImage);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            javax.swing.SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new ComboTest();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to change content dynamically based on a JRadioButton selection... My simplified code
I was trying to validate an XML signature. The validation according to this tutorial
I'm trying to validate a input field with the JQuery validation plugin. The numeric
I'm trying to dynamically add a validation rule to some dynamic controls: $(input[id*=Hours]).rules(add, required);
I'm trying to add a validation rule to multiple textboxes dynamically. Here's the js:
I'm trying to add a jQuery validator rule dynamically and it's not working. It
I'm trying to change a JList inside a JScrollPane dynamically, using myList.setListData(someArray); After this
I have a form dynamically created using Jquery and i am trying to validate
I'm trying to validate a user's windows credentials on a computer that's not joined
I am trying to validate the WPF form against an object. The validation fires

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.