Reputation: 324
Please I am trying to load data stored in an ArrayList unto a DefaultTableModel but I dont see the data coming up. please review my code below.
// this is to save to the array.
ArrayList<MusicEdit> musiclist = new ArrayList<MusicEdit>();
code = Integer.parseInt(txtAddMusicCode.getText());
title = txtAddMusicTitle.getText();
artist = txtAddMusicArtist.getText();
price = Integer.parseInt(txtAddMusicprice.getText());
MusicEdit s = new MusicEdit(code, title, artist, price);
musiclist.add(s);
JOptionPane.showMessageDialog(null, "Added Successfully");
And to load the table Model, this what I've got.
public class DisplayMusicCD extends javax.swing.JFrame {
// Here is to load the TableModel
String[] columnName = {"Code", "Title", "Artist", "Price"};
DefaultTableModel dtm = new DefaultTableModel(columnName, 0);
ArrayList<MusicEdit> musiclist= new ArrayList<MusicEdit>();
public void loadAll(){
for (Object s : musiclist) {
dtm.addRow( (Object[]) s);
}
tblMusic.setModel(dtm);
}
public DisplayMusicCD() {
initComponents();
loadAll();
}
Please what I'm I missing here, the Table loads but only the column names shows up but the data doesn't show up. I tried using AbstractTableModel but I did not no my way around it. so its not an option presently. Thanks in advance.
Upvotes: 1
Views: 3205
Reputation: 1938
public class MusicEdit {
public ArrayList<MusicEdit> getMusiclist() {
ArrayList<MusicEdit> musiclist = new ArrayList<MusicEdit>();
code = Integer.parseInt(txtAddMusicCode.getText());
title = txtAddMusicTitle.getText();
artist = txtAddMusicArtist.getText();
price = Integer.parseInt(txtAddMusicprice.getText());
MusicEdit s = new MusicEdit(code, title, artist, price);
musiclist.add(s);
return musiclist;
}
}
public class DisplayMusicCD extends javax.swing.JFrame {
// Here is to load the TableModel
String[] columnName = {"Code", "Title", "Artist", "Price"};
MusicEdit musicEdit = new MusicEdit ();
DefaultTableModel dtm = new DefaultTableModel(columnName, 0);
ArrayList<MusicEdit> musiclist = musicEdit.getMusiclist();
public void loadAll(){
for (Object s : musiclist) {
dtm.addRow( (Object[]) s);
}
tblMusic.setModel(dtm);
}
public DisplayMusicCD() {
initComponents();
loadAll();
}
I code it here, to give you and idea how to do it. Give it a try i should work for you.
Upvotes: 2
Reputation: 601
I can see from the below code for your UI
public class DisplayMusicCD extends javax.swing.JFrame {
// Here is to load the TableModel
String[] columnName = {"Code", "Title", "Artist", "Price"};
DefaultTableModel dtm = new DefaultTableModel(columnName, 0);
ArrayList<MusicEdit> musiclist= new ArrayList<MusicEdit>();
...
You are creating a new MusicEdit array list and iterating over the list which is empty as it is just created without any object being added to it.
You need to iterate over the MusicEdit list created earlier in your 1st section of code. Where you have actually added objects to the list.
MusicEdit s = new MusicEdit(code, title, artist, price);
musiclist.add(s);
JOptionPane.showMessageDialog(null, "Added Successfully");
Try to get reference to the musiclist you have added element to and iterate over it.
Upvotes: 1
Reputation: 1938
// this is to save to the array.
ArrayList<MusicEdit> musiclist = new ArrayList<MusicEdit>();
code = Integer.parseInt(txtAddMusicCode.getText());
title = txtAddMusicTitle.getText();
artist = txtAddMusicArtist.getText();
price = Integer.parseInt(txtAddMusicprice.getText());
MusicEdit s = new MusicEdit(code, title, artist, price);
musiclist.add(s);
JOptionPane.showMessageDialog(null, "Added Successfully");
This code is fine, In second peace of code line 5,
ArrayList<MusicEdit> musiclist= new ArrayList<MusicEdit>();
Here you are creating new object musiclist
which is null. In order to load data from your list you need to get that list. If you share your class then i can refactor your code accordingly.
Upvotes: 0