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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:31:19+00:00 2026-06-04T21:31:19+00:00

Question: Is there an easy way to sort jList using Up/Down Buttons on jFrame?

  • 0

Question: Is there an easy way to sort jList using Up/Down Buttons on jFrame?
My JList stores path’s of image files and displays string with name of the file. I would like to move down/up the element by clicking down/up Button.

Here’s what I did – the effect is moving the selection (blue field), not the element. Button2 is button “up”.

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    int indexOfSelected = jList1.getSelectedIndex();
    File selectedFile = (File) jList1.getSelectedValue();
    indexOfSelected = indexOfSelected - 1;
    jList1.setSelectedIndex(indexOfSelected );
    jList1.updateUI();

}

This is how the JList is created:

public void openButtonActionPerformed() {

        fc.setMultiSelectionEnabled(true);
        int returnVal = fc.showDialog(null, "Open");

         if (returnVal == JFileChooser.APPROVE_OPTION) { 
               file = fc.getSelectedFiles();
               len = file.length;
               System.out.println(len);   
         }    
         for (i=0; i<len; i++){ 
            listModel.add(i, file[i]);
         }
         jList1.setModel(listModel);
         jList1.updateUI();
}

Thank you for your help and patience – in advance. I’m a begginer:)

  • 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-04T21:31:22+00:00Added an answer on June 4, 2026 at 9:31 pm

    If you use list model, that supports set operation you can do following:

    private void swapElements(int pos1, int pos2) {
        File tmp = (File) listModel.get(pos1);
        listModel.set(pos1, listModel.get(pos2));
        listModel.set(pos2, tmp);
    }
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        int indexOfSelected = jList1.getSelectedIndex();
        swapElements(indexOfSelected, indesOfSelected - 1);
        indexOfSelected = indexOfSelected - 1;
        jList1.setSelectedIndex(indexOfSelected );
        jList1.updateUI();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As a follow up to this question: Is there an easy way to pickle
A straight forward question: Is there some easy way to write fixtures (i.e. in
Dumb question is there an easy way to clear to the desktop and then
Question Is there a way to define a method only once in C# (in
Question: Is there a way to check if a given font is one of
Silly question - Is there a way to download the iPhone SDK without Xcode
Possible duplicate question: Is there a way to indefinitely pause a thread? In my
The real question: Is there a way to clear certain attributes for all components
Is there an easy way to manually (ie. not through code) find the size
For some background, please see my question: Rails app using STI -- easiest way

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.