使用JTable显示Arraylist的值

时间:2014-07-24 11:54:50

标签: java swing user-interface arraylist jtable

我创建了一个名为EmployeeGUI的类,它可以创建任意数量的用户Employee对象并将它们添加到arraylist中。我的问题是我的GUI底部有一个JTable,我想显示我创建的所有对象,但在我的情况下,GUI每次通过for循环时创建一个新的表对象一个对象的细节但是当for循环继续时,新的细节会覆盖旧的细节,而不是被添加到jtable的底部。任何人都可以帮我调试我的错误吗?

这是我的代码

import java.awt.event.*;

public class EmployeeGUI extends JFrame implements ActionListener{

/**
 * @param args
 */
JFrame frame;
JButton button1, button2, button3, button4;
JTextField box1, box2, box3;
JLabel label1, label2, label3;
JTable table1;
int length = 0;
ArrayList<Employee> empArray = new ArrayList<Employee>();

public static void main(String[] args) {
    // TODO Auto-generated method stub
    EmployeeGUI empG = new EmployeeGUI();
    empG.frame.setVisible(true);
}

public EmployeeGUI()
{
    initialize();
}

public void initialize() {
    // TODO Auto-generated method stub
    frame = new JFrame("A Sample Window");
    frame.setBounds(50,50,680,400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    label1 = new JLabel("F-Name:");
    label1.setBounds(30,33,54,25);
    frame.getContentPane().add(label1);

    box1 = new JTextField();
    box1.setBounds(94, 35, 128,20);
    frame.getContentPane().add(box1);

    label2 = new JLabel("S-Name:");
    label2.setBounds(250,33,54,25);
    frame.getContentPane().add(label2);

    box2 = new JTextField();
    box2.setBounds(305, 35, 128,20);
    frame.getContentPane().add(box2);

    label3 = new JLabel("Phone:");
    label3.setBounds(461,33,54,25);
    frame.getContentPane().add(label3);

    box3 = new JTextField();
    box3.setBounds(500, 35, 128,20);
    frame.getContentPane().add(box3);

    button1 = new JButton("Add Employee");
    button1.addActionListener(this);
    button1.setBounds(71,131,113,39);
    frame.getContentPane().add(button1);

    button2 = new JButton("Remove Employee");
    button2.addActionListener(this);
    button2.setBounds(194,131,128,39);
    frame.getContentPane().add(button2);

    button3 = new JButton("Display Employee");
    button3.addActionListener(this);
    button3.setBounds(332,131,128,39);
    frame.getContentPane().add(button3);

    button4 = new JButton("Quit Program");
    button4.addActionListener(this);
    button4.setBounds(475,131,113,39);
    frame.getContentPane().add(button4);

    table1 = new JTable();
    table1.setBounds(0,184,664,178);
    frame.getContentPane().add(table1);
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    String action = ((JButton) e.getSource()).getActionCommand();
    if(action.equals("Add Employee"))
    {
        String fName = box1.getText();
        String lName = box2.getText();
        String pNo = box3.getText();

        int mobile = Integer.parseInt(pNo);

        Employee ee = new Employee(fName,lName,mobile);
        empArray.add(ee);
        length++;
        JOptionPane.showMessageDialog(null, "Employee Added");
    }
    if(action.equals("Remove Employee"))
    {
        String fName = box1.getText(), lName = box2.getText(); 
        int mobile = Integer.parseInt(box3.getText());
        Employee ee = new Employee(fName,lName,mobile);
        if(length>0)
        {
            for(int i=0; i<empArray.size(); i++)
            {
                if(empArray.get(i).getLName() == ee.getLName())
                {
                    empArray.remove(i);
                    JOptionPane.showMessageDialog(null, "Employee Removed");
                }
            }
        }
        else{       
            throw new ListEmptyException("List is Empty");
        }
    }
    if(action.equals("Display Employee"))
    {
        for(int i = 0; i <empArray.size(); i++)
        {
            table1.setModel(new DefaultTableModel(
                    new Object[][] {
                            {empArray.get(i).getFName(),empArray.get(i).getLName(),empArray.get(i).getMobile()}
                    },
                    new String[] {
                        "First Name", "Surname", "Phone Number"
                    }
                ));
        }
    }
    if(action.equals("Quit Program"))
    {
        System.exit(0);
    }
}
}

和员工类

public class Employee {
private String fName,lName;
private int mobile;

public Employee(String fName, String lName, int mobile)
{
    setFName(fName);
    setLName(lName);
    setMobile(mobile);
}

private void setMobile(int mobile) {
    // TODO Auto-generated method stub
    this.mobile = mobile;
}

public void setLName(String lName) {
    // TODO Auto-generated method stub
    this.lName = lName;
}

public void setFName(String fName) {
    // TODO Auto-generated method stub
    this.fName = fName;
}

public String getFName()
{
    return fName;
}

public String getLName()
{
    return lName;
}

public int getMobile()
{
    return mobile;
}

public String toString()
{
    return getFName()+" "+getLName()+" "+getMobile();
}

public void print()
{
    System.out.println(toString());
}
}

3 个答案:

答案 0 :(得分:2)

仅评论,更长

  • 查看在循环中创建的table1.setModel(new DefaultTableModel(的数量for(int i = 0; i <empArray.size(); i++)

  • new Object[][] {{empArray.get(i).getFName(), empArray.get(i).getLName(),empArray.get(i).getMobile()}},已转换为DataVector,您已丢失,无法访问此阵列

  • (如果有理由在程序中保留两个相同的数组)请在util.List

    中使用基于AbstractTableModel的数组

答案 1 :(得分:1)

  

新细节会覆盖旧细节,而不是添加到jtable的底部。

首先在List中添加所有记录,然后将模型设置为,否则它将覆盖循环中的最后一个。

示例代码:

if (action.equals("Display Employee")) {
    List<Object[]> list = new ArrayList<Object[]>();
    for (int i = 0; i < empArray.size(); i++) {
        list.add(new Object[] { 
                                  empArray.get(i).getFName(), 
                                  empArray.get(i).getLName(),
                                  empArray.get(i).getMobile() 
                              });

    }
    table1.setModel(new DefaultTableModel(list.toArray(new Object[][] {}), 
                        new String[] {"First Name", "Surname", "Phone Number"}));
}

答案 2 :(得分:0)

您可以使用addRow()的{​​{1}}方法,如下所示:

DefaultTableModel