Sortowanie obiektów w JList

2016-09-27 20:56
0

W jaki sposób można posortować JList obiektów według nazwy itd.? Wiem jak sortować tablicę poprzez implementację Comparable, ale jak zrobić to z JList to nie mam pojęcia i nic zrozumiałego nie mogę znaleźć.


Pozostało 580 znaków

2016-09-27 21:12
2016-09-27 22:10
0

W ogóle tego nie kumam. Próbuję iść na około, ale też mam problem. Kawałek kodu:

comboSorted = new JComboBox<String>();
        comboSorted.setBounds(50,5, 90, 20);
        comboSorted.addItem("     ");
        comboSorted.addItem("A-Z");
        comboSorted.addItem("Z-A");
        panelList.add(comboSorted);
        comboSorted.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int index = comboSorted.getSelectedIndex();
                status = 1;

                if(index == 1) {
                    model.removeAllElements();
                    Collections.sort(listOriginalTitles);
                    Collections.sort(listMovie);

                    for(Movie movie : listMovie) {
                        model.addElement(movie.getOriginalTitle());
                    }
                }

            }

        });

        listFavouriteMovies = new JList<String>(model);

        FILES.loadNumberOfMovies(login);

        if(Add.numberOfMovies > 0) {
            if(status == 0) {
                deserializationMovie();
                listOriginalTitles = new ArrayList<String>(listMovie.size());
                for(Movie movie : listMovie) {
                    model.addElement(movie.getOriginalTitle());
                    listOriginalTitles.add(movie.getOriginalTitle());
                }
            }
        }

        listFavouriteMovies.setBounds(0, 28, 150, 660);
        panelList.add(listFavouriteMovies);
        listFavouriteMovies.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                panelHome.removeAll();

                int movieID = listFavouriteMovies.getSelectedIndex();

                String path = listMovie.get(movieID).getLinkPoster();

odpowiadający za sortowanie. Zamiast sortować na JList, to wczytuję do Listy tytuły, aby potem można je było łatwo posortować. Nie wiem dlaczego wywala mi błąd na

String path = listMovie.get(movieID).getLinkPoster();

Log kompilacji:

 Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.elementData(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Home$2.valueChanged(Home.java:123)
    at javax.swing.JList.fireSelectionValueChanged(Unknown Source)
    at javax.swing.JList$ListSelectionHandler.valueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.removeIndexInterval(Unknown Source)
    at javax.swing.plaf.basic.BasicListUI$Handler.intervalRemoved(Unknown Source)
    at javax.swing.AbstractListModel.fireIntervalRemoved(Unknown Source)
    at javax.swing.DefaultListModel.removeAllElements(Unknown Source)
    at Home$1.actionPerformed(Home.java:85)
    at javax.swing.JComboBox.fireActionEvent(Unknown Source)
    at javax.swing.JComboBox.setSelectedItem(Unknown Source)
    at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Wiem, że wyjątek wskazuje na nieprawidłowy indeks listy, ale nie wiem dlaczego. Trochę się pogubiłem.

Do sortowania obiektów używam tylko tego:

@Override
    public int compareTo(Movie movie) {
        int comparisonOriginalTitle = movie.getOriginalTitle().compareTo(movie.getOriginalTitle());

        return comparisonOriginalTitle;
    }

edytowany 2x, ostatnio: Jonki1997, 2016-09-27 22:36

Pozostało 580 znaków

2017-01-16 22:01
0

Wiem, że wątek został założony dość dawno temu, ale czy byłbyś Jonki uprzejmy wrzucić cały kod? Chętnie bym poszukał buga ;-)


Prawdziwy hardcorowiec nie robi backupów. Prawdziwy hardcorowiec korzysta tylko z biblioteki standardowej.

Pozostało 580 znaków

Liczba odpowiedzi na stronę

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0