🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #12903

Need help Printing a JTextPane

#12903
From: Edward Carr
Date: Fri, 16 Oct 1998 00:00
91 lines
2695 bytes
I am trying to print the HTML contents of a JTextPane to a printer. The
following code has a file spooled to the printer, which looks like it
contains actual data due to the size being about 328 bytes, and is then
deleted as if it has been printed.

The printer, however, does not print a thing.

Anyone have any ideas?

Here is the code that I used to print:
----------------------------------------------------------------------------
---------------------------------------
package Test;

import com.sun.java.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class TestPrint extends JFrame
{
  JTextPane jTextPane1 = new JTextPane();
  JPanel jPanel1 = new JPanel();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();

  public TestPrint()
  {
    try
    {
      jbInit();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception
  {
    jTextPane1.setContentType("text/html");
    jTextPane1.setText("Testing");
    jButton1.setText("Print");
    jButton1.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        jButton1_actionPerformed(e);
      }
    });
    jButton2.setText("Cancel");
    jButton2.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        jButton2_actionPerformed(e);
      }
    });
    this.getContentPane().add(jTextPane1, BorderLayout.CENTER);
    this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
    jPanel1.add(jButton1, null);
    jPanel1.add(jButton2, null);
  }

  void jButton2_actionPerformed(ActionEvent e)
  {
    dispose();
    System.exit(0);
  }

  void jButton1_actionPerformed(ActionEvent e)
  {
    PrintJob pj = jTextPane1.getToolkit().getPrintJob(this, "Test", null);
    if(pj != null)
    {
      Graphics pg = pj.getGraphics();
      if(pg != null)
      {
        jTextPane1.paint(pg);
        pg.dispose();
      }
      pj.end();
    }
  }

  public static void main(String[] args)
  {
    TestPrint main = new TestPrint();
    main.show();
  }
}
Technical Details
Message-ID: <707uh6$s1o6@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,c7c37ec4bc7c2f6d,start
X-Google-Attributes: gid111d45,public
From: "Edward Carr" <apollo@fast.net>
Subject: Need help Printing a JTextPane
Date: 1998/10/16
Message-ID: <707uh6$s1o6@forums.borland.com>
X-Deja-AN: 401857026
Organization: Another Netscape Collabra Server User
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Newsgroups: borland.public.jbuilder.java.swing