简体   繁体   English

从ArrayList加载数据到DefaultTableModel

[英]Loading Data from ArrayList Unto a DefaultTableModel

Please I am trying to load data stored in an ArrayList unto a DefaultTableModel but I dont see the data coming up. 请我尝试将存储在ArrayList中的数据加载到DefaultTableModel,但我没有看到数据出现。 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. 我尝试使用AbstractTableModel,但我没有办法解决它。 so its not an option presently. 所以现在不是一个选择。 Thanks in advance. 提前致谢。

    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. 试一试,我应该为你工作。

I can see from the below code for your UI 我可以从下面的代码中看到您的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. 您正在创建一个新的MusicEdit数组列表并迭代列表,该列表是空的,因为它刚刚创建时没有添加任何对象。

You need to iterate over the MusicEdit list created earlier in your 1st section of code. 您需要遍历在第一部分代码中创建的MusicEdit列表。 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. 尝试引用已添加元素的音乐列表并对其进行迭代。

// 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, 这段代码很好,在代码第5行的第二个和平,

ArrayList<MusicEdit> musiclist= new  ArrayList<MusicEdit>(); 

Here you are creating new object musiclist which is null. 在这里,您将创建新的对象musiclist ,该列表为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. 如果您分享您的课程,那么我可以相应地重构您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM