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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:29:44+00:00 2026-05-15T21:29:44+00:00

OK so I have a JList and the content is provided with an array.

  • 0

OK so I have a JList and the content is provided with an array. I know how to add elements to an array but I want to know how to refresh a JList… or is it even possible? I tried Google. :\

import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class bs extends JApplet implements MouseListener {

public static String newline;
public static JList list;

    public void init() {



             String[] data = {"one", "two", "three", "four"};
              list = new JList(data);



        this.getContentPane().add(list);

        list.addMouseListener(this);

        String newline = "\n";

        list.setVisible(true);

    }

    public void refresh(){
        Address found;
        this.listModel.clear();

        int numItems = this.getAddressBookSize();
        String[] a = new String[numItems];
        for (int i=0;i<numItems;i++){
            found = (Address)Addresses.get(i);
            a[i] = found.getName();
        }
        /* attempt to sort the array */
        Arrays.sort(a, String.CASE_INSENSITIVE_ORDER);
        for (int i=0;i<numItems;i++) {
            this.listModel.addElement(a[i]);
        }
    }



    public void mousePressed(MouseEvent e) { }

    public void mouseReleased(MouseEvent e) {
        Object index = list.getSelectedValue();
       System.out.println("You clicked on: " + index);
    }

    public void mouseEntered(MouseEvent e) { }

    public void mouseExited(MouseEvent e) { }

    public void mouseClicked(MouseEvent e) { }




    public void paint(Graphics g) {

    }
}

Any ideas?

Thank you.

  • 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-15T21:29:45+00:00Added an answer on May 15, 2026 at 9:29 pm

    One good approach is to create a ListModel to manage the data for you and handle updates.

    Something like:

    DefaultListModel listModel=new DefaultListModel();
    for (int i=0; i<data.length; i++) {
      listModel.addElement(data[i]);
    }
    list=new JList(listModel);
    

    Then you can simply make changes via the list model e.g.

    listModel.addElement("New item");
    listModel.removeElementAt(1); // remove the element at position 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.