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 7128805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:10:36+00:00 2026-05-28T11:10:36+00:00

I have the following Programm package utests; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.ArrayList; import

  • 0

I have the following Programm

package utests;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Foo extends JFrame {
    private JComboBox combo;

    public static void main(String... args) {
    Foo f = new Foo();
    f.showUI();
    }

    public void showUI() {
    this.setVisible(true);
    JPanel pane = new JPanel();
    this.setContentPane(pane);
    combo = new JComboBox();
    combo.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent arg0) {
        if (arg0.getStateChange() == ItemEvent.SELECTED) {
            System.out.println("You selected " + combo.getSelectedItem());
        }
        }
    });
    refreshBox();

    pane.add(combo);
    this.pack();
    }

    public void refreshBox(){
    combo.removeAllItems();
ArrayList<String> list = new ArrayList<String>();
    list.add("godsf");
    list.add("södlkf");
    list.add("ldsjlkfdsj");

    for (String s : list) {
        combo.addItem(s);
    }
    }
}

The programm will immediatly show you the string

You selected godsf

How can i check if the user checked something and not the program?

edit: i changed the programm a bit: The problem is that i need to refresh the box some times. Every Time i refresh the data in the box, the ItemListener is hit, and i get wrong results.
How can i check that a Action is done by the User and not by the Programm itself? To add some booleans seems to be not very Java-like…

  • 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-05-28T11:10:37+00:00Added an answer on May 28, 2026 at 11:10 am

    Change the code of method refreshBox to code below:

    public void refreshBox()
    {
        combo.removeAllItems();
        ArrayList<String> list = new ArrayList<String>();
        list.add("godsf");
        list.add("södlkf");
        list.add("ldsjlkfdsj");
    
        for (String s : list) 
        {
            combo.insertItemAt(s, 0);
        }
    }
    

    does it meet your requirement?
    I just change the code:

    combo.addItem(s);
    

    to the code:

    combo.insertItemAt(s, 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Constructor and method not working as expected in Java program I have the following
I have a Java program when I compile it I get the following error
I have the following algorithm implemented in Java which uses TCP/IP: -Client request a
I have defined the following grammar. grammar Sample_1; @header { package a; } @lexer::header
I have created a java project following these steps: 1.File->java project 2. Created the
I have the following package structure where im trying to run a sample hibernate
I have the following Exception: Exception in thread main java.lang.SecurityException: no manifiest section for
i have two programs one in directory /home/redhat/Documents/java1/j1 Demo1.java package j1; public class Demo1
I have the following program: ~/test> cat test.cc int main() { int i =
I have the following program to open lot's of sockets, and hold them open

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.