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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:36:34+00:00 2026-06-13T10:36:34+00:00

I want to add elements in JList from an user-defined function which is not

  • 0

I want to add elements in JList from an user-defined function which is not any event listener.

I have JList as :

DefaultListModel listModel = new DefaultListModel();
JList jList1 = new JList(listModel);
listModel.add(0,"Welcome");
listModel.addListDataListener(new MyListDataListener());

class MyListDataListener implements ListDataListener {
        public void contentsChanged(ListDataEvent e) {
             //DefaultListModel model = (DefaultListModel)e.getSource();
            //jList1.setModel(listModel);
            System.out.println("inside content");
        }
        public void intervalAdded(ListDataEvent e) {
           // jList1.setModel(listModel);
            jList1.setListData(a);
            //System.out.println("inside added"+e.getSource()+" "+jList1.getModel());

        }
        public void intervalRemoved(ListDataEvent e) {

        }
    }

As, intervalAdded is called whenever I want to add elements from a mouseclicked listener,as

private void sendButtonMouseClicked(java.awt.event.MouseEvent evt) {                                        
        // TODO add your handling code here:
        if(!inputField.getText().isEmpty())
        {
            message=myName+" : "+inputField.getText();


        listModel.add(GlobalVariable.i,message);


        inputField.setText(null);
        GlobalVariable.i++;
        }

    }

and also from my user-defined function,that is

public void messagereceived(String s)
    {
                 listModel.add(GlobalVariable.i,message);
                 GlobalVariable.i++;
}

But the listModel.setmodel() does not work whenever it called from the messagereceived() function.

How do I add element in JList from this function?

  • 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-13T10:36:35+00:00Added an answer on June 13, 2026 at 10:36 am
    1. In messagereceived, you are not calling listModel.setmodel() but listModel.add(GlobalVariable.i,message);
    2. When you call jList1.setListData(a); you are implicitly creating and setting a new ListModel on jList1. Therefore, when you modify listModel, it does not reflect on your jList1.
    3. GlobalVariable–>looks like the sign of some ancient C code-style. Use objects appropriately instead.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add elements to my Hash lists, which can have more than
i want to add listener to a jlist. but items are added dynamically to
add unique Elements to List of User defined class (i.e. List ) public class
I want to add some JSeparators to a JList. I am using a DefaultListModel,
I have an arraylist where I want to add elements via a for loop.
Suppose I have an iterator, and I want to add some elements before or
I want to add elements to arrays within an object that I access like
HI.. i want to add elements dynamically to listview in QT for symbian OS,
I want to add database elements into array. how do I do that. the
This is my array and I want to add some elements to make it

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.