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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:25:48+00:00 2026-06-13T14:25:48+00:00

I have a problem with showing results of method which sorting Person object by

  • 0

I have a problem with showing results of method which sorting Person object by name sortujKolekcjePoImionach(). This method works perfect when I put it in rozpocznijProgram() wchich is loading some names and put it in Person object and then put every single Person to ArrayList. DefaultListModel model refreshing Jlist so it works great. But when I put method sortujKolekcjePoImionach() to other claas which I connect to single button it doesn’t work class sortujKolekcjePoImionach. I don’t know if model doesn’t see that ArrayList daneOsobowe was sorted or I probably don’t know how to use Collections.sort(daneOsobowe,new SortujImie());

Main.java

public class Main {

    JFrame oknoGlowne;
    JTextArea sciezkaDoPliku;
    DefaultListModel<Person> model = new DefaultListModel<>();
    JList listaOsob;
    JButton wczytaj;
    JButton sortujImie;
    JButton sortujDate;
    JButton sortujImieNazwiskoDate;
    ArrayList daneOsobowe = new ArrayList();

    public void createGUI(){

        oknoGlowne = new JFrame("Kolekcje");
        oknoGlowne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        sciezkaDoPliku  = new JTextArea(); 

        wczytaj = new JButton("Wczytaj");

        sortujImie = new JButton("Posortuj Imiona");
        sortujDate = new JButton("Posortuj Daty");
        sortujImieNazwiskoDate = new JButton("Posortuj Imiona Nazwiska Daty");

        wczytaj.addActionListener(new rozpocznijProgram());

        listaOsob = new JList(model);  //MODEL
                    sortujImie.addActionListener(new sortujKolekcjePoImionach());  //TRYING TO SORT PERSON OBJECTS BY NAME
                    //rest of GUI...
                    oknoGlowne.setPreferredSize(new Dimension(870, 550));
        oknoGlowne.setLayout(new FlowLayout());
        oknoGlowne.setVisible(true);
        oknoGlowne.pack();
}
//METHOD WHICH IS ADDING PERSON OBJECTS TO ARRAY
class rozpocznijProgram implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent evt) {

        try {
            Wczytywanie wczytaj = new Wczytywanie(sciezkaDoPliku.getText());
            daneOsobowe = wczytaj.kopiowanieTablicy(sciezkaDoPliku.getText());

            //Collections.sort(daneOsobowe,new SortujImieNazwiskoData());

            for(int i=0; i<daneOsobowe.size(); i++){
                model.add(i, (Person) daneOsobowe.get(i));
                System.out.println(model.get(i));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        }
    }
    //METHOD WHICH IS SORTING PERSON OBJECTS BY NAME
    class sortujKolekcjePoImionach implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent evt) {

        Collections.sort(daneOsobowe,new SortujImie());  //IT DOESN'T WORK, JLIST DOESN'T CHANGE AFTER CLICK


        }
    }
  • 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-13T14:25:49+00:00Added an answer on June 13, 2026 at 2:25 pm

    You have a list of persons (daneOsobowe), and each of the persons in the list is added to a ListModel, which maintains its own list of persons. So you in fact have two different lists of persons: one in your own class, and one in the list model.

    Sorting your own list won’t magically sort the one used by the model. And even if it did, the model would have to trigger events in order for the view to repaint itself.

    So, if you sort the list, you need to update the list model in order to reorder the persons in the list model as well (for example, by removing all the persons from the model and re-adding them in the new order).

    Another, better solution, would be to implement your own list model (based on AbstractListModel), and provide a sort method in this model that would sort the internal list and trigger the appropriate event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://jsfiddle.net/q8P7Y/ I have a problem of showing the final score at the end, there
I have a problem where an element is not showing on Opera nor Firefox.
I have a problem with validation messages not showing after a redirect, even when
I have a list showing up when I click on button, the problem is
I have problem with my query on C, I’m using the oci8 driver. This
I have a problem showing my progress bar when reading a file in Java.
I have a problem with a taglib method c:forEach. I want to get a
I have a Problem with a UIScrollView CATiledLAyer stucture. What I'm doing is showing
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are

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.