setValue: Integer with blank for zero

I’m using setValue() on a proprty in a table that has an Integer class attached to it. I want to display blanks when the property is zero, but I can’t set the value to blank because that’s not part of the Integer class.

Do I need to create a new class that is an Integer but allows blanks, or is there an easier way to format the display of properties in a table?

Off the top of my head, I can think of several approaches - in descending order of appropriateness (IMHO)

  • Override Table#formatPropertyValue, and perform the formatting there
  • Use a
    PropertyFormatter
  • Use a
    ColumnGenerator
  • If you’re using an underlying bean, and can modify it, introduce a new pseudo-property that formats appropriately

See
Book Of Vaadin
for a few more details

I prefer the first approach - in our project, we have a simple Table subclass that overrides formatPropertyValue, and allows to register formatters for each column.

HTH,

Cheers,

Charles.

I am overriding the formatPropertyValue() method but none of the properties that are of the class Integer are getting there. I have put a logger in the method and all the properties are Strings. Why would Integers avoid the formatPropertyValue() method?

Hi,

I suspect that you are assuming the second parameter of formatProperty is the value - it isn’t. The first parameter is rowId, the second parameter is the columnId, and the third parameter is the Property containing the value for that particular cell.

I’ve knocked together a small demonstration
here on GitHub
showing how to format a 0 as an empty string in a table, by overriding formatPropertyValue.

Cheers,

Charles.

Your method works great when there is a value of zero, but what about when there is no value? Then Vaadin uses the default value, which can’t be blank. The only solution I can think of is a class that is an Integer except that its toString() is blank for zero. Maybe even that won’t work, as I need blank to be a valid character for Integer. Somehow I need Integer + blank as a class.