Creating Expressions Activity

 

 

 

Step

Action

1. 

Activity End Result

At the end of this activity the Department report displays department ID alongside department name and displays a message instead of the chart for departments whose employees are of one gender, as displayed above.

 

To achieve this you:

- Create a text field expression to display the department_ID alongside the department name

- Add conditional logic to the chart and a text field so that if male or female count is zero, a message replaces the chart.

2. 

Open the Department Report you created earlier.

3. 

Begin by combining the department_description and department_id expressions into one text field.

 

In the Detail band, click in the department_description field.

4. 

In the Properties panel, click the Text Field Expression button.

5. 

Note: The expression editor appears, with the department expression in the field.

6. 

Click to position the cursor after the Text Field Expression.

7. 

The expression will display a department name followed by a colon, and then the department's ID value.

 

Enter " + ":" + " into the Text Field Expression field.

8. 

In the center pane below, double-click department_id to insert it at the end of the expression.

9. 

Note: The department_id field is added to the expression. The expression now displays a department name followed by a colon, and then the department's ID value.

10. 

Click the OK button to save changes and close the editor.

11. 

Since the department_description text field now also displays the department_id, the department_id text field is redundant and should be removed from the report.

 

In the Detail band, click the department_id field to select it.

12. 

Press [Delete] to delete the field from the band.

13. 

Click the Preview button to execute the report.

14. 

Note: The Department Report displays the department name and ID number together under the gray header bar.

15. 

Click the Designer button to return to design view.

16. 

Next, open the Employee Report 

from the previous activity.

 

Click the File menu.

17. 

Point to the Open Recent File menu.

18. 

Click the employee_report.jrxml menu item.

19. 

Note: The Employee Report opens in design view.

20. 

Now, configure a Print When expression for the Employees chart. A Print When expression provides logic that determines when an object appears in a report.

 

Click the Chart object to select it.

21. 

In the Properties panel, click the Print When Expression button.

22. 

Note: The Print When Expression window appears. Configure the expression so charts appear only for departments that employ both males and females.

23. 

Enter "$V{maleCount} > 0 && $V{femaleCount} > 0" into the Print When Expression field.

24. 

Note: This expression uses the two variables created in an earlier activity to determine if the numbers of males and females are both greater than zero.

25. 

Click the OK button to save the expression and close the window.

26. 

Click the Preview button.

27. 

View information for Department 2.

 

Enter "2" into the department_ID field.

28. 

Click the OK button.

29. 

Note: The report for Department 2 is generated. The chart does not appear because the Print When expression allows it to appear only when both males and females work in a department.

Department 2 employs only females.

30. 

Next, configure a simple message to appear in place of the chart when employees in a department are of one gender.

 

Click the Designer button.

31. 

Drag and drop a Text Field element from the Palette onto the top of the chart in the Summary band. 

32. 

In the Report Inspector under the Summary band node, click the new Text Field item to select it.

33. 

Edit the Text Field properties to be font size 14 and bold.

 

In the Properties panel, click the Size dropdown button to activate the menu.

34. 

Click the 14 list item.

35. 

Click the Bold option.

36. 

Next, define the expression to display a message when there are females and no males in the department.

 

Click the Text Field Expression button.

37. 

Enter "$V{femaleCount} > 0 && $V{maleCount}==0 ? "All-Female Dept." : """ into the Text Field Expression field.

38. 

Note: This expression reads:

When there are females and no males in the department, then display the message All-Female Dept. Because no department in the sample data is all male, no similar message is necessary for an all-male department.

39. 

Click the OK button.

40. 

Note: The Text Field message is defined.

41. 

Next, configure a Print When expression for the text field so the system knows when to display it.

 

Click the Print When Expression button.

42. 

Enter "$V{maleCount}==0 || $V{femaleCount}==0" into the Print When Expression field.

43. 

Note: This expression configures the system to display the All-Female Dept. message when there are no males or no females in the department. Because no department is all male, the All-Female Dept. message cannot display erroneously. 

44. 

Click the OK button.

45. 

Click the Preview button.

46. 

Enter 2 into the field and click OK.

47. 

Note: The message prints in place of the chart, since Department 2 employs only females.

48. 

Click the Run again button, at the right of the Preview button.

49. 

Enter "1" into the department_ID field.

50. 

Click the OK button.

51. 

Note: The chart prints because Department 1 employs males and females.

52. 

Next, make sure the sub-report appears correctly when viewed as part of the parent report.

 

Click the Designer button.

53. 

Click the department_report.jrxml tab.

54. 

Click the Preview button.

55. 

Note: The updated department report appears in the preview window. The chart appears because Department 1 has both male and female employees.

56. 

Click the pagination arrow button to view page two.

57. 

Note: The alert message appears because Department 2 employs only females. For Department 3 down below, the bar chart displays.

58. 

Congratulations!

 

You created an expression that contains two fields, defined a Text Field expression containing a message, and created two Print When expressions so the system knows when to display the chart or a message.

End of Procedure.