当前位置: 动力学知识库 > 问答 > 编程问答 >

java - How to add a table to the layout

问题描述:

I have created a table in its own class FileUploaderResultsTable.java, which looks like this:

public class FileUploaderResultsTable extends Table

{

public FileUploaderResultsTable()

{

Table resultTable = new Table("Rendering table");

resultTable.addStyleName("testTable2");

resultTable.addContainerProperty("Name", String.class, null);

resultTable.addContainerProperty("Mag", Float.class, null);

resultTable.addItem(new Object[]{"Canopus", -0.72f}, 2);

resultTable.addItem(new Object[]{"Arcturus", -0.04f}, 3);

resultTable.addItem(new Object[]{"Alpha Centauri", -0.01f}, 4);

resultTable.setPageLength(resultTable.size());

System.out.println("table created");

}

I have another class FileUploaderComponent.java that, among the other things has a vertical layout that is added to a Window object. I'm trying to display the above table, so in the FileUploaderComponent.java I've created a FileUploaderResultsTable object , like so:

public class FileUploaderComponent extends AbstractJavaScriptComponent

{

...

private VerticalLayout myLayout = new VerticalLayout();

private FileUploaderResultsTable resultsTable = new FileUploaderResultsTable();

...

public void addTable(){

myLayout.addComponent(resultsTable);

}

}

myLayout then gets added to a new window object in another function. The mechanism works because I'm adding other elements like text fields etc, but for some reasons that table doesn't get added in properly.

In the DOM I can see that there is a table but it's empty and it doesn't have the class name specified.

To be really honest, ordinarily if I wanted to add a component from another class to a layout I would usually refer to the component as resultsTable.resultTable so the object.component syntax rather than just resultsTable, but the compiler doesn't like that. SO, is there another way to add a table as a component to the layout or am I just doing something stupid?

网友答案:

You are using your extended class the wrong way. Try this:

public class FileUploaderResultsTable extends Table
{

    public FileUploaderResultsTable()
    {
        setCaption("Rendering table");
        addStyleName("testTable2");
        addContainerProperty("Name", String.class, null);
        addContainerProperty("Mag",  Float.class, null);

        addItem(new Object[]{"Canopus",        -0.72f}, 2);
        addItem(new Object[]{"Arcturus",       -0.04f}, 3);
        addItem(new Object[]{"Alpha Centauri", -0.01f}, 4);

        setPageLength(resultTable.size());

        System.out.println("table created");
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: