I have a problem getting started with dynamic jasper. I have a controller class that has a table and would like to convert the table to a fast report using dynamic Jasper so that I can print it out given the current content. Would anybody know how to use Dynamic Jasper? I read the documentation but can't quite understand it. I included my jasper code in the printAction
function so that it can print out content from the table once pressed. However, I don't understand where do I get "branch"
and String.class.getName()
given my classes? Do I have to create a jrxml file first with jasper? How do I basically implement the code so that it will work? The code from printAction
is the sample code found on their website (http://dynamicjasper.com/documentation-examples/getting-started/). Hope somebody knows how to use this. Thank you.
My controller class:
public class ProfileController implements Initializable {
@FXML
private JFXTextField name, age;
@FXML
private TableView<Profile> table;
@FXML
private TableColumn<Profile, String> nameColumn;
@FXML
private TableColumn<Profile, String> ageColumn;
ProfileDAO profileDAO = new ProfileDAO();
@Override
public void initialize(URL url, ResourceBundle rb) {
//prepare cells
nameColumn.setCellValueFactory(cellData -> cellData.getValue().profileNameProperty());
ageColumn.setCellValueFactory(cellData -> cellData.getValue().profileAgeProperty());
try() {
table.setItems(profileDAO.search())
}catch(){
}
}
@FXML
private void printAAction() {
FastReportBuilder drb = new FastReportBuilder();
DynamicReport dr = drb.addColumn("State", "state", String.class.getName(),30)
.addColumn("Branch", "branch", String.class.getName(),30)
.addColumn("Product Line", "productLine", String.class.getName(),50)
.addColumn("Item", "item", String.class.getName(),50)
.addColumn("Item Code", "id", Long.class.getName(),30,true)
.addColumn("Quantity", "quantity", Long.class.getName(),60,true)
.addColumn("Amount", "amount", Float.class.getName(),70,true)
.addGroups(2)
.setTitle("November 2006 sales report")
.setSubtitle("This report was generated at " + new Date())
.setPrintBackgroundOnOddRows(true)
.setUseFullPageWidth(true)
.build();
JRDataSource ds = new JRBeanCollectionDataSource(TestRepositoryProducts.getDummyCollection());
JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
JasperViewer.viewReport(jp); //finally display the report report
}
}
Profile Model
public class Profile {
private final SimpleStringProperty profileName;
private final SimpleStringProperty profileAge;
public Profile() {
this("", "");
}
public Profile(String profileName, String profileAge) {
this.profileName = new SimpleStringProperty(profileName);
this.profileAge = new SimpleStringProperty(profileAge);;
}
//getter
public String getProfileName() {
return profileName.get()
}
public String getProfileAge() {
return profileAge.get()
}
//setter
public void setProfileName(String profileName) {
this.profileName.set(profileName);
}
public void setProfileAge(String profileAge){
this.profileAge.set(profileAge);
}
//property
public StringProperty profileNameProperty(){
return profileName;
}
public StringProperty profileAgeProperty() {
return profileAge;
}
}
ProfileDAO
ublic class ProfileDAO {
public ObservableList<Profile> search() throws SQLException,ClassNotFoundException {
//declare PreparedStatement & ResultSet
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//Declare a SELECT statement
String selectQuery = "SELECT * FROM profiles";
//Execute SELECT statement
try {
//initializing PreparedStatement
preparedStatement = connection.prepareStatement(selectQuery);
//initializing ResultSet via SqliteConnection method
resultSet = preparedStatement.executeQuery();
ObservableList<Profile> profileList = getProfileList(resultSet);
//Return Profile object
return titlesList;
} catch (SQLException e) {
System.out.println("error " + e);
//Return exception
throw e;
}
finally {
if(preparedStatement != null)
{
preparedStatement.close();
}
if(resultSet != null)
{
resultSet.close();
}
}
}
private static ObservableList<Profile> getProfileList(ResultSet resultSet) throws SQLException, ClassNotFoundException {
//Declare a observable List
ObservableList<Profile> profileList = FXCollections.observableArrayList();
while (resultSet.next()) {
Profile profile = new Profile();
profile.setProfileName(resultSet.getInt("name"));
profile.setProfileAge(resultSet.getString("age"));
//Add to ObservableList
profileList.add(profile);
}
//return empList (ObservableList of Employees)
return titlesList;
}
}