Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Re: how to convert html? Date: Wed, 30 Aug 2017 14:52:26 -0500 Organization: A noiseless patient Spider Lines: 77 Message-ID: References: <1d4eecd0-a437-4541-841a-4cba7fef7380@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 30 Aug 2017 19:48:06 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="6edda3de1ea69e8d7b59253f78ee9620"; logging-data="3908"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+QEBwQ4/cKczb3ruyw7FSt" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 In-Reply-To: Content-Language: en-US Cancel-Lock: sha1:U/xd0yeDOZM999NnJy9OLwwbz4Y= Xref: csiph.com comp.lang.java.programmer:37360 On 8/30/2017 13:41, Eric Douglas wrote: > Best I've found so far is > JTextPane jp = new JTextPane(); > jp.setContentType("text/html"); > jp.setText(myToolTipText); > jp.setSize(myPrintArea); > jp.paint(myPrintGraphics); > > This seems to be working except now it is just html text, which means I can't set the font, except to do this? > http://www.java2s.com/Tutorials/Java/Swing_How_to/JTextPane/Style_JTextPane_with_HTML_CSS_and_StyleSheet.htm This prints a JToolTip but I still don't know a way to get the actual tool tip from a component or to set a JToolTip on a component. import java.awt.*; import java.awt.event.*; import java.awt.print.*; import javax.swing.*; public class PrintableToolTip extends JToolTip implements Printable { public PrintableToolTip(String text) { super(); setTipText(text); } public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex == 0) { Graphics2D g = (Graphics2D)graphics; g.translate(pageFormat.getImageableX(),pageFormat.getImageableY()); paint(g); return Printable.PAGE_EXISTS; } else return Printable.NO_SUCH_PAGE; } public static void main(String... args) { EventQueue.invokeLater(() -> { JFrame f = new JFrame(); f.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.insets = new Insets(2,2,2,2); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); PrintableToolTip tip = new PrintableToolTip( "Line one
Underline one"); f.add(tip,c); JButton b = new JButton("Print"); b.addActionListener(event -> { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(tip,job.defaultPage()); if (job.printDialog()) try { job.print(); } catch (PrinterException pe) { pe.printStackTrace(); } }); f.add(b,c); f.pack(); f.setVisible(true); }); } } -- Knute Johnson