我在按下排序按钮时尝试对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按照添加顺序显示元素。但是如果用户想要对它们进行排序,我创建了一个排序按钮,当按下按钮时,JList按排序顺序显示元素。我该如何设法做到这一点?我一遍又一遍地尝试,但它不起作用。
答案 0 :(得分:6)
只需将方法sort()添加到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();
}
});
答案 1 :(得分:1)
在您的模型中,尝试使用TreeSet<Song>
,然后创建Comparator<Song>
并将其传递到TreeSet<Song>
的构造函数中。它会在添加元素时自动对所有元素进行排序。
答案 2 :(得分:0)
修改内容后,需要调用列表模型的相应fire*
方法。在fireContentsChanged(this, 0, songs.size() - 1)
中致电getSortedList()
。 (顺便说一下,这个名字令人困惑)。