Thomas S
Thomas S

Reputation: 21

Fill JTable in Java using Web Service recieving a List<Project>

Hello I have a school project where we created webmethods using web service with an ordinary .asmx file.

The programs I made in .Net C# have no problem with the return being a List<Project>. However, I try to get the Java to work, and I tried Object[][] = wws.SelectAllProject() and so forth.

Here's the webmethod:

  [WebMethod]//Ska vara collection
    public List<Project> SelectAllProjects()
    {
        Controller contr = new Controller();
        List<Project> project = contr.SelectAllProjects();
        return project;
    }

Here's the Project.cs file:

public class Project
{
public string pId { get; set; }
    public string pName { get; set; }
    public string pDescript { get; set; }

    public Project()
    {
        this.pId = pId;
        this.pName = pName;
        this.pDescript = pDescript;
    }
}

So in Java how shall I call the method? I can call my other methods that are void which are ADD/Remove project...

public void SelectAllProjects () {
    WebService webService = new WebService();
    wws = webService.getWebServiceSoap12();
    //Anropar webservice och får en Collection av Project
    String[] columnNames = {"Project ID:", "Project Name:", "Project Description:"};
    Object[][] = wws.selectAllProjects();
    //Array[]test = wws.selectAllProjects();
}

Now I want to return something from this method (it should not be void) that which I can use to fill out a JTable in my view class (using MVC). The webmethod you see and returns a list of project. How shall I make this in Java? Tried googling, but I don't seem to find anything that is close to my project.

Thanks.

Upvotes: 1

Views: 1060

Answers (1)

trashgod
trashgod

Reputation: 205775

Compose your TableModel with a reference to your List<Project>, and use the model to construct your JTable. For example, your table might display two of the three project attribtes, and your row count would rely on the list's size().

@Override
public int getColumnCount() {
    return 2;
}

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

For reference, this example extends AbstractTableModel and illustrates an implementation of getValueAt() that uses a Map<String, String>. See also How to Use Tables: Creating a Table Model.

Upvotes: 2

Related Questions