如何开始使用Dynamic Jasper

时间:2018-08-03 16:06:47

标签: java jasper-reports dynamic-jasper

动态碧玉入门时遇到问题。我有一个具有表的控制器类,并希望使用动态Jasper将表转换为快速报告,以便在给定当前内容的情况下可以将其打印出来。有人知道如何使用Dynamic Jasper吗?我阅读了文档,但不太了解。我在printAction函数中包含了jasper代码,以便一旦按下它就可以从表中打印出内容。但是,我不知道在给我的课程后我该从哪里得到"branch"String.class.getName()?我必须首先使用jasper创建一个jrxml文件吗?我如何基本上实现代码,使其起作用? printAction中的代码是在其网站(http://dynamicjasper.com/documentation-examples/getting-started/)上找到的示例代码。希望有人知道如何使用它。谢谢。

我的控制器类:

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     
    }

}

个人资料模型

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;
    }   
}

0 个答案:

没有答案