从 JTable 打印用户指定的列

发布于 2024-08-29 23:28:01 字数 357 浏览 6 评论 0原文

我有一个应用程序,我想打印一个 JTable,但由于它有很多列,我希望用户选择/限制要打印的列,以便它可以适合常规打印纸。

我正在使用 JTable.print() 函数进行打印。 (http://java.sun.com/docs/books /tutorial/uiswing/misc/printtable.html) 现在,我的解决方案是使用用户选择的列创建另一个 JTable,然后用数据重新填充表,然后将其发送到打印机。

有更好的方法吗?

I have an application, and I would like to print a JTable, but since it has many columns, I would like user to select/limit which columns to print so it can fit in regular printer paper.

I'm using JTable.print() function to print. (http://java.sun.com/docs/books/tutorial/uiswing/misc/printtable.html)
Right now, my solution, is to create another JTable with the columns that user selected, then repopulate the table with the data, then send it to printer.

Is there a better way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫瑟鸿黎 2024-09-05 23:28:01

正如我在此处回答 ,我通过在打印之前隐藏列并在打印后恢复列来解决这个问题:

// get column num from settings
int num = gridSettings.getColumnsOnPage();// first <num> columns of the table will be printed   

final TableColumnModel model = table.getColumnModel();

// list of removed columns. After printing we add them back
final List<TableColumn> removed = new ArrayList<TableColumn>();

int columnCount = model.getColumnCount();

// hiding columns which are not used for printing
for(int i = num; i < columnCount; ++i){
    TableColumn col = model.getColumn(num);
    removed.add(col);
    model.removeColumn(col);
}                                                   

// printing after GUI will be updated
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        // table printing
        try {
            table.print(PrintMode.FIT_WIDTH, null, null, true, hpset, true); // here can be your printing
        } catch (PrinterException e) {
            e.printStackTrace();
        }

        // columns restoring
        for(TableColumn col : removed){
            model.addColumn(col);
        }
    }
});

对于打印 JTable 的特定部分,只需稍微更改一下此代码即可。

As I've answered here, I solved it by hiding columns before printing and restoring columns after printing:

// get column num from settings
int num = gridSettings.getColumnsOnPage();// first <num> columns of the table will be printed   

final TableColumnModel model = table.getColumnModel();

// list of removed columns. After printing we add them back
final List<TableColumn> removed = new ArrayList<TableColumn>();

int columnCount = model.getColumnCount();

// hiding columns which are not used for printing
for(int i = num; i < columnCount; ++i){
    TableColumn col = model.getColumn(num);
    removed.add(col);
    model.removeColumn(col);
}                                                   

// printing after GUI will be updated
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        // table printing
        try {
            table.print(PrintMode.FIT_WIDTH, null, null, true, hpset, true); // here can be your printing
        } catch (PrinterException e) {
            e.printStackTrace();
        }

        // columns restoring
        for(TableColumn col : removed){
            model.addColumn(col);
        }
    }
});

For printing specific part of a JTable, just change a little bit this code.

一身仙ぐ女味 2024-09-05 23:28:01

最好的方法:只需将列宽设置为零即可隐藏:)

TableColumn column = table.getColumnModel().getColumn(i);

//backup first
int minWidth=column.getMinWidth();
int width=column.getPreferredWidth();

//to hide:
column.setMinWidth(0);
column.setPreferredWidth(0);

//to show it back:
column.setMinWidth(minWidth);
column.setPreferredWidth(width);

The best way: just set column width to zero then it will be hidden:)

TableColumn column = table.getColumnModel().getColumn(i);

//backup first
int minWidth=column.getMinWidth();
int width=column.getPreferredWidth();

//to hide:
column.setMinWidth(0);
column.setPreferredWidth(0);

//to show it back:
column.setMinWidth(minWidth);
column.setPreferredWidth(width);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文