彩色打印页眉和页脚?

发布于 2024-07-20 05:31:51 字数 378 浏览 4 评论 0 原文

我正在尝试在打印 JTable 时创建彩色页眉和页脚。 具体来说,我正在查看 javax.swing.JTable 中的 getPrintable(),但 MessageFormat 没有提供指定页眉或页脚颜色的选项。

我该怎么做?

澄清 我有兴趣在打印时设置页眉/页脚。 例如,记事本将文件名作为标题附加到您打印的内容中。

更新 似乎没有标准方法可以做到这一点,有人可以给我一些解决方法吗? 到目前为止发布的唯一答案与打印(如发送到打印机,不显示到屏幕)页眉/页脚无关。

摘自我的评论:我对打印页眉/页脚感兴趣。 例如,当您从记事本打印文档时,它会将文件名附加为标题(或者可能是页脚,我不记得了)

I am trying to create colored headers and footers when printing a JTable. Specifically, I am looking at getPrintable() in javax.swing.JTable, but MessageFormat does not give me the option to specify the color of the header or footer.

How can I do it?

clarification
I am interested in setting the header/footers while printing. For example, notepad appends the filename as a header to what you print.

update
Seems like there is no standard way of doing this, can someone give me some workarounds? The only answer posted so far has nothing to do with printing(as in send to a printer, not displaying to screen) header/footers.

Copied from my comment: I am interested in the printing header/footer. For example, when you are printing a document from notepad, it appends the filename as a header (or perhaps its the footer, I do not remember exactly)

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

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

发布评论

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

评论(4

打小就很酷 2024-07-27 05:31:51

我能想到的一种解决方案是使用您自己的可打印:

public class CustomTablePrintable implements Printable {

    Printable tablePrintable;

    public void setTablePrintable(Printable printable) {
        tablePrintable = printable;        
    }

    public int print(Graphics graphics, PageFormat pageFormat, 
            int pageIndex) throws PrinterException {
        if (pageIndex > 0) {
            return NO_SUCH_PAGE;
        }

        tablePrintable.print(graphics, pageFormat, pageIndex);

        Graphics2D g2d = (Graphics2D)graphics;
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        // Draw header/footer here
        graphics.drawString(header, posx, posy);

        return PAGE_EXISTS;        
    }
}

当您从 JTable 调用 getPrintable 时,将其注入到自定义可打印的新实例中,然后将其与 PrinterJob 一起使用。

现在,您可以根据需要绘制页眉和页脚,但也会丢失一些内容:

  • 您无法使用 MessageFormat 来格式化消息。 我相信您可以轻松地将此功能添加到您的可打印文件中。
  • 页眉和页脚不会自动定位。 不过,您可以对这些进行粗略估计。

编辑:我查看了 Java 源代码,发现私有类 TablePrintable 可以完成所有工作。 您可以查看源代码以查看页眉和页脚是如何打印的。 然后您可以将此功能移至 Printable 类。

One solution I can think of is to use your own printable:

public class CustomTablePrintable implements Printable {

    Printable tablePrintable;

    public void setTablePrintable(Printable printable) {
        tablePrintable = printable;        
    }

    public int print(Graphics graphics, PageFormat pageFormat, 
            int pageIndex) throws PrinterException {
        if (pageIndex > 0) {
            return NO_SUCH_PAGE;
        }

        tablePrintable.print(graphics, pageFormat, pageIndex);

        Graphics2D g2d = (Graphics2D)graphics;
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        // Draw header/footer here
        graphics.drawString(header, posx, posy);

        return PAGE_EXISTS;        
    }
}

When you call getPrintable from your JTable, inject it to a new instance to the custom printable and then use this with the PrinterJob.

You can now draw the header and footer as you wish, but you also lose some stuff:

  • You can't use MessageFormat to format the messages. I believe that you could easily add this functionality to your printable.
  • Header and footer aren't automatically positioned. You could have rough estimates for these though.

EDIT: I've looked at the Java Sources and there is the private class TablePrintable that does all the job. You can peak at the source code to see how the header and footer are printed. Then you can move this functionality to your Printable class.

孤城病女 2024-07-27 05:31:51

这段代码主要来自www.java2s.com,并进行了修改以显示如何更改颜色。 这不是最漂亮的解决方案,但希望对您有所帮助。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class MainClass {
  public static void main(String args[]) {
    String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
    String headers[] = { "Upper", "Lower" };

    JFrame frame = new JFrame("Label Header");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);

    Border headerBorder = UIManager.getBorder("TableHeader.cellBorder");

    JLabel headerLabel1 = new JLabel(headers[0], JLabel.CENTER);
    headerLabel1.setBorder(headerBorder);

    // Here is where the color is changed.
    headerLabel1.setBackground(new Color(255, 0, 0));
    headerLabel1.setForeground(new Color(0, 0, 255));
    // End of color change.

    JLabel headerLabel2 = new JLabel(headers[1], JLabel.CENTER);
    headerLabel2.setBorder(headerBorder);

    TableCellRenderer renderer = new JComponentTableCellRenderer();

    TableColumnModel columnModel = table.getColumnModel();

    TableColumn column0 = columnModel.getColumn(0);
    TableColumn column1 = columnModel.getColumn(1);

    column0.setHeaderRenderer(renderer);
    column0.setHeaderValue(headerLabel1);

    column1.setHeaderRenderer(renderer);
    column1.setHeaderValue(headerLabel2);

    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}

class JComponentTableCellRenderer implements TableCellRenderer {
  public Component getTableCellRendererComponent(JTable table, Object value, 
      boolean isSelected, boolean hasFocus, int row, int column) {
    return (JComponent)value;
  }
}

最重要的是:

    // Here is where the color is changed.
    headerLabel1.setBackground(new Color(255, 0, 0));
    headerLabel1.setForeground(new Color(0, 0, 255));
    // End of color change.

请让我知道你的进展如何:)

This code is mainly from www.java2s.com with changes to show how to change the color. It is not the prettiest solution but hopefully will help you.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class MainClass {
  public static void main(String args[]) {
    String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
    String headers[] = { "Upper", "Lower" };

    JFrame frame = new JFrame("Label Header");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);

    Border headerBorder = UIManager.getBorder("TableHeader.cellBorder");

    JLabel headerLabel1 = new JLabel(headers[0], JLabel.CENTER);
    headerLabel1.setBorder(headerBorder);

    // Here is where the color is changed.
    headerLabel1.setBackground(new Color(255, 0, 0));
    headerLabel1.setForeground(new Color(0, 0, 255));
    // End of color change.

    JLabel headerLabel2 = new JLabel(headers[1], JLabel.CENTER);
    headerLabel2.setBorder(headerBorder);

    TableCellRenderer renderer = new JComponentTableCellRenderer();

    TableColumnModel columnModel = table.getColumnModel();

    TableColumn column0 = columnModel.getColumn(0);
    TableColumn column1 = columnModel.getColumn(1);

    column0.setHeaderRenderer(renderer);
    column0.setHeaderValue(headerLabel1);

    column1.setHeaderRenderer(renderer);
    column1.setHeaderValue(headerLabel2);

    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}

class JComponentTableCellRenderer implements TableCellRenderer {
  public Component getTableCellRendererComponent(JTable table, Object value, 
      boolean isSelected, boolean hasFocus, int row, int column) {
    return (JComponent)value;
  }
}

The most important bit is:

    // Here is where the color is changed.
    headerLabel1.setBackground(new Color(255, 0, 0));
    headerLabel1.setForeground(new Color(0, 0, 255));
    // End of color change.

HTH let me know how you got on with it :)

飘落散花 2024-07-27 05:31:51

这有点超出了常规,但是您可以使用像 iText 这样的库来创建PDF 文档,然后将其发送到打印机。 您将能够更好地控制文档的外观和演示文稿,包括管理表格的行如何突破页面边界的能力,并且您将能够非常轻松地设置页眉和页脚。

您可以按照以下说明在 Java 中打印 PDF: http://www.juixe.com/techknow/index.php/2008/01/17/print-a-pdf-document-in-java/

This is a little out of left-field, but you could use a library like iText to create a PDF document, and then send that to the printer. You'll get a much greater level of control over the appearance and presentation of the document, including the ability to manage how the rows of your table break over page boundaries, and you'll be able to set the page header and footer very readily.

You can print a PDF in Java following these instructions: http://www.juixe.com/techknow/index.php/2008/01/17/print-a-pdf-document-in-java/

长安忆 2024-07-27 05:31:51

这很容易:

JTable table = new JTable();
            ((DefaultTableCellRenderer)table.getTableHeader().getColumnModel().getColumn(4).getHeaderRenderer()).setForeground(Color.ORANGE);

It's easy:

JTable table = new JTable();
            ((DefaultTableCellRenderer)table.getTableHeader().getColumnModel().getColumn(4).getHeaderRenderer()).setForeground(Color.ORANGE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文