2013-08-18 88 views
1

我想按排序按鈕時排序JList,但我不知道如何。 這是我的代碼:排序jlist的元素

宋類:

package song; 

public class Song implements Comparable<Song>{ 
    private String name; 
    private String artist; 

    public Song(String name, String artist){ 
     this.name = name; 
     this.artist = artist; 
    } 

    public String getName(){ 
     return name; 
    } 

    public String getArtist(){ 
     return artist; 
    } 

    public void setArtist(String artist){ 
     this.artist = artist; 
    } 

    public void setName(String name){ 
     this.name = name; 
    } 

    public int compareTo(Song s){ 
     return this.name.compareTo(s.getName()); 
    } 

    public String toString(){ 
     return name + " " + artist; 
    } 
} 

列表型號:

package song; 

import java.util.ArrayList; 
import java.util.Collections; 

import javax.swing.AbstractListModel; 

public class ListModel extends AbstractListModel{ 
    ArrayList<Song> songs; 

    public ListModel(ArrayList<Song> array){ 
     songs = array; 
    } 

    public int getSize(){ 
     return songs.size(); 
    } 

    public Object getElementAt(int index){ 
     return (Song)songs.get(index); 
    } 

    public ArrayList<Song> getSongList(){ 
     return songs; 
    } 

    public void setList(ArrayList<Song> array){ 
     this.songs = array; 
    } 

    public void getSortedList(ArrayList<Song> array){ 
     Collections.sort(array); 
     songs = array; 
    } 
} 

App類:

package song; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.Reader; 
import java.util.ArrayList; 
import java.util.Collections; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.ListSelectionModel; 

public class App extends JFrame{ 
    JFrame frame; 
    JList<Song> list; 
    ArrayList<Song> songList; 
    ListModel songModelList; 
    private static final String FILE = "songs.txt"; 
    JPanel loadPanel, addBtnPanel; 
    JButton btnLoad, btnSort, btnAdd, btnSave; 
    JTextField txtArtist, txtName; 

    public static void main(String[] args){ 
     App a = new App(); 
    } 

    public App(){ 
     frame = new JFrame("Playlist"); 

     loadPanel = createLoadPanel(); 
     loadPanel.setBackground(Color.BLUE); 

     JPanel btnPanel = createButtonPanel(); 
     btnPanel.setBackground(Color.ORANGE); 
     frame.getContentPane().add(btnPanel,BorderLayout.EAST); 
     frame.getContentPane().add(loadPanel,BorderLayout.CENTER); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(300, 300); 
//  frame.pack();s 
     frame.setVisible(true); 

    } 

    public JPanel createButtonPanel(){ 
     JPanel panel = new JPanel(); 
     btnLoad = new JButton("Load List"); 
     btnSort = new JButton("Sort list"); 
     btnAdd = new JButton("Add"); 
     btnSort.addActionListener(new ActionListener() { //here is the action listener for the button used for sorting 

      @Override 
      public void actionPerformed(ActionEvent e) { 
//    Collections.sort(songList); 
//    songModelList.setList(songList); 
       songModelList.getSortedList(songList); 
       list.updateUI(); 
      } 
     }); 
     btnLoad.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       loadPanel.setVisible(true); 
      } 
     }); 
     btnAdd.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       addBtnPanel = createAddPanel(); 
      } 
     }); 

     panel.add(btnLoad); 
     panel.add(btnSort); 
     panel.add(btnAdd); 

     return panel; 
    } 

    public JPanel createAddPanel(){ 
     JFrame frame = new JFrame(); 
     JPanel panel = new JPanel(new GridLayout(3,2)); 
     JLabel lblName = new JLabel("Name"); 
     JLabel lblArtist = new JLabel("Artist"); 
     txtName = new JTextField(15); 
     txtArtist = new JTextField(15); 
     panel.add(lblName); 
     panel.add(txtName); 
     panel.add(lblArtist); 
     panel.add(txtArtist); 
     btnSave = new JButton("Save"); 
     panel.add(btnSave); 
     btnSave.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       String name = txtName.getText(); 
       String artist = txtArtist.getText(); 
       Song s = new Song(name, artist); 
       songModelList.getSongList().add(s); 
       list.updateUI(); 
      } 
     }); 

     frame.getContentPane().add(BorderLayout.CENTER,panel); 
     frame.setVisible(true); 
     frame.setSize(new Dimension(300,100)); 
     return panel; 
    } 

    public JPanel createLoadPanel(){ 
     JPanel LoadPanel = new JPanel(); 
     list = new JList<Song>(); 
     songList = loadFromFile(); 
     songModelList = new ListModel(songList); 
     songList = new ArrayList<Song>(); 
     list.setModel(songModelList); 
     list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     LoadPanel.add(list); 
     LoadPanel.setVisible(false); 
     return LoadPanel; 

    } 

    public ArrayList<Song> loadFromFile(){ 
     ArrayList<Song> array = new ArrayList<Song>(); 
     try{ 
      BufferedReader reader = new BufferedReader(new FileReader(FILE)); 
      String line = null; 
      while((line = reader.readLine()) != null){ 
       String[] parts = line.split(" - "); 
       if(parts.length == 2){ 
        String name = parts[0]; 
        String artist = parts[1]; 
        Song s = new Song(name, artist); 
        array.add(s); 
       } 
       else{ 
        System.out.println("Invalid line!"); 
       } 
      } 
      reader.close(); 
     }catch(IOException e){ 
      System.out.println("File not found"); 
      e.printStackTrace(); 
     } 
     return array; 
    } 
} 

所以,JList中desplays元素的順序他們被添加。但是如果用戶想對它們排序,我做了一個排序按鈕,當按下按鈕時,JList按排序順序顯示元素。我如何設法做到這一點?我一遍又一遍地嘗試,但它不起作用。

+1

請您將您的代碼僅限於所需的部分? – Tala

回答

5

只需添加方法排序()來的ListModel:

public void sort(){ 
    Collections.sort(songs); 
    fireContentsChanged(this, 0, songs.size()); 
} 

和修復btnSort行動:

btnSort.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     songModelList.sort(); 
    } 
}); 
+0

好吧,那真的很簡單。我不敢相信我試了幾個小時,我什麼也沒有想出來。再次感謝! :) –

0

當您修改內容時,您需要調用列表模型的相應fire*方法。撥打getSortedList()撥打fireContentsChanged(this, 0, songs.size() - 1)。 (順便提一下,這是令人困惑地命名的)。

+0

謝謝你的回答 –

1

在模型中,嘗試使用TreeSet<Song>,然後創建一個Comparator<Song>,並將它傳遞到您的TreeSet<Song>的構造函數。它會在元素添加時自動對所有元素進行排序。

+0

謝謝你的回答。我也會嘗試TreeSet :) –