Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #37354 > unrolled thread

how to convert html?

Started byEric Douglas <e.d.programmer@gmail.com>
First post2017-08-30 07:03 -0700
Last post2017-08-31 13:37 -0700
Articles 12 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  how to convert html? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-30 07:03 -0700
    Re: how to convert html? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-08-30 10:59 -0500
      Re: how to convert html? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-30 11:41 -0700
        Re: how to convert html? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-08-30 14:52 -0500
          Re: how to convert html? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-30 14:00 -0700
            Re: how to convert html? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-31 05:47 -0700
              Re: how to convert html? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-31 05:56 -0700
                Re: how to convert html? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-31 07:12 -0700
                  Re: how to convert html? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-08-31 13:20 -0500
                    Re: how to convert html? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-31 11:49 -0700
                      Re: how to convert html? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-08-31 14:52 -0500
                        Re: how to convert html? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-31 13:37 -0700

#37354 — how to convert html?

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-08-30 07:03 -0700
Subjecthow to convert html?
Message-ID<1d4eecd0-a437-4541-841a-4cba7fef7380@googlegroups.com>
I am generating a printout from a GUI screen.  Now I want to see if I can somehow incorporate the tooltips.
I can call setToolTipText on my components and the Java somehow converts this.  Text may be "tool tip text" or may be "<html>line one<br/>line two</html>".
I can't see where it converts it but it does display the html text simply as 2 lines of plain text.
I tried searching for how to make my tooltips into printable text as you see on the screen and can't find anything easy, so I'm either hard coding to parse out the html strings, or including a third party html jar to run it through like JTidy or JSoup.  Somehow this is built into tooltips, so there has to be an easier way.

[toc] | [next] | [standalone]


#37357

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2017-08-30 10:59 -0500
Message-ID<oo6n5i$i41$1@dont-email.me>
In reply to#37354
On 8/30/2017 09:03, Eric Douglas wrote:
> I am generating a printout from a GUI screen.  Now I want to see if I can somehow incorporate the tooltips.
> I can call setToolTipText on my components and the Java somehow converts this.  Text may be "tool tip text" or may be "<html>line one<br/>line two</html>".
> I can't see where it converts it but it does display the html text simply as 2 lines of plain text.
> I tried searching for how to make my tooltips into printable text as you see on the screen and can't find anything easy, so I'm either hard coding to parse out the html strings, or including a third party html jar to run it through like JTidy or JSoup.  Somehow this is built into tooltips, so there has to be an easier way.
> 

I didn't come up with any good solution to what I think you want to do 
but you could create a new JToolTip (with your html text), display that 
in some container and print it.  I don't know how to get a hold of the 
default tool tip on a JComponent.  What would be cool is if there was a 
way to trigger all of the tool tips in an application at once.

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#37359

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-08-30 11:41 -0700
Message-ID<a53e0873-5233-4509-8099-b7c57b7c974e@googlegroups.com>
In reply to#37357
On Wednesday, August 30, 2017 at 12:00:18 PM UTC-4, Knute Johnson wrote:
> 
> I didn't come up with any good solution to what I think you want to do 
> but you could create a new JToolTip (with your html text), display that 
> in some container and print it.  I don't know how to get a hold of the 
> default tool tip on a JComponent.  What would be cool is if there was a 
> way to trigger all of the tool tips in an application at once.
> 
> -- 
> 
> Knute Johnson

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

[toc] | [prev] | [next] | [standalone]


#37360

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2017-08-30 14:52 -0500
Message-ID<oo74pl$3q4$1@dont-email.me>
In reply to#37359
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(
              "<html>Line one<br><u>Underline one</u></html>");

             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

[toc] | [prev] | [next] | [standalone]


#37361

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-08-30 14:00 -0700
Message-ID<8e62a8c5-8a8c-4f32-96a2-adbd437a9726@googlegroups.com>
In reply to#37360
On Wednesday, August 30, 2017 at 3:52:47 PM UTC-4, Knute Johnson wrote:
> 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(
>               "<html>Line one<br><u>Underline one</u></html>");
> 
>              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

This is interesting...
That works as expected.  Now, in my case I don't need it to be printable.  I already have a custom class doing the printing.

So, I create this object inside a generic wrapper which extends JComponent.
protected JToolTip jt = new JToolTip();

Then, inside my print method where I've already established a g which is the print graphics.
jt.setTipText(htmlText);
setSize(jt.getPreferredSize());
jt.setSize(jt.getPreferredSize());
jt.paint(g);

This sizes it and paints it as expected...but the font is all wrong by default.
I try calling jt.setFont(mycustomfont).  Then I can call jt.getFont() and it returns what I passed in, but it still doesn't look right on my printout.
Now I call these 3 statements before I paint it which don't appear to be linked to the tooltip, but I had started the setup to customize the font for the JTextPane, and the font looks correct.  I have no idea why.
StyleSheet styleSheet = new StyleSheet();
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
htmlEditorKit.setStyleSheet(styleSheet);

[toc] | [prev] | [next] | [standalone]


#37365

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-08-31 05:47 -0700
Message-ID<0df9b410-65f7-4064-8748-f35691ef7355@googlegroups.com>
In reply to#37361
On Wednesday, August 30, 2017 at 5:01:18 PM UTC-4, Eric Douglas wrote:
> This sizes it and paints it as expected...but the font is all wrong by default.
> I try calling jt.setFont(mycustomfont).  Then I can call jt.getFont() and it returns what I passed in, but it still doesn't look right on my printout.
> Now I call these 3 statements before I paint it which don't appear to be linked to the tooltip, but I had started the setup to customize the font for the JTextPane, and the font looks correct.  I have no idea why.
> StyleSheet styleSheet = new StyleSheet();
> HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
> htmlEditorKit.setStyleSheet(styleSheet);

So that was really weird, no idea why it was painting properly only when I tried to setup a style sheet.  I did have it embedded in a custom component designed to draw images, thinking at some point the html would have to be painted as an image.  So I took it out and put it in a new control and it works better now.  Still have no idea how JToolTip handles html to simply draw the text string(s) myself but it works just telling a JToolTip to draw it for me so I'll do that.  I just have to create this control, assign it the text, and ask it for it's preferred size if I just want to know how many characters across, how many lines the text will take on the page.

[toc] | [prev] | [next] | [standalone]


#37367

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-08-31 05:56 -0700
Message-ID<804837be-8404-48df-8f3c-b6a1672e3a3a@googlegroups.com>
In reply to#37365
On Thursday, August 31, 2017 at 8:47:23 AM UTC-4, Eric Douglas wrote:
> > Now I call these 3 statements before I paint it which don't appear to be linked to the tooltip, but I had started the setup to customize the font for the JTextPane, and the font looks correct.  I have no idea why.
> > StyleSheet styleSheet = new StyleSheet();
> > HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
> > htmlEditorKit.setStyleSheet(styleSheet);
> 
> So that was really weird, no idea why it was painting properly only when I tried to setup a style sheet.  I did have it embedded in a custom component designed to draw images, thinking at some point the html would have to be painted as an image.  So I took it out and put it in a new control and it works better now.  Still have no idea how JToolTip handles html to simply draw the text string(s) myself but it works just telling a JToolTip to draw it for me so I'll do that.  I just have to create this control, assign it the text, and ask it for it's preferred size if I just want to know how many characters across, how many lines the text will take on the page.

Actually, I forgot, I am still creating the style sheet somewhere.  It doesn't look right if you don't do this, no idea why.

[toc] | [prev] | [next] | [standalone]


#37368

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-08-31 07:12 -0700
Message-ID<8a035a47-e940-4153-b595-c3a65da887ec@googlegroups.com>
In reply to#37367
On Thursday, August 31, 2017 at 8:56:45 AM UTC-4, Eric Douglas wrote:
> 
> Actually, I forgot, I am still creating the style sheet somewhere.  It doesn't look right if you don't do this, no idea why.

I'm getting there...
javax.swing.plaf.basic.BasicToolTipUI.paint
...
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
        if (v != null) {
            v.paint(g, paintTextR);
        } else {
            g.setFont(font);
            SwingUtilities2.drawString(c, g, tipText, paintTextR.x,
                                  paintTextR.y + metrics.getAscent());
        }

So if I call setText on my JToolTip with a plain string then call this getClientProperty it returns null.  If I call setText, string starting with <html> then call this getClientProperty, it returns a javax.swing.plaf.basic.BasicHTML$Renderer.

So I trace that to the source
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/swing/plaf/basic/BasicHTML.java?av=f

and find that I can just call setFont() on my JToolTip, then for html font I call BasicHTML.updateRenderer(myToolTip,myToolTip.getTipText()).  That uses that style sheet logic and updates it from the JToolTip's font, except it doesn't do underlines..

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/swing/SwingUtilities2.java#SwingUtilities2.displayPropertiesToCSS%28java.awt.Font%2Cjava.awt.Color%29

[toc] | [prev] | [next] | [standalone]


#37370

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2017-08-31 13:20 -0500
Message-ID<oo9job$hnv$1@dont-email.me>
In reply to#37368
It is really unclear what you are trying to do.  You don't want to print 
the text from a JToolTip or you do?  You want to use css styles with a 
JToolTip, that works just fine.  What is it that you are really trying 
to do?  A JLabel will display HTML text too.

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#37371

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-08-31 11:49 -0700
Message-ID<c20a9a60-2ac5-4332-bd53-d578957d7d00@googlegroups.com>
In reply to#37370
On Thursday, August 31, 2017 at 2:20:20 PM UTC-4, Knute Johnson wrote:
> It is really unclear what you are trying to do.  You don't want to print 
> the text from a JToolTip or you do?  You want to use css styles with a 
> JToolTip, that works just fine.  What is it that you are really trying 
> to do?  A JLabel will display HTML text too.
> 
> -- 
> 
> Knute Johnson

Yes, I have a custom JTree with a custom print class which dynamically creates reports.  I set up a tree report class to turn any custom tree into a report.  This prints the icon and text of every node with indents.  Now I'm printing the node tooltips.

So I discovered they're using BasicHTML.createHTMLView to paint a JToolTip if it has <html> in it. (I don't know if they're specifically looking for that tag or how they know it's html) If there's no html it does a straight up drawString.

So I have a JToolTip called jt and I want to set the font I call jt.setFont() updates how it draws that plain string.  Then to update the html font I call BasicHTML.updateRenderer(jt,jt.getTipText()), and to get the html to properly use the font I had to put a static initializer on my class to call new HTMLEditorKit().setStyleSheet(new StyleSheet());.  To update the plain text background I can call jt.setBackground(Color.blue), but to update the html background I call new HTMLEditorKit().getStyleSheet().addRule("body {background:blue}").

The dynamic report object does everything.  It uses standard fixed width font embedded in the jar so I can dynamically specify how many character to print across the page and specify where to print text anywhere on the page by column/row.  It puts that information in a serializable object I can use repeatedly, to draw on a printer's graphics, draw a PDF using iText graphics, or draw on the screen using a JPanel in a JScrollPane in a JFrame.

Anyway the consistent display was tricky because JToolTip renders plain string different than html string, some tooltips have html some don't, and the html/css doesn't seem clearly documented.

[toc] | [prev] | [next] | [standalone]


#37372

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2017-08-31 14:52 -0500
Message-ID<oo9p5k$4c5$1@dont-email.me>
In reply to#37371
On 8/31/2017 13:49, Eric Douglas wrote:
> On Thursday, August 31, 2017 at 2:20:20 PM UTC-4, Knute Johnson wrote:
>> It is really unclear what you are trying to do.  You don't want to print
>> the text from a JToolTip or you do?  You want to use css styles with a
>> JToolTip, that works just fine.  What is it that you are really trying
>> to do?  A JLabel will display HTML text too.
>>
>> -- 
>>
>> Knute Johnson
> 
> Yes, I have a custom JTree with a custom print class which dynamically creates reports.  I set up a tree report class to turn any custom tree into a report.  This prints the icon and text of every node with indents.  Now I'm printing the node tooltips.
> 
> So I discovered they're using BasicHTML.createHTMLView to paint a JToolTip if it has <html> in it. (I don't know if they're specifically looking for that tag or how they know it's html) If there's no html it does a straight up drawString.
> 
> So I have a JToolTip called jt and I want to set the font I call jt.setFont() updates how it draws that plain string.  Then to update the html font I call BasicHTML.updateRenderer(jt,jt.getTipText()), and to get the html to properly use the font I had to put a static initializer on my class to call new HTMLEditorKit().setStyleSheet(new StyleSheet());.  To update the plain text background I can call jt.setBackground(Color.blue), but to update the html background I call new HTMLEditorKit().getStyleSheet().addRule("body {background:blue}").
> 
> The dynamic report object does everything.  It uses standard fixed width font embedded in the jar so I can dynamically specify how many character to print across the page and specify where to print text anywhere on the page by column/row.  It puts that information in a serializable object I can use repeatedly, to draw on a printer's graphics, draw a PDF using iText graphics, or draw on the screen using a JPanel in a JScrollPane in a JFrame.
> 
> Anyway the consistent display was tricky because JToolTip renders plain string different than html string, some tooltips have html some don't, and the html/css doesn't seem clearly documented.
> 

In that case I think I would be tempted to only use HTML in the 
JToolTips and then they will all render the same.  You can put the CSS 
styles in the HTML if you want and skip all the HTMLEditorKit and 
StyleSheet stuff.

The CSS version is quite old and limited in Swing and I don't know that 
it will ever be updated.  I love Swing but it is showing its age.

I'm curious though have you figured out a way to get at the actual 
JToolTip from a JComponent?

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#37373

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-08-31 13:37 -0700
Message-ID<b85fd7a1-9f9d-4c01-8dae-15a1dbe35ebe@googlegroups.com>
In reply to#37372
On Thursday, August 31, 2017 at 3:52:48 PM UTC-4, Knute Johnson wrote:
> In that case I think I would be tempted to only use HTML in the 
> JToolTips and then they will all render the same.  You can put the CSS 
> styles in the HTML if you want and skip all the HTMLEditorKit and 
> StyleSheet stuff.
> 
> The CSS version is quite old and limited in Swing and I don't know that 
> it will ever be updated.  I love Swing but it is showing its age.
> 
> I'm curious though have you figured out a way to get at the actual 
> JToolTip from a JComponent?
> 
> -- 
> 
> Knute Johnson

If you set the matching properties as I mentioned, if they're not rendering exactly the same I think they're close enough.  I didn't look at the flag they're using to determine if the text is html but it seems ugly to force html tips where they're not needed.

The stylesheet stuff sounds cleaner than intercepting every setToolTipText to force it into html code.  This css sets a default font and a default background each tooltip may override if they need something special.

I've heard people say before that Swing is old as if it's outdated but I haven't found anything I really can't do with it.  Of course it is extremely modern compared to what we started with 17 years ago.  I've been working on an API for about 10 years.  I'd love to show you what it can do.  I've extended every Swing class I could find a use for and made them do so much more.  I have quite a long list now of what I can do with a JTable.  One thing Swing hasn't been good at is rendering a full scale web site, but it does have a way to integrate a Java FX component for that, and I haven't had much use for that.  The limited html/css functionality in the tool tips and other components, as you say the JLabel and of course the JTextPane, seems sufficient for a business application.

Other than instantiating a JToolTip for rendering the tool tip text on a report I'm not sure what you'd need to access the actual JToolTip from a JComponent for.  I'm sure that functionality is there if you dig enough but why.  I did customize JPopupMenu, made the regular list menu scrollable with an adjustable max visible items, and made one display a calendar for a date input inquiry...

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web