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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:32:18+00:00 2026-06-01T14:32:18+00:00

I’m making a swing based application where I have a JList that periodically get’s

  • 0

I’m making a swing based application where I have a JList that periodically get’s updated with different orderings of the same data, gets infrequently updated with new items, and also infrequently updated with less items. I’m trying to figure out the best way to make this look good. Right now, I just call

JList.setListData(String [] data);

Which doesn’t look too great, and it clears the selected items.

I want a way to update it that only clears the selected items if it was removed from the list, but otherwise keeps the same items selected, even if their index changes. I was looking into keeping track of which indexs are selected and then setting the selected items after changing the data but that sounds horrible. I also looked at ListModels and keeping track of my own but it seems that which items are selected is kept in the JList itself so that wouldn’t work perfectly either.

I’d really appreciate any suggestions.

  • 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-01T14:32:19+00:00Added an answer on June 1, 2026 at 2:32 pm

    I made you a working example.

    I’ve used the JList like you requested, added buttons that resort, add, and delete from the list.

    Anyways, hope this helps!

    Cheers.

    public class Test {
    
        private static String selectedValue = null;
        private static JList jList = new JList();
    
        public static void main(String args[]) {
            JFrame jFrame = new JFrame();
            jFrame.setSize(500, 500);
            jFrame.setLocationRelativeTo(null);
            jFrame.setLayout(new GridLayout(4, 1));
    
            jList = new JList(new String[]{"1", "2", "3", "4", "5"});
            jFrame.add(jList);
    
            JButton rearrangeButton = new JButton("rearrange");
            rearrangeButton.addActionListener(new Test().new SelectedListener());
            jFrame.add(rearrangeButton);
    
            JButton addButton = new JButton("add");
            addButton.addActionListener(new Test().new SelectedListener());
            jFrame.add(addButton);
    
            JButton deleteButton = new JButton("delete");
            deleteButton.addActionListener(new Test().new SelectedListener());
            jFrame.add(deleteButton);
    
            jFrame.setVisible(true);
        }
    
        private class SelectedListener implements ActionListener {
    
            public void actionPerformed(ActionEvent actionEvent) {
                storeSelected();
    
                if (actionEvent.getActionCommand().equalsIgnoreCase("rearrange")) {
                    jList.setListData(new String[]{"5", "4", "3", "2", "1"});
                } else if (actionEvent.getActionCommand().equalsIgnoreCase("add")) {
                    List< String> items = new ArrayList< String>();
    
                    for (int item = 0; item < jList.getModel().getSize(); item++) {
                        items.add(jList.getModel().getElementAt(item).toString());
                    }
    
                    items.add("new");
    
                    jList.setListData(items.toArray());
                } else if (actionEvent.getActionCommand().equalsIgnoreCase("delete")) {
                    List< String> items = new ArrayList< String>();
    
                    for (int item = 0; item < jList.getModel().getSize(); item++) {
                        items.add(jList.getModel().getElementAt(item).toString());
                    }
    
                    items.remove(0);
    
                    jList.setListData(items.toArray());
                }
    
                reSelect();
            }
        }
    
        private static void storeSelected() {
            if (jList.getSelectedIndex() > -1) {
                selectedValue = jList.getSelectedValue().toString();
            } else {
                selectedValue = null;
            }
        }
    
        private static void reSelect() {
            if (selectedValue != null) {
                jList.setSelectedValue(selectedValue, true);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.