並べ替えボタンを押したときに 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;
}
}
アプリ クラス:
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 に要素が並べ替えられた順序で表示されます。どうすればそれを行うことができますか?何度も何度も試しましたがうまくいきません。