jtable仅使用链表中的值填充一行

时间:2018-09-04 03:50:00

标签: java jtable

所以我有一个包含雇员的姓名,地址和所在城市的类。之后,该类将存储在链接列表中,并且当用户在文本字段中输入名称,地址和城市时,会将值插入到链接列表中,然后填充到jtable中。到目前为止,我已经能够获取用户详细信息并将其添加到“喜欢的列表”,但是当我单击“添加”按钮时,如果我在文本字段中插入一些其他值并单击再次使用addbutton,它只是删除了先前添加的行,并添加了仍仅显示一行的新行,我希望jtable显示所有值。这是我的代码。

public class EmployeeDetails extends javax.swing.JFrame {
String [] str = {"NAME", "ADDRESS", "CITY", "ZICODE"};
LinkedList<EmployeeDetails> linkedlist;
String name;
String address;
String city;
String zipcode;


public EmployeeDetails(String name, String address, String city, String zipcode) {
   this.name = name;
   this.address = address;
   this.city = city;
   this.zipcode = zipcode;
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void setAddres(String address) {
    this.address = address;
}

public String getAddress() {
    return address;
}

public void setCity(String city) {
    this.city = city;
}

public String getCity() {
    return city;
}

public void setZip(String zipcode) {
    this.zipcode = zipcode;
}

public String getZip() {
    return zipcode;
}

public String toString() {
    return name + " " + address + " " + city + " " + zipcode;
}

public EmployeeDetails() {
    initComponents();
}

//this is the method that does the adding
public void add() {
    name = nametxt.getText().trim();
    address = addresstxt.getText().trim();
    city = citytxt.getText().trim();
    zipcode = zipcodetxt.getText().trim();

    linkedlist = new LinkedList<>();
    linkedlist.add(new EmployeeDetails(name, address, city, zipcode));
    DefaultTableModel model = new DefaultTableModel(str, linkedlist.size());

    for(EmployeeDetails details : linkedlist) {
        System.out.println(details);
        model.addRow(new Object[]{details.getName()details.getAddress(), details.getCity(), details.getZip()});
    }

    employeeTable.setModel(model);
}
}

1 个答案:

答案 0 :(得分:1)

看看您的add方法。它创建了一个新的LinkedList,并添加了一个EmployeeDetails(从JFrame扩展过来!!!),并创建了一个新的TableModel,并添加了(单个)EmployeeDetails应用于它,然后将其应用于employeeTable ...为什么感到惊讶?

EmployeeDetails没有理由从基于UI的组件扩展,它为您提供了零收益。 EmployeeDetails类也不负责管理任何形状或形式的UI。

OO开发的关键方面之一是职责分离。 EmployeeDetails应该管理员工的详细信息,没有其他内容……

public class EmployeeDetails {

    String name;
    String address;
    String city;
    String zipcode;

    public EmployeeDetails(String name, String address, String city, String zipcode) {
        this.name = name;
        this.address = address;
        this.city = city;
        this.zipcode = zipcode;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAddres(String address) {
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCity() {
        return city;
    }

    public void setZip(String zipcode) {
        this.zipcode = zipcode;
    }

    public String getZip() {
        return zipcode;
    }

    public String toString() {
        return name + " " + address + " " + city + " " + zipcode;
    }

}

尽管DefaultTableModel是一个强大而灵活的模型,但它也...有点基础。对我来说,我更喜欢可以管理其显示的对象并可以更好地决定如何显示它们的对象。当涉及到模型的可变状态时,这一点尤为重要(应该允许添加,删除甚至是编辑各个行吗?)

根据您的情况,您希望至少能够添加新的EmployeeDetails对象...

public static class EmployeeTableModel extends AbstractTableModel {

    protected static String [] COLUMN_NAMES = {"NAME", "ADDRESS", "CITY", "ZICODE"};

    private List<EmployeeDetails> rows = new ArrayList<>(25);

    @Override
    public int getRowCount() {
        return rows.size();
    }

    @Override
    public int getColumnCount() {
        return COLUMN_NAMES.length;
    }

    @Override
    public String getColumnName(int column) {
        return COLUMN_NAMES[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        EmployeeDetails ed = rows.get(rowIndex);
        switch (columnIndex) {
            case 0: return ed.getName();
            case 1: return ed.getAddress();
            case 2: return ed.getCity();
            case 3: return ed.getZip();
        }
        return null;
    }

    public void add(EmployeeDetails ed) {
        rows.add(ed);
        int row = rows.size() - 1;
        fireTableRowsInserted(row, row);
    }

    public void remove(EmployeeDetails ed) {
        int row = rows.indexOf(ed);
        if (row < 0) {
            return;
        }
    }

    public void remove(int row) {
        if (row < 0 || row > rows.size()) {
            return;
        }
        rows.remove(row);
        fireTableRowsInserted(row, row);
    }
}

除了管理模型状态外,该模型不执行其他任何操作。我更复杂的解决方案可能有一个附加的模型/控制器来管理如何创建新对象以及如何更新或删除现有对象,但是我将由您自己来添加。

最后,我们可以将它们缝合在一起...

public class TestPane extends JPanel {

    private EmployeeTableModel model = new EmployeeTableModel();

    public TestPane() {
        setLayout(new BorderLayout());
        add(new JScrollPane(new JTable(model)));

        JButton add = new JButton("Add");
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = model.getRowCount();
                EmployeeDetails ed = new EmployeeDetails("Employee " + row, "@ " + row, "Metro" + row, Integer.toString(row));
                model.add(ed);
            }
        });
        add(add, BorderLayout.SOUTH);
    }

}

一个带有按钮和表格的简单面板,现在您可以将“添加”按钮混搭,并观察员工列表的增长...希望您有足够的资金;)

确保您花时间阅读How to Use Tables,其中包含许多重要和相关的信息以及您可以使用的可运行示例

可运行的示例...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private EmployeeTableModel model = new EmployeeTableModel();

        public TestPane() {
            setLayout(new BorderLayout());
            add(new JScrollPane(new JTable(model)));

            JButton add = new JButton("Add");
            add.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int row = model.getRowCount();
                    EmployeeDetails ed = new EmployeeDetails("Employee " + row, "@ " + row, "Metro" + row, Integer.toString(row));
                    model.add(ed);
                }
            });
            add(add, BorderLayout.SOUTH);
        }

    }

    public static class EmployeeTableModel extends AbstractTableModel {

        protected static String [] COLUMN_NAMES = {"NAME", "ADDRESS", "CITY", "ZICODE"};

        private List<EmployeeDetails> rows = new ArrayList<>(25);

        @Override
        public int getRowCount() {
            return rows.size();
        }

        @Override
        public int getColumnCount() {
            return COLUMN_NAMES.length;
        }

        @Override
        public String getColumnName(int column) {
            return COLUMN_NAMES[column];
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            EmployeeDetails ed = rows.get(rowIndex);
            switch (columnIndex) {
                case 0: return ed.getName();
                case 1: return ed.getAddress();
                case 2: return ed.getCity();
                case 3: return ed.getZip();
            }
            return null;
        }

        public void add(EmployeeDetails ed) {
            rows.add(ed);
            int row = rows.size() - 1;
            fireTableRowsInserted(row, row);
        }

        public void remove(EmployeeDetails ed) {
            int row = rows.indexOf(ed);
            if (row < 0) {
                return;
            }
        }

        public void remove(int row) {
            if (row < 0 || row > rows.size()) {
                return;
            }
            rows.remove(row);
            fireTableRowsInserted(row, row);
        }
    }

    public class EmployeeDetails {

        String name;
        String address;
        String city;
        String zipcode;

        public EmployeeDetails(String name, String address, String city, String zipcode) {
            this.name = name;
            this.address = address;
            this.city = city;
            this.zipcode = zipcode;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setAddres(String address) {
            this.address = address;
        }

        public String getAddress() {
            return address;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getCity() {
            return city;
        }

        public void setZip(String zipcode) {
            this.zipcode = zipcode;
        }

        public String getZip() {
            return zipcode;
        }

        public String toString() {
            return name + " " + address + " " + city + " " + zipcode;
        }

    }

}