Yes, it is possible.
1) You can one of this methods: ColumnBuilder.addConditionalStyles or ColumnBuilder.addConditionalStyle
2) You should implement your own ConditionStyleExpression
The working sample
The code
The code of implementation ConditionStyleExpression:
public class FieldComparator extends ConditionStyleExpression implements CustomExpression {
private String fieldName;
private String valueToCheck;
public FieldComparator(String fieldName, String valueToCheck) {
this.fieldName = fieldName;
this.valueToCheck = valueToCheck;
}
@Override
public Object evaluate(Map fields, Map variables, Map parameters) {
Object value = getCurrentValue();
if (value == null)
return null;
if (!fields.containsKey(fieldName)) {
return Boolean.FALSE;
}
if (valueToCheck == null) {
return Boolean.FALSE;
}
return Boolean.valueOf(valueToCheck.equals(fields.get(fieldName)));
}
@Override
public String getClassName() {
return Boolean.class.getName();
}
}
I've compared the field's value with string passed via the parameter.
The code for building report:
public class ConditionalStylesTest extends BaseDjReportTest {
public static void main(String[] args) throws Exception {
ConditionalStylesTest test = new ConditionalStylesTest();
test.testReport();
test.exportToJRXML();
JasperViewer.viewReport(test.jp);
}
@Override
public DynamicReport buildReport() throws Exception {
DynamicReportBuilder drb = new DynamicReportBuilder();
Integer margin = 20;
drb.setTitle("NBA Players")
.setTitleHeight(30)
.setDetailHeight(15)
.setLeftMargin(margin)
.setRightMargin(margin)
.setTopMargin(margin)
.setBottomMargin(margin)
.setPrintBackgroundOnOddRows(true)
.setColumnsPerPage(1)
.setColumnSpace(1);
drb.addColumn(ColumnBuilder.getNew().setColumnProperty("age", Integer.class.getName())
.setTitle("Age").setWidth(30).setFixedWidth(true).build());
drb.addColumn(ColumnBuilder.getNew().setColumnProperty("name", String.class.getName())
.setTitle("Name").setWidth(100).setFixedWidth(true).build());
drb.addColumn(ColumnBuilder.getNew().setColumnProperty("currentTeam", String.class.getName())
.setTitle("Team").setWidth(120).addConditionalStyle(createConditionalStyle()).setFixedWidth(true).build());
drb.setUseFullPageWidth(true);
return drb.build();
}
private ConditionalStyle createConditionalStyle() {
Style style = new Style();
style.setHorizontalAlign(HorizontalAlign.CENTER);
ConditionStyleExpression expression = new FieldComparator("currentTeam", "-");
return new ConditionalStyle(expression, style);
}
@Override
protected JRDataSource getDataSource() {
return new JRBeanCollectionDataSource(Players.get());
}
}
In this sample I've used the JavaBean datasource. And I've use conditional style for currentTeam field.
The bean Player:
public class Player {
private Integer age;
private String name;
private String currentTeam;
public Player(Integer age, String name, String team) {
this.age = age;
this.name = name;
this.currentTeam = team;
}
public Integer getAge() {
return age;
}
public String getName() {
return name;
}
public String getCurrentTeam() {
return currentTeam;
}
}
The result
The result generated in JasperViewer:

As you can see the string "-"
has Center alignment.