-1

i have a Swing App, i use Retrofit to Consume Rest Service in my Swing App.

Now I want to Populate the Response of m Get Service in a Jtable.

Thais the List for all Employees that i get using Retrofit

 ArrayList<Employee> employees = EmployeesData.getInstance().getEmployees();

I want to Populate that in a Jtable

That is My Employee Modell

public class Employee {

    private long empno;
    private String ename;
    private String job;
    private Date hiredate;
    private long mgr;
    private long sal;
    private long deptno;

    public long getEmpno() {
        return empno;
    }

    public void setEmpno(long empno) {
        this.empno = empno;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public Date getHiredate() {
        return hiredate;
    }

    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }

    public long getMgr() {
        return mgr;
    }

    public void setMgr(long mgr) {
        this.mgr = mgr;
    }

    public long getSal() {
        return sal;
    }

    public void setSal(long sal) {
        this.sal = sal;
    }

    public long getDeptno() {
        return deptno;
    }

    public void setDeptno(long deptno) {
        this.deptno = deptno;
    }

    @Override
    public String toString() {
        return empno + " \t"
                + ename + " \t"
                + deptno + " \t"
                + mgr + " \t"
                + sal + " \t"
                + hiredate.toString();
    }
}

Here i want to Populate the Table

public void updateConsole() {
        ArrayList<Employee> employees = EmployeesData.getInstance().getEmployees();
        StringBuilder builder = new StringBuilder();
        for (Employee employee : employees) {
            builder.append(employee.toString());


            columns.add("col1");
            columns.add("col2");
            columns.add("col3");

            TableModel tableModel = new DefaultTableModel(employees.toArray(new Object[][] {}), columns.toArray());

            table = new JTable(tableModel);

            builder.append("\n");
        }

        textPane.setText(builder.toString());
    }

And i have that Error

java.lang.ArrayStoreException
    at java.lang.System.arraycopy(Native Method)
    at java.util.Arrays.copyOf(Arrays.java:3213)
    at java.util.ArrayList.toArray(ArrayList.java:407)
    at View.Console.updateConsole(Console.java:43)
    at application.Main.updateConsole(Main.java:54)

Somebody can help me please?

  • 1) [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) 2) What have you tried? Where are you stuck? 3) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). If the current code is successfully fetching the data, factor that out & start with hard coded data. – Andrew Thompson Jul 15 '17 at 13:33
  • @AndrewThompson i have edit the Post. I have Arraylist of Employees. I get it using Rest Services. I want to populäre it in Jtable – alli pierre yotti Jul 15 '17 at 13:38
  • Why did you edit the question to include an uncompilable code snippet? Follow the links & actually **read** the content! – Andrew Thompson Jul 15 '17 at 13:39
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) BTW - there is ***still*** no MCVE / SSCCE. Glad I did not delay down voting this question. It's a waste of other people's time. – Andrew Thompson Jul 15 '17 at 13:53

1 Answers1

0

it Works fine now. I make it like so

public void updateConsole() {
        ArrayList<Employee> employees = EmployeesData.getInstance().getEmployees();
        Object[] columnNames = { "Deptno", "Empno", "Name","Hiredate","Job","Management","Salary"};
        DefaultTableModel model = new DefaultTableModel(new Object[0][0], columnNames);
        StringBuilder builder = new StringBuilder();
        for (Employee employee : employees) {
            //builder.append(employee.toString());

            Object[] o = new Object[7];
            o[0] = employee.getDeptno();
            o[1] = employee.getEmpno();
            o[2] = employee.getEname();
            o[3] = employee.getHiredate();
            o[4] = employee.getJob();
            o[5] = employee.getMgr();
            o[6] = employee.getSal();
            model.addRow(o);



            //builder.append("\n");
        }

        //textPane.setText(builder.toString());
        table.setModel(model);
    }