Domanda

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


        }
    }
È stato utile?

Soluzione

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.

Altri suggerimenti

You will need to add the sorted data to your table model, just like you did initially, but clearing first:

model.clear();

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

An alternative approach is to use Swing's TableRowSorter. See this example.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top