如何从arraylist填充jframe表?

时间:2023-01-29 09:05:46

I have a jtable that I created using java GUI and I need to fill from an arraylist. I'm not familiar with java and I have no idea how to fill a jTable. I don't know if I need to use TableModel or just JTable table = new JTable(data, columnNames). This is my code:

我有一个使用java GUI创建的jtable,我需要从arraylist填充。我不熟悉java,我不知道如何填写jTable。我不知道我是否需要使用TableModel或只需要JTable table = new JTable(data,columnNames)。这是我的代码:

 public void filltable(){

    ArrayList A=new ArrayList();
    jframe4 j=new jframe4();
    String m=j.getloggedemail();

  //  A=s.getEmails(m);
  //this is an example of my arraylist

    A.add("sender@gmail.com");
    A.add("my subject");
    A.add("hiiii");

    try {
        String[] columnNames = {"Sender",
                        "Subject",
                        "Body",
                     };

         //here I need to fill my table


            }

        catch(Exception e) {
           JOptionPane.showMessageDialog(null, e);
        }
    }

This is the declaration of jTable1 that I need to fill:

这是我需要填写的jTable1的声明:

   jTable1.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null},
            {null, null, null},
            {null, null, null},
            {null, null, null}
        },
        new String [] {
            "Sender", "Subject", "Body"
        }
    ));
    jScrollPane1.setViewportView(jTable1);

2 个解决方案

#1


I don't know if I need to use TableModel or just JTable table = new JTable(data, columnNames)

我不知道是否需要使用TableModel或只需要JTable table = new JTable(data,columnNames)

Depends on your needs, you can do either. I will demonstrate how to use the latter. If you want to use your own TableModel, see the link in @Andrew Thompson's comment.

取决于您的需求,您可以做任何一种。我将演示如何使用后者。如果您想使用自己的TableModel,请参阅@Andrew Thompson评论中的链接。

public class Example extends JFrame {

    Example() {

        String[] colNames = new String[] {"Sender", "Subject", "Body"};

        List<String> list = new ArrayList<>();
        list.add("sender@gmail.com");
        list.add("my subject");
        list.add("hiiii");
        list.add("sender2@gmail.com");
        list.add("my subject2");
        list.add("hiiii2");

        String[][] rowData = new String[list.size() / colNames.length][colNames.length];
        for (int i = 0; i < rowData.length; i++) {
            for (int j = 0; j < rowData[i].length; j++) {
                rowData[i][j] = list.get(i * colNames.length + j);
            }
        }

        JTable table = new JTable(rowData, colNames);

        add(new JScrollPane(table));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {

        new Example();
    }
}

Just note that if the list does not contain 3 elements in a row, the whole row will not be displayed.

请注意,如果列表中不包含3个元素,则不会显示整行。

#2


You can use custom bean class and specify your data then load it to your table. Even I have done similar work. I've modified for your data.

您可以使用自定义bean类并指定数据,然后将其加载到表中。即使我做过类似的工作。我已经修改了你的数据。

public class ListToJtable {
public static void main(String[] args) {
    List<Message> messags = new ArrayList<>();
    Message message = new Message("sender@gmail.com", "my subject", "hiiii");
    messags.add(message);
    Message message1 = new Message("sender1@gmail.com", "my subject1",
            "hiiii1");
    messags.add(message1);
    JTable t = new JTable();
    t.setModel(new javax.swing.table.DefaultTableModel(
            new Object[][] {}, 
            new String[] { "Sender", "Subject", "Body" }));
    addToTable(messags, t);
    JPanel panel = new JPanel();
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(t);
    panel.add(scrollPane);

    JFrame frame = new JFrame();
    frame.add(panel);
    frame.setSize(500, 500);
    frame.setVisible(true);
}

public static void addToTable(List<Message> messags, JTable jTable) {
    for (Message message : messags) {
        ((DefaultTableModel) jTable.getModel()).addRow(new Object[] {
                message.getSender(), message.getSubject(),
                message.getBody() });
    }
}

}


public class Message {

String sender;
String subject;
String body;
public Message(String a1,String a2,String a3){
    sender = a1;
    subject = a2;
    body = a3;
}
public String getSender() {
    return sender;
}
public void setSender(String sender) {
    this.sender = sender;
}
public String getSubject() {
    return subject;
}
public void setSubject(String subject) {
    this.subject = subject;
}
public String getBody() {
    return body;
}
public void setBody(String body) {
    this.body = body;
}

}

#1


I don't know if I need to use TableModel or just JTable table = new JTable(data, columnNames)

我不知道是否需要使用TableModel或只需要JTable table = new JTable(data,columnNames)

Depends on your needs, you can do either. I will demonstrate how to use the latter. If you want to use your own TableModel, see the link in @Andrew Thompson's comment.

取决于您的需求,您可以做任何一种。我将演示如何使用后者。如果您想使用自己的TableModel,请参阅@Andrew Thompson评论中的链接。

public class Example extends JFrame {

    Example() {

        String[] colNames = new String[] {"Sender", "Subject", "Body"};

        List<String> list = new ArrayList<>();
        list.add("sender@gmail.com");
        list.add("my subject");
        list.add("hiiii");
        list.add("sender2@gmail.com");
        list.add("my subject2");
        list.add("hiiii2");

        String[][] rowData = new String[list.size() / colNames.length][colNames.length];
        for (int i = 0; i < rowData.length; i++) {
            for (int j = 0; j < rowData[i].length; j++) {
                rowData[i][j] = list.get(i * colNames.length + j);
            }
        }

        JTable table = new JTable(rowData, colNames);

        add(new JScrollPane(table));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {

        new Example();
    }
}

Just note that if the list does not contain 3 elements in a row, the whole row will not be displayed.

请注意,如果列表中不包含3个元素,则不会显示整行。

#2


You can use custom bean class and specify your data then load it to your table. Even I have done similar work. I've modified for your data.

您可以使用自定义bean类并指定数据,然后将其加载到表中。即使我做过类似的工作。我已经修改了你的数据。

public class ListToJtable {
public static void main(String[] args) {
    List<Message> messags = new ArrayList<>();
    Message message = new Message("sender@gmail.com", "my subject", "hiiii");
    messags.add(message);
    Message message1 = new Message("sender1@gmail.com", "my subject1",
            "hiiii1");
    messags.add(message1);
    JTable t = new JTable();
    t.setModel(new javax.swing.table.DefaultTableModel(
            new Object[][] {}, 
            new String[] { "Sender", "Subject", "Body" }));
    addToTable(messags, t);
    JPanel panel = new JPanel();
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(t);
    panel.add(scrollPane);

    JFrame frame = new JFrame();
    frame.add(panel);
    frame.setSize(500, 500);
    frame.setVisible(true);
}

public static void addToTable(List<Message> messags, JTable jTable) {
    for (Message message : messags) {
        ((DefaultTableModel) jTable.getModel()).addRow(new Object[] {
                message.getSender(), message.getSubject(),
                message.getBody() });
    }
}

}


public class Message {

String sender;
String subject;
String body;
public Message(String a1,String a2,String a3){
    sender = a1;
    subject = a2;
    body = a3;
}
public String getSender() {
    return sender;
}
public void setSender(String sender) {
    this.sender = sender;
}
public String getSubject() {
    return subject;
}
public void setSubject(String subject) {
    this.subject = subject;
}
public String getBody() {
    return body;
}
public void setBody(String body) {
    this.body = body;
}

}