使用 JTable 显示 Arraylist 的值 [英] Displaying the values of an Arraylist with a JTable
问题描述
我创建了一个名为 EmployeeGUI 的类,它能够创建任意数量的用户 Employee 对象并将它们添加到数组列表中.我的问题是我的 GUI 底部有一个 JTable,我想显示我创建的所有对象,但在我的情况下,GUI 每次通过 for 循环时都会创建一个新的表对象对象的详细信息,但随着 for 循环的进行,新的详细信息会覆盖旧的详细信息,而不是添加到 jtable 的底部.谁能帮我调试错误?
I created a class named EmployeeGUI that is able to create any amount of user Employee objects and add them to an arraylist. The problem i have is that i have a JTable at the bottom of my GUI that i want to display all the objects that i've created but in my case the GUI Creates a new Table Object each time it goes through a for loop with the details of an object but as the for loop goes on the new details overwrites the old details rather than being added to the bottom of a jtable. Can anyone help me debug my errors?
这是我的代码
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());
}
}
推荐答案
新的细节会覆盖旧的细节,而不是被添加到 jtable 的底部.
the new details overwrites the old details rather than being added to the bottom of a jtable.
首先将所有记录添加到列表中,然后设置模型只需一次,否则它将覆盖循环中的最后一个.
First add all the record in a List then set the model just once otherwise it will override the last one in loop.
示例代码:
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"}));
}
这篇关于使用 JTable 显示 Arraylist 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!