java - JTable排序和Jasper报告输出

标签 java swing jtable jasper-reports

在我的一个基于 Java swing 桌面的应用程序中,我使用 JTable 并添加了列的排序功能。我需要根据用户添加的数值对其进行排序,然后获得 jasper 报告输出。

当前在排序并打印报告后,报告未显示排序顺序。但顺序时的值是从DB中取出的。如何打印用户表格排序的报告?

这是我的 jasper 报告生成代码

try {
    DefaultTableModel de = (DefaultTableModel)Dashboard.catalogTbl.getModel();
    JRTableModelDataSource jr = new JRTableModelDataSource(de);
    String reportName = reportPath + "reports/AuctionSale/Catalogue/catalouge_frm_tbl.jrxml";
    String compiledName = reportPath + "reports/AuctionSale/Catalogue/catalouge_frm_tbl.jasper";
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("Lot_No", "Lot No");
    params.put("Mark", "Mark");
    params.put("Invoice", "Invoice");
    params.put("Grade", "Grade");
    params.put("Weight", "Weight");
    params.put("Price", "Price");
    params.put("Buyer", "Buyer");
    JasperCompileManager.compileReportToFile(reportName, compiledName);
    JasperPrint jasperPrint = JasperFillManager.fillReport(compiledName, params, jr);
    JasperViewer.viewReport(jasperPrint, false);
} catch (Exception e) {
    e.printStackTrace();
}

最佳答案

我猜报告是基于 TableModel 生成的,而典型的排序仅影响 JTable 本身,而不影响模型。

您可以做的是装饰您传递给报告生成器的表模型,以便它接管 JTable 的排序。风格类似

public class TableModelDecorator implements TableModel{
  private TableModel delegate;
  private JTable table;

  @Override
  public Object getValueAt( int rowIndex, int columnIndex ) {
    return delegate.getValueAt( table.convertRowIndexToView( rowIndex ), table.convertColumnIndexToView( columnIndex ) );
  }
}

然后是所有相关方法。

关于java - JTable排序和Jasper报告输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8396078/

相关文章:

java - 自定义 AbstractTableModel 无法正确显示数据

Java Swing : get index of combobox inside table

java - 创建 Blob 时出现运行时异常

java - 我生成了如下代码,但重置按钮未在开始和完成按钮之间显示

java - 高尔顿板的java实现

Java Eclipse : ComboBox action button based on item selected (e. g.,温度转换)

swing - JTable 输入验证器

java - okHttp NetworkOnMainThreadException 尽管有新线程

java - Hibernate - TableView 绑定(bind)

java - GridLayout 未在 JFrame 中显示