显示一个ArrayList的值与一个JTable [英] Displaying the values of an Arraylist with a JTable
问题描述
我创建了一个名为EmployeeGUI类是能够创建用户Employee对象的数量,并将它们添加到一个ArrayList。我的问题是,我有一个JTable的,我要显示我已经创建了,但在我的情况下GUI创建一个新的表对象每次经过一个for循环与时间的所有对象我的GUI的底部对象的详细信息,但作为对新的信息在for循环进入覆盖旧的信息,而不是加入到一个JTable的底部。谁能帮我调试我的错误?
下面是我的code
进口java.awt.event中。*;公共类EmployeeGUI扩展JFrame中实现的ActionListener {/ **
* @参数ARGS
* /
JFrame的框架;
JButton的按钮1,按钮2,按钮3,将Button4;
JTextField的BOX1,BOX2,BOX3;
JLabel的LABEL1,LABEL2,LABEL3;
表1的JTable;
INT长度= 0;
ArrayList的<员工> empArray =新的ArrayList<员工>();公共静态无效的主要(字串[] args){
// TODO自动生成方法存根
EmployeeGUI EMPG =新EmployeeGUI();
empG.frame.setVisible(真);
}公共EmployeeGUI()
{
初始化();
}公共无效的initialize(){
// TODO自动生成方法存根
帧=新的JFrame(A样本窗);
frame.setBounds(50,50,680,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane()的setLayout(空)。 LABEL1 =新的JLabel(F-名称:);
label1.setBounds(30,33,54,25);
。frame.getContentPane()加(LABEL1); BOX1 =新的JTextField();
box1.setBounds(94,35,128,20);
。frame.getContentPane()加(BOX1); LABEL2 =新的JLabel(S-名称:);
label2.setBounds(250,33,54,25);
。frame.getContentPane()加(LABEL2); BOX2 =新的JTextField();
box2.setBounds(305,35,128,20);
。frame.getContentPane()加(BOX2); LABEL3 =新的JLabel(手机);
label3.setBounds(461,33,54,25);
。frame.getContentPane()加(LABEL3); BOX3 =新的JTextField();
box3.setBounds(500,35,128,20);
。frame.getContentPane()加(BOX3); BUTTON1 =的新的JButton(添加员工);
button1.addActionListener(本);
button1.setBounds(71,131,113,39);
。frame.getContentPane()加(按钮1); 按钮2 =的新的JButton(删除员工);
button2.addActionListener(本);
button2.setBounds(194,131,128,39);
。frame.getContentPane()加(按钮2); 按钮3 =的新的JButton(显示雇员);
button3.addActionListener(本);
button3.setBounds(332,131,128,39);
。frame.getContentPane()加(按钮3); 将Button4 =的新的JButton(退出计划);
button4.addActionListener(本);
button4.setBounds(475,131,113,39);
。frame.getContentPane()加(将Button4); 表1 =新的JTable();
table1.setBounds(0184664178);
。frame.getContentPane()增加(表1);
}@覆盖
公共无效的actionPerformed(ActionEvent的五){
// TODO自动生成方法存根
字符串行动=((JButton的)e.getSource())getActionCommand()。
如果(action.equals(添加员工))
{
字符串FNAME = box1.getText();
字符串LNAME = box2.getText();
串PNO = box3.getText(); INT移动=的Integer.parseInt(PNO); 员工EE =新员工(FNAME,LNAME,手机);
empArray.add(EE);
长++;
JOptionPane.showMessageDialog(NULL,员工增加了);
}
如果(action.equals(删除员工))
{
字符串FNAME = box1.getText(),LNAME = box2.getText();
INT移动=的Integer.parseInt(box3.getText());
员工EE =新员工(FNAME,LNAME,手机);
如果(长度大于0)
{
的for(int i = 0; I< empArray.size();我++)
{
如果(empArray.get(ⅰ).getLName()== ee.getLName())
{
empArray.remove(ⅰ);
JOptionPane.showMessageDialog(NULL,员工删除了);
}
}
}
其他{
抛出新ListEmptyException(列表为空);
}
}
如果(action.equals(显示雇员))
{
的for(int i = 0; I< empArray.size();我++)
{
table1.setModel(新的DefaultTableModel(
新的对象[] [] {
{empArray.get(ⅰ).getFName(),empArray.get(ⅰ).getLName(),empArray.get(ⅰ).getMobile()}
},
新的String [] {
名,姓,电话号码
}
));
}
}
如果(action.equals(退出计划))
{
System.exit(0);
}
}
}
和Employee类
公共类Employee {
私人字符串FNAME,LNAME;
私人诠释移动;公务员(FNAME字符串,字符串LNAME,诠释移动)
{
setFName(FName参数);
setLName(LNAME);
setMobile(手机);
}私人无效setMobile(INT移动){
// TODO自动生成方法存根
this.mobile =移动;
}公共无效setLName(字符串LNAME){
// TODO自动生成方法存根
this.lName = LNAME;
}公共无效setFName(字符串FName参数){
// TODO自动生成方法存根
this.fName = FNAME;
}公共字符串getFName()
{
返回FNAME;
}公共字符串getLName()
{
返回LNAME;
}公众诠释getMobile()
{
返回移动;
}公共字符串的toString()
{
返回getFName()++ getLName()++ getMobile();
}公共无效的print()
{
的System.out.println(的toString());
}
}
新的细节将覆盖旧的细节,而不是被添加到一个JTable的底部。
块引用>首先添加的所有记录在一个列表然后设置模型的只有一次的否则会的覆盖的最后一个环路。
样code:
如果(action.equals(显示雇员)){
清单<对象[]>名单=新的ArrayList<对象[]>();
的for(int i = 0; I< empArray.size();我++){
list.add(新的对象[] {
empArray.get(ⅰ).getFName(),
empArray.get(ⅰ).getLName(),
empArray.get(ⅰ).getMobile()
}); }
table1.setModel(新的DefaultTableModel(list.toArray(新对象[] [] {}),
新的String [] {名,姓,电话号码}));
}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?
Here is my Code
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); } } }
and the Employee Class
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()); } }
解决方案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.
sample code:
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"})); }
这篇关于显示一个ArrayList的值与一个JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!