在我的一个基于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 ) );
}
}
@Robin回答基本正确,只是翻译成jasper说话:-)
"装饰器"是JRDataSource或(这里)JRRewindableDataSource的自定义实现。使其仅用于数据并基于表的RowSorter,类似于(注意:只是编译,而不是测试!)
public class JRTableSorterDataSource implements JRRewindableDataSource {
private RowSorter<? extends TableModel> sorter;
private int currentRow = -1;
private HashMap<String, Integer> columnNames = new HashMap<String, Integer>();
public JRTableSorterDataSource(RowSorter<? extends TableModel> sorter) {
if (sorter == null) return; // do nothing, no sorter
this.sorter = sorter;
TableModel tableModel = sorter.getModel();
if (tableModel != null) {
for (int i = 0; i < tableModel.getColumnCount(); i++) {
this.columnNames.put(tableModel.getColumnName(i),
Integer.valueOf(i));
}
}
}
@Override
public Object getFieldValue(JRField field) throws JRException {
String fieldName = field.getName();
Integer columnIndex = this.columnNames.get(fieldName);
return sorter.getModel().getValueAt(sorter.convertRowIndexToModel(currentRow), columnIndex.intValue());
}
@Override
public boolean next() throws JRException {
if (sorter == null || sorter.getModel() == null)
return false;
this.currentRow++;
return (this.currentRow < sorter.getViewRowCount());
}
@Override
public void moveFirst() throws JRException {
this.currentRow = -1;
}
protected int getColumnIndex(JRField field) throws JRException {
String fieldName = field.getName();
Integer columnIndex = this.columnNames.get(fieldName);
if (columnIndex != null) {
return columnIndex;
} else if (fieldName.startsWith("COLUMN_")) {
return Integer.parseInt(fieldName.substring(7));
}
throw new JRException("Unknown column name : " + fieldName);
}
}
然后在设置报告时使用它:
JRDataSource jr = new JRTableSorterDataSource(Dashboard.catalogTbl.getRowSorter());
/// ... same as your example
编辑
只是一个非常快速的可运行代码片段(懒得做一个完整的报告,忘记了这些文件是如何工作的;-)-所以在这里我们创建一个表(使用标准的SwingX模型),在其RowSorter上创建一个dataSource并循环遍历第一列的值,没有问题:
JTable table = new JXTable(new AncientSwingTeam());
JRDataSource source = new JRTableSorterDataSource(table.getRowSorter());
table.getRowSorter().toggleSortOrder(0);
JRField field = createField("First Name");
String firstNames = "First Name: ";
while (source.next()) {
firstNames += "n " + source.getFieldValue(field);
}
LOG.info(firstNames);
有点晚了,但我希望它能帮助别人:
在这段代码中,我所做的是在jTable1中应用过滤器之后,将获得的行放入辅助模型中。
然后将辅助模型分配给辅助表。这个表就是我要发送给JasperReports的。
//** jTable1 is the table in the jFrame where the data is loaded and I apply
//the RowFilter or RowSorter filters
DefaultTableModel dataModel_tableFiltered = null; //auxiliary model
JTable tableAuxiliary = null; //table where we will put the auxiliary model
public constructor {
dataModel_tableFiltered = new DefaultTableModel ();
// Set the number and name of the columns in the auxiliary model
for (int i = 0; i <jTable1.getColumnCount(); i ++) {
dataModel_tableFiltered.addColumn(jTable1.getColumnName (i));
}
tableAuxiliary = new JTable ();
}
private void btn_PrintActionPerformed (java.awt.event.ActionEvent evt) {
fillModel_filtered ();
try {
Map params = new HashMap ();
params.put ("nameCustomer", "**");
JRDataSource dataSource = new JRTableModelDataSource (tableAuxiliary.getModel ());
JasperPrint print = JasperFillManager.fillReport (reportPath, params, dataSource);
JasperViewer.viewReport (print, false); // true == Exit on Close
} catch (JRException ex) {
ex.printStackTrace ();
}
}
// Put resulting rows in the model after applying filters in jTable1
public void fillModel_filtered () {
dataModel_tableFiltered.setRowCount (0); // Empty rows of the model
for (int i = 0; i <jTable1.getRowCount (); i ++) {
Object row [] = new Object [jTable1.getColumnCount ()];
for (int j = 0; j <jTable1.getColumnCount (); j ++) {
row [j] = jTable1.getValueAt (i, j);
}
dataModel_tableFiltered.addRow (row);
}
tableAuxiliary.setModel(dataModel_tableFiltered); // Very Important
}