如何使用动态的ArrayList填充gui JTable?

时间:2017-11-22 14:57:14

标签: java swing arraylist

所以,我试图用一个显示ArrayList的JTable创建一个gui。这个ArrayList反过来应该在实际的gui中动态填充。

更具体地说,我正在尝试编写一个程序,允许您向这些人添加人员和银行帐户。然后,我想显示哪个人拥有JTable中的哪些帐户。

我知道代码中存在一些非常错误的东西,因为我一直在尝试不同的东西来解决这个问题,到目前为止,这只会让我头疼。

非常感谢任何帮助!真的非常非常感激。

到目前为止我的代码:

控制器类

package org.lu.ics.inlämningsuppgifter;

import javax.swing.JFrame;

public class Controller  {
PersonRegister persons;
JFrame frame;

public Controller (PersonRegister personReg, JFrame frame) {
    this.persons=personReg;
    this.frame=frame;
}

public void addPerson (String pNbr, String pName) {
    Person tmpPerson = new Person(pNbr, pName);
    persons.addPerson(tmpPerson);
}

public void addPersonAccount (String pNbr, String pName, String accNbr) {
    Person tmpPerson = new Person(pNbr, pName);
    Account account = new Account(accNbr);
    tmpPerson.setAccounts(account);

}
public String[] findPerson(String pNbrFind) {
     Person p;
     String[] aPerson = null;
     p = persons.findPerson(pNbrFind);
     if (p != null && p.getAccounts() != null) {
     aPerson = new String[4];
     aPerson[0] = p.getPNbr();
     aPerson[1] = p.getName();
     aPerson[2] = p.getAccounts();
     } else if (p != null) {
     aPerson = new String[2];
     aPerson[0] = p.getPNbr();
     aPerson[1] = p.getName();
     }
     return aPerson;
     }


}

帐户类

package org.lu.ics.inlämningsuppgifter;

public class Account {

private String nbr;
private double balance; 
private Person owner;

public Person getOwner() {
    return owner;
}

public void setOwner(Person newOwner) {
    owner = newOwner;
}

public void setNbr(String newNbr) {
    nbr = newNbr;
}

public String getNbr() {
    return nbr;
}

public Account (String accNbr) {
    balance = 0.00;
    this.setNbr(accNbr);

}

public void setBalance(double newBalance) {
    balance = newBalance;
    this.setBalance(newBalance);

}

public double getBalance() {
    return balance;
}

public void credit(double amount) {
    balance = balance + amount;
}

public void withdraw(double amount) {
    balance = balance - amount;
}


}

人类     包org.lu.ics.inlämningsuppgifter;

import java.util.*;

public class Person {

private String pNbr;
private String name;
private ArrayList<Account> accounts = new ArrayList<Account>();

public Person(String pNbr, String pName) {
    this.setPNbr(pNbr);
    this.setName(pName);
}

public ArrayList<Account> getAccount() {
    return accounts;
}

public void setPNbr(String newPNbr) {
    pNbr = newPNbr;
}

public String getPNbr() {
    return pNbr;
}

public void setName(String newName) {
    name = newName;
}

public String getName() {
    return name;
}

public void setAccounts(ArrayList<Account> newAccounts) {
    accounts = newAccounts;
}

public ArrayList<Account> getAccounts() {
    return accounts;
}

public void addAccount(Account anAccount) {
    this.getAccounts().add(anAccount);
}


}

PersonApplication Class(愚蠢的名字,我知道)

package org.lu.ics.inlämningsuppgifter;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class PersonApplication {

private JFrame frame;
private final JTable tblAccounts = new JTable();
private JTextField textField;
private JTextField textField_1;
private JTextField textField_AccNbr;

private Controller controller;
private PersonRegister personRegister;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                PersonApplication window = new PersonApplication();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public PersonApplication() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    JFrame frame = new JFrame("Accounts");
    frame.setBounds(100, 100, 840, 524);
    frame.getContentPane();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    String col[] = {"Account", "Balance"};

    DefaultTableModel tableModel = new DefaultTableModel(col, 2);

    JTable table = new JTable(tableModel);

    JButton btnAddPerson = new JButton("Add person");
    btnAddPerson.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });
    btnAddPerson.setBounds(35, 296, 105, 23);
    frame.getContentPane().add(btnAddPerson);

    JButton btnRemovePerson = new JButton("Remove person");
    btnRemovePerson.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });
    btnRemovePerson.setBounds(35, 330, 107, 23);
    frame.getContentPane().add(btnRemovePerson);

    JButton btnFindPerson = new JButton("Find person");
    btnFindPerson.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String accNumber = textField_AccNbr.getText();
            //String[] tmpCustomer = controller.
        }
    });
    btnFindPerson.setBounds(35, 364, 91, 23);
    frame.getContentPane().add(btnFindPerson);

    JButton btnAddAccount = new JButton("Add Account");
    btnAddAccount.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });
    btnAddAccount.setBounds(269, 296, 99, 23);
    frame.getContentPane().add(btnAddAccount);

    JButton btnShowAccount = new JButton("Show Accounts");
    btnShowAccount.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            DefaultTableModel model=(DefaultTableModel) 
 tblAccounts.getModel();

        }
    });
    btnShowAccount.setBounds(439, 451, 105, 23);
    frame.getContentPane().add(btnShowAccount);
    tblAccounts.setBounds(439, 300, 352, 132);
    frame.getContentPane().add(tblAccounts);

    JLabel lblName = new JLabel("Name:");
    lblName.setBounds(47, 29, 31, 14);
    frame.getContentPane().add(lblName);

    textField = new JTextField();
    textField.setBounds(139, 26, 86, 20);
    frame.getContentPane().add(textField);
    textField.setColumns(10);

    JLabel lblPnmbr = new JLabel("PNmbr:");
    lblPnmbr.setBounds(47, 57, 35, 14);
    frame.getContentPane().add(lblPnmbr);

    textField_1 = new JTextField();
    textField_1.setBounds(139, 51, 86, 20);
    frame.getContentPane().add(textField_1);
    textField_1.setColumns(10);

    JLabel lblAccountNumber = new JLabel("Account number:");
    lblAccountNumber.setBounds(47, 90, 82, 14);
    frame.getContentPane().add(lblAccountNumber);

    textField_AccNbr = new JTextField();
    textField_AccNbr.setBounds(139, 87, 86, 20);
    frame.getContentPane().add(textField_AccNbr);
    textField_AccNbr.setColumns(10);
}
}

PersonRegister Class

package org.lu.ics.inlämningsuppgifter;

import java.util.*;

public class PersonRegister {

private ArrayList<Person> persons = new ArrayList<Person>();

public ArrayList<Person> getPersoner() {
    return persons;
}

public void setPersons(ArrayList<Person> newPersons) {
    persons = newPersons;
}

public void addPerson(Person newPerson) {
    persons.add(newPerson);
}

public Person findPerson(String pNbr) {
    for (Person p : this.persons) {
        if (p.getPNbr().equals(pNbr)) {
            return p;
        }
    }
    return null;
}

public Person removePerson(String pNbr) {
    Person p = findPerson(pNbr);
    if (p != null) {
        persons.remove(p);
    }
    return null;
}
}

1 个答案:

答案 0 :(得分:1)

  

我正在尝试使用显示ArrayList的JTable创建一个gui。这个ArrayList反过来应该在实际的gui中动态填充。

问题是Swing适用于模型,而不适用于ArrayLists。因此,特别需要创建一个自定义TableModel来保存数据。此模型可以使用ArrayList来保存数据,但您的应用程序永远不能直接使用ArrayList。您可以使用模型的方法添加/删除数据。

查看Row Table Model以获取有关如何创建在ArrayList内部保存Person对象的自定义TableModel的分步示例。

您的PersonRegister类被PersonTableModel类及其所有必需的方法和自定义方法替换。