How do I access different parts of an item in a table?

Hello!
I have just started learning programming with Vaadin, and overall I’m quite the Java novice.

I’ve created a simple table that gets the content through an .xml file, and it’s working all fine and dandy, but I’m having a hard time figuring out how to simply get different parts of the table in to different fields, like into Strings to be printed elsewhere.

I have a table that has the properties “Name” and “Armor points”.

This table holds items such as “Leather helmet”, which has “5” armor points.

I can get the name of the selected item through this code:

	Object helmetRowId = helmetTable.getValue(); 							// Get the selected rows id
	String nameHelmet = (String)helmetTable.getContainerProperty(helmetRowId,"Name").getValue();	// Get the item name

… but I don’t know how to access the armor points part. I’ve tried this:

	Item itemHelmet = helmetTable.getItem(helmetRowId);

and it works nicely, but returns the whole item “Leather helmet 5”, whereas I only need the 5.

How do I access single properties of an item stored inside a table? I tried fooling around wtih getItemProperty but I just couldn’t figure out how it works!

Thanks in advance!

Hi"

This means that you have got the whole item containing all properties. You could think of the table as a coordinate system where Items are the rows and Properties the columns. You need them both to be able to pinpoint a cell of your clicked row.

Here is an example.
Lets first create the table

        final Table helmetTable = new Table();
        helmetTable.addContainerProperty("Name", String.class, null);
        helmetTable.addContainerProperty("Armor points", Integer.class, null);
        helmetTable.setSelectable(true);

After that let’s populate it with helmets (this is just one way of doing it):


        while (i++ < 10) {

            // Don't worry about this syntax, it is just a shorthand
            Helmet s = new Helmet() {
                {
                    setName(("Name " + (int) (Math.random() * 100)));
                    setArmor((int) (Math.random() * 100));
                }
            };
            Item item = helmetTable.addItem(s);
            item.getItemProperty("Name").setValue(s.getName());
            item.getItemProperty("Armor points").setValue(s.getArmor());
        }

After the table has been populated, let’s create a button that writes what you have selected if clicked:




        Button b = new Button("Click");
        b.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                // So, first table.getItem() to get the item, and from the item, 
                // getItemProperty("your property") to get the right cell for that row
                System.out.println(helmetTable.getItem(helmetTable.getValue()).getItemProperty(
                        "Name").getValue());
                System.out.println(helmetTable.getItem(helmetTable.getValue()).getItemProperty(
                        "Armor points").getValue());
            }
        });

And here is the Helmet class:

    class Helmet {
        String name;
        Integer armor;

        public String getName() {
            return name;
        }

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

        public Integer getArmor() {
            return armor;
        }

        public void setArmor(Integer armor) {
            this.armor = armor;
        }
    }

Thanks for the answer!

I actually figured this out a few days later after posting, and was about to post back here with how I got it working. Had one silly mistake. :bashful:

I build the table exactly as you posted and I populate it by reading an XML file that has multiple different helmets all with their own stats and names.

The problem was that I was getting a null point exception whenever I was trying to get the armor points off the selected row. The reason for this was that I was trying to find and get it out as a String, whereas it was an int. Changed the String to an Object instead and after that it worked.


The faulty code:

String helmetAP = (String)helmetTable.getContainerProperty(helmetRowId,"Armor points").getValue();


And the working code:

Object helmetAP = helmetTable.getContainerProperty(helmetRowId,"Armor points").getValue();

Ok, great you got it working. That’s even a shorter way of getting the properties :wink: