Reputation: 82
I am trying to figure out how to use a JList
correctly. I am struggling with the usage of lists in combination with my own classes/data models.
My goal is to to extract the list in a separate class and change the displayed data within the list from multiple actions (Button Events / Action Listeners).
For example: If the user has chosen a compatible CSV file, the list should show the contents of that file. The file itself contains a simple playlist which is compound as follows: Artist; Title; File path
If the user adds a further song to the playlist, the list should "refresh". I've read something about "FireContents..." but I firstly would like to know how to use those lists properly.
I am going to add the relevant parts of my source code. Any help & advice are appreciated.
public class MusicTrack implements project.model.Track{
private String strArtist;
private String strTitle;
private String strFilepath;
public MusicTrack(String strArtist, String strTitle, String strFilepath){
this.strArtist = strArtist;
this.strTitle = strTitle;
this.strFilepath = strFilepath;
}
MusicPlayer:
public class MusicPlayer implements ActionListener{
private JFrame mainframe;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MusicPlayer window = new MusicPlayer();
window.mainframe.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MusicPlayer() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
mainframe = new JFrame();
mainframe.setTitle("MusicPlayer");
mainframe.setBounds(100, 100, 450, 300);
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.setLocationRelativeTo(null);
// menu & menuitems
JMenuBar menuBar = new JMenuBar();
mainframe.setJMenuBar(menuBar);
JMenu mnFile = new JMenu("Datei");
menuBar.add(mnFile);
JMenuItem mnItemLoadNewList = new JMenuItem("Neue Liste laden");
mnFile.add(mnItemLoadNewList);
JMenuItem mnItemLoadLIst = new JMenuItem("Liste laden & anhängen");
mnFile.add(mnItemLoadLIst);
JMenuItem mnItemSave = new JMenuItem("Speichern");
mnFile.add(mnItemSave);
JMenuItem mnItemNewEntry = new JMenuItem("Neuer Eintrag");
mnFile.add(mnItemNewEntry);
JMenu mnSort = new JMenu("Sortieren");
menuBar.add(mnSort);
JMenuItem mnItemArtist = new JMenuItem("nach Interpret");
mnSort.add(mnItemArtist);
JMenuItem mnItemTitle = new JMenuItem("nach Titel");
mnSort.add(mnItemTitle);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{450, 0};
gridBagLayout.rowHeights = new int[]{227, 29, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
mainframe.getContentPane().setLayout(gridBagLayout);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
JPanel listPanel = new JPanel();
listPanel.setLayout(gridBagLayout);
mainframe.getContentPane().add(listPanel);
listPanel.setVisible(true);
// Buttons
JButton btnPlayAll = new JButton("Play All");
GridBagConstraints gbc_btnPlayAll = new GridBagConstraints();
gbc_btnPlayAll.anchor = GridBagConstraints.NORTH;
gbc_btnPlayAll.fill = GridBagConstraints.HORIZONTAL;
gbc_btnPlayAll.gridx = 0;
gbc_btnPlayAll.gridy = 1;
mainframe.getContentPane().add(btnPlayAll, gbc_btnPlayAll);
//Action- & Eventlistener, registering individual ActionListener per Button
mnItemLoadNewList.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser fChoose = new JFileChooser();
FileNameExtensionFilter fExt = new FileNameExtensionFilter("CSV", "csv");
fChoose.setFileFilter(fExt);
int returnVal = fChoose.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION){
JList<Track> list = new JList<Track>();
ListModel<Track> listModel = new DefaultListModel<Track>();
list.setModel(listModel);
JScrollPane sPane = new JScrollPane(list);
sPane.add(listPanel);
}
};
});
// FIXME - Attach!
mnItemLoadLIst.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser fChoose = new JFileChooser();
FileNameExtensionFilter fExt = new FileNameExtensionFilter("CSV", "csv");
fChoose.setFileFilter(fExt);
int returnVal = fChoose.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION){
}
};
});
// FIXME - Save!
mnItemSave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser fChoose = new JFileChooser();
FileNameExtensionFilter fExt = new FileNameExtensionFilter("CSV", "csv");
fChoose.setFileFilter(fExt);
int returnVal = fChoose.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION){
File selcdFile = new File(fChoose.getSelectedFile().toString());
PlayList myPL = new PlayList();
myPL.getTracks();
try {
myPL.save(selcdFile);
} catch (IOException e1) {
JOptionPane.showMessageDialog(mainframe, e1 + "\n\nBitte eine kompatible Datei wählen.", "MusicPlayer Error", JOptionPane.WARNING_MESSAGE);
}
}
};
});
mnItemNewEntry.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
NewEntry myNewEntry = new NewEntry(new JFrame(), "Neuer Eintrag");
myNewEntry.setSize(300, 240);
myNewEntry.setLocationRelativeTo(mainframe);
myNewEntry.setVisible(true);
};
});
Class NewEntry
// FIXME - btnChooseFile
btnChooseFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser fChoose = new JFileChooser();
FileNameExtensionFilter fExt = new FileNameExtensionFilter("MP3", "mp3"); // FIXME add wave etc.
fChoose.setFileFilter(fExt);
int returnVal = fChoose.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION){
txtFilepath.setText(fChoose.getSelectedFile().getAbsolutePath());
}
};
});
PlayList (contains simple methods such as saving, loading)
public class PlayList implements project.model.PlayList{
private List<Track> playList = new LinkedList<>();
private String strDelimiter = ";";
public void setPlayList(List<Track> playList) {
this.playList = playList;
}
Code for JList, that still gives me headache.
ListModel<PlayList> theModel = new DefaultListModel<PlayList>();
JList<PlayList> myJList = new JList<PlayList>(theModel);
for(int i = 0; i < myPlayList.getTracks().size(); i++){
theModel.addElement(i);
}
JScrollPane myScrollPane = new JScrollPane(myJList);
mainframe.add(myScrollPane);
Upvotes: 2
Views: 1788
Reputation: 2739
You should really start by reading the official tutorial for JList
, as there's quite a lot to cover. Here's the overview, though:
DefaultListModel
will do.ListCellRenderer
.Upvotes: 2