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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:21:38+00:00 2026-06-02T21:21:38+00:00

I have a JList that lists an arrayList. When I press the button called

  • 0

I have a JList that lists an arrayList. When I press the button called delete I want to delete the selected element from the list (and ideally the arraylist). At the moment I have the following code, however it is not working. By this I mean that it seems to do nothing if an element is selected and it gives me a ArrayIndexOutOfBoundsException when nothing is selected.

private JList listOrders = new JList(arrayList.toArray());

public void myList() {
    listOrders.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION );
    panelForLists.add(listOrders);
}

public void updateList() {
    listOrders.setListData(arrayList.toArray());
    listOrders.revalidate();
    listOrders.repaint();
}

public void actionPerformed(ActionEvent event) {
    if (event.getSource() == deleteItem) {
         int index = listOrders.getSelectedIndex();
            listOrders.remove(index);
           updateList();
    }
}

I assume that maybe it’s because I have to add in a ListSelectionListener somewhere, but I don’t know how I can work this. I’ve searched on the internet for an answer for ages and this was what I came up with from the JList oracle demo.

Thanks a lot!

  • 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-02T21:21:44+00:00Added an answer on June 2, 2026 at 9:21 pm

    This is basically what Hovercraft Full Of Eels already answered, but with some more code pointers.

    1. You indeed remove and re-add the element, which of course has no effect. The easiest would be if your ArrayList could be used directly as ListModel but this is not possible (as the ArrayList does not fire any events). You might be tempted to use the JList( Vector ) constructor instead, but that one does not allow to modify the vector afterwards. So you are stuck with creating your own ListModel. The DefaultListModel class is probably just what you are looking for.
    2. The code in your actionPerformed method always assume there is an element selected. Note that the getSelectedIndex method returns -1 when nothing is selected. The best thing you could do (for the user) is to disable the delete button when nothing is selected, or when more then one element is selected. This can be achieved by letting the action behind the button listen to the selection model of the list, and update its enabled state depending on the selection (do not forget to fire a PropertyChangeEvent if needed). If you do not work with an Action backing up the button, just switch the enabled state of the button directly based on the selection.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I have a method that creates a list from the parsed ArrayList. I
I have this method here that gets an object from a JList. The object
i have a simple example of a JList that is getting it's data from
I have a JList which i want to use to display a list a
I programmed a JFrame that have a Button and JList , and when i
I have a list of Customer objects that I need to have selectable from
I have a JList that will point to a selected JList depending on user.
I have a JList of users, and whenever an element in the list is
I'm making a swing based application where I have a JList that periodically get's
I have a JList inside a Scrollpane. If you click on the list and

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.