Table Data not show when hide colums

I have 2 problem
1 → Below show how I created table


        tblRecivedTask = new Table("Recived requests");
        tblRecivedTask.setImmediate(true);
        tblRecivedTask.setSelectable(true);
        tblRecivedTask.setNullSelectionAllowed(false);
        tblRecivedTask.setHeight("200px");
        tblRecivedTask.setWidth("600px");
        tblRecivedTask.addContainerProperty(TASK_PROPERTY_TASK_NAME, String.class, "");
        tblRecivedTask.addContainerProperty(TASK_PROPERTY_REF_NO, String.class, "");
        tblRecivedTask.addContainerProperty(TASK_PROPERTY_FROM, String.class, "");
        tblRecivedTask.addContainerProperty(TASK_PROPERTY_RECIVED_TIME, String.class, "");
        tblRecivedTask.addContainerProperty(TASK_PROPERTY_APPROVED_TIME, String.class, "");

After that I added data to table below show that



 for (int x = 0; x < 5; x++) {
            String y = Integer.toString(x);
            tblRecivedTask.addItem(new Object[]{y, y, y, y, y}, x);
        }

        Object[] recivedVisbleColum = {TASK_PROPERTY_TASK_NAME, TASK_PROPERTY_REF_NO, TASK_PROPERTY_FROM, TASK_PROPERTY_RECIVED_TIME};
        tblRecivedTask.setVisibleColumns(recivedVisbleColum);

above way show table data and hide columns

but if I change order like below way

Object[] recivedVisbleColum = {TASK_PROPERTY_TASK_NAME, TASK_PROPERTY_REF_NO, TASK_PROPERTY_FROM, TASK_PROPERTY_RECIVED_TIME};
        tblRecivedTask.setVisibleColumns(recivedVisbleColum);
        
        for (int x = 0; x < 5; x++) {
            String y = Integer.toString(x);
            tblRecivedTask.addItem(new Object[]{y, y, y, y, y}, x);
        }

Table didn’t show any data but colums hide why is that how can i solve this problem ?

2 - >

I’m using refresher to load data to table if i use hide columns data not visible, if i remove it work perfectly how can i solve this problem below show that code

@Override
        public void run() {
            try {
                while (true) {                    
                    if (running) {

                        // synchronize with the application, to avoid concurrent
                        // edits on the label's value.
                        synchronized (MyApplication.this) {
                            tblRecivedTask.removeAllItems();
                            int u = x++;

                            for (int a = 0; a < 5; a++) {
                                String y = Integer.toString(a);
                                tblRecivedTask.addItem(new Object[]{y, "a", y, u, y}, a);
                            }
                            Object[] recivedVisbleColum = {TASK_PROPERTY_TASK_NAME, TASK_PROPERTY_REF_NO, TASK_PROPERTY_FROM, TASK_PROPERTY_RECIVED_TIME};
                            tblRecivedTask.setVisibleColumns(recivedVisbleColum);
                        }
                    }
                    sleep(SLEEP_TIME_IN_MILLIS);
                }

            } catch (Exception e) {
                e.printStackTrace();               
            }
        }

How can I solve those issues ?

Hi!

Basically your problems come from the fact that Table.addItem(Object, Object) only allows you to specify the values of the
visible
columns. This means that if you hide them before trying to add new items, the addItem() call will fail (since cells.length != visibleColumns.size()).

You should think about creating an object that contains the properties that you want to show in the table, e.g.

public class Task {
    private String name;
    private String refNo;
    .
    .
    .
    public void getName() {
         return name;
    }
    .
    .
    .
}

And set the table to use a BeanItemContainer as its data source. This way you can easily add and update items in the table.

HTH,
/Jonatan

Hi,

I think I answered this on Skype already, but here goes:

The javadoc for
Table.addItem(Object[],Object)

states "Adds the new row to table and fill the visible cells […]
", and indeed, it uses the
visible
columns, not all columns.

Your addItem() call is probably failing because your giving it 5 values, and it’s expecting 4 - it’ll return null to indicate this (an exception could have been more informative though…)

So you have two options: give only four values to the “shorthand” addItem() leaving the fifth to its default value, or leave the shorthand alone and set each property separately. I’d recommend the latter, even if it’s more verbose. That, or use Beans for the data.

Oh, and both 1 and 2 are probably the same problem, if I read the code correctly.

Best Regards,
Marc

Thanks for the replies. I found the answer for my both question.

        BeanItemContainer<Customer> bic = new BeanItemContainer<Customer>(Customer.class);
        for (int i = 0; i < 5; i++) {
            Customer customer = new Customer();

            customer.setCustomerID(Integer.toString(i));
            customer.setCustomerName(Integer.toString(i));
            customer.setAddress(Integer.toString(i));
            bic.addItem(customer);
        }
        tblRecivedTask.setContainerDataSource(bic);
        tblRecivedTask.setColumnHeaders(new String[]{"Customer ID", "Custome Name", "Custome Address"});
        tblRecivedTask.setVisibleColumns(new Object[]{"customerID", "customerName"});

Now I faced another problem,

If I set columns header before visible columns → column headers names are interchange but data visible correctly. like below


tblRecivedTask.setColumnHeaders(new String[]{"Customer ID", "Customer Name", "Customer Address"});
tblRecivedTask.setVisibleColumns(new Object[]{"customerID", "customerName","address"});

If I do it other way around work correctly, below show that


tblRecivedTask.setVisibleColumns(new Object[]{"customerID", "customerName","address"});
tblRecivedTask.setColumnHeaders(new String[]{"Customer ID", "Customer Name", "Customer Address"});

If I hide one column like below


tblRecivedTask.setVisibleColumns(new Object[]{"customerID", "customerName"});
tblRecivedTask.setColumnHeaders(new String[]{"Customer ID", "Customer Name", "Customer Address"});

gave below exception

java.lang.IllegalArgumentException: The length of the headers array must match the number of visible columns

how can I solve this issue

I removed
setColumnHeaders
and add below code now work . please correct me if I wrong



        tblRecivedTask.addContainerProperty("customerID", String.class, null);
        tblRecivedTask.addContainerProperty("customerName", String.class, null);
        tblRecivedTask.addContainerProperty("customerAddress", String.class, null);

	tblRecivedTask.setColumnHeader("customerID", "Customer ID");
        tblRecivedTask.setColumnHeader("customerName", "Customer Name");
        tblRecivedTask.setColumnHeader("age", "Age");

	BeanItemContainer<Customer> bic = new BeanItemContainer<Customer>(Customer.class);
        for (int i = 0; i < 5; i++) {
            Customer customer = new Customer();

            customer.setCustomerID(Integer.toString(i));
            customer.setCustomerName(Integer.toString(i) + "name");
            customer.setCustomerAddress(Integer.toString(i) + "address");
            bic.addItem(customer);
        }
        tblRecivedTask.setContainerDataSource(bic);

	tblRecivedTask.setVisibleColumns(new Object[]{"customerID", "customerName"});

Hi,

Yeah, that’s a less error-prone way of doing it (the ‘proper’ way, I’d almost say).

The array-of-string/object way of doing it is a shorthand notation, really, and one has to be careful because order and length is important.
The reason why the headers got mixed up for you is this: when you first set the headers they are applied to the original (unordered) column order, then when you rearrange the columns the headers follow the columns they were applied to.

Anyway, specifying the id explicitly is less error prone, and not so easy to break when refactoring.

Best Regards,
Marc

Hi Marc,

I have a similar problem
I’m using an array that holds the names of the visible columns and as i don’t want to display the id of the objects on the table so i removed the id from the array of visible columns, which works perfectly but that leads me another problem.

If i want to delete any item from the table i can only remove it from the container (which will make it disappear from the table) but doesn’t allow me to remove from the database because i cant get the id of the selected object.

Do you have any idea how i can get around that?

Thanks

Sorry, this is too little information for me, I’m afraid… Why can’t you get the id (how are you trying)?
Code, perhaps?

Best Regards,
Marc