Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default The <sup> tag doesn't work properly

    Greetings,

    see the SSCCE below; multiple <sup> tags don't work as I expected in a JEditorPane. The tags work fine in a browser. Is there anything I can do about it? I've been scrutinizing the API documentation but, as always, I got completely lost in that darn Swing documentation ...

    Java Code:
    import java.awt.BorderLayout;
    
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    
    
    
    public class T extends JFrame {
    	
    	private JEditorPane pane= new JEditorPane();
    	
    	public T() {
    		pane.setContentType("text/html");
    		// this line renders incorrect:
    		pane.setText("<html>a<sup>b<sup>c</sup></sup></html>");
    		
    		this.getContentPane().add(pane, BorderLayout.CENTER);
    		this.setSize(500, 500);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.pack();
    		this.setVisible(true);
    	}
    	
    	public static void main(String[] args) {
    
    		new T();
    	}
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: The <sup> tag doesn't work properly

    Doubt there's a great deal, unless there's another renderer somewhere.
    As a side note, I've got an instance of <sup> not working in Chrome.
    Just the one, which may end up being to do with the nesting of certain tags, but still...

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: The <sup> tag doesn't work properly

    Quote Originally Posted by Tolls View Post
    Doubt there's a great deal, unless there's another renderer somewhere.
    As a side note, I've got an instance of <sup> not working in Chrome.
    Just the one, which may end up being to do with the nesting of certain tags, but still...
    I find it sloppy; e.g. in an unrelated TextAttribute class the API is babbling about levels of super/sub scripts; and why is the HTML.Tag class unrelated to that other class to start with? And that Chrome browser sucks
    too :-P so there ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: The <sup> tag doesn't work properly

    Anyone would think they just made it up as they went along...

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: The <sup> tag doesn't work properly

    Quote Originally Posted by Tolls View Post
    Anyone would think they just made it up as they went along...
    Yep, that counts for the umptieth html specification and Chrome (and all those other darn browsers) as well. The sad thing is that the entire world depends on that crap.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: The <sup> tag doesn't work properly

    Quote Originally Posted by JosAH View Post
    multiple <sup> tags don't work as I expected in a JEditorPane. The tags work fine in a browser. Is there anything I can do about it?
    Depends. If all you need is a read-only display of multiple levels of superscripts, that's possible in a roundabout way.

    Quote Originally Posted by JosAH View Post
    I find it sloppy; e.g. in an unrelated TextAttribute class the API is babbling about levels of super/sub scripts; and why is the HTML.Tag class unrelated to that other class to start with?
    TextAttribute is part of java.awt.font; javax.swing.text is a completely different kettle of fish. I think the design weakness is in javax.swing.text.StyleConstants#setSuperscript(... ) which takes a boolean parameter -- not an int, which would have allowed designing the HTMLEditorKit to cater to multiple levels of superscript.

    Playing with this, I discovered that JLabel's preferredSize doesn't track the height needed to display superscripted font. Bad.

    Here's a crude example of displaying the double superscript using JLabels. I increased the font size so my old eyes could actually read the 'c'
    Java Code:
    import java.awt.*;
    import java.awt.font.TextAttribute;
    import java.util.HashMap;
    import java.util.Map;
    import javax.swing.*;
    
    public class SuperSuperscriptDemo {
    
      public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {
    
          @Override
          public void run() {
            new SuperSuperscriptDemo().makeUI();
          }
        });
      }
    
      public void makeUI() {
        JLabel a = new JLabel("a");
        Font baseFont = a.getFont();
        baseFont = baseFont.deriveFont(32F);
        a.setFont(baseFont);
        Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>();
    
        attributes.put(TextAttribute.SUPERSCRIPT, 1);
        Font superFont = baseFont.deriveFont(attributes);
        JLabel b = new JLabel("b");
        b.setFont(superFont);
    
        attributes.put(TextAttribute.SUPERSCRIPT, 2);
        Font superSuperFont = baseFont.deriveFont(attributes);
        JLabel c = new JLabel("c");
        c.setFont(superSuperFont);
    
        JPanel panel = new JPanel(new GridLayout(1, 0)) {
    
          @Override
          public Dimension getPreferredSize() {
            Dimension d = super.getPreferredSize();
            d.height += 10;
            return d;
          }
        };
        panel.add(a);
        panel.add(b);
        panel.add(c);
    
        JFrame frame = new JFrame();
        frame.setLayout(new GridBagLayout());
        frame.add(panel);
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: The <sup> tag doesn't work properly

    I think the design weakness is in javax.swing.text.StyleConstants#setSuperscript(... ) which takes a boolean parameter -- not an int
    ... and there isn't a setFont(...), just a setFontFamily(...) and setFontSize(...). Grrrr.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: The <sup> tag doesn't work properly

    I see what you're doing but I don't think it's an option for the rendering of html Strings. I just want a<sup>b<sup>c</sup></sup> to render properly ;-) I'm trying to use html (only) for the rendering of (simple) mathematical formulas (no TeX clones or images or whatsoever), but I'm nowhere yet ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: The <sup> tag doesn't work properly

    Give up and use one of the many LaTex or MathML renderers out there.
    :)

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: The <sup> tag doesn't work properly

    Quote Originally Posted by Tolls View Post
    Give up and use one of the many LaTex or MathML renderers out there.
    :)
    I don't know; html is fine for tables and decorated text and when I start using TeX or MathML I'm stuck with at least two renderers ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: The <sup> tag doesn't work properly

    I suppose so, but I still think you'd get better results quicker from Tex.
    It is designed for this sort of thing.

    Of course I speak as Someone Without A Clue...:)

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: The <sup> tag doesn't work properly

    Quote Originally Posted by Tolls View Post
    I suppose so, but I still think you'd get better results quicker from Tex.
    It is designed for this sort of thing.

    Of course I speak as Someone Without A Clue...:)
    I played with JMathTeX a bit: given a TexFormula object (that's easy to construct given a mathematical expression), it can deliver a TexIcon, an object that implements the Icon interface; it can easily be drawn, but now suppose I want to draw a formula next to a nice little table. A table can easily be drawn by html but it is not so easy to stick that TexIcon next to it. It's fiddling with two disjunct technologies ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: The <sup> tag doesn't work properly

    Output the image to a file then use an <img> tag...

    (I only said that to make you cringe)
    :D

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: The <sup> tag doesn't work properly

    shaddup ...

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: The <sup> tag doesn't work properly

    Woot! JavaFX WebView trumps JTextPane.
    Java Code:
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.web.WebView;
    import javafx.stage.Stage;
    
    public class Main extends Application {
    
      public static void main(String[] args) {
        Application.launch(Main.class, args);
      }
    
      @Override
      public void start(Stage primaryStage) {
        WebView webView = new WebView();
        webView.getEngine().loadContent("<html>a<sup>b<sup>c</sup></sup></html>");
    
        Scene scene = new Scene(webView, 300, 250);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Superscript test");
        primaryStage.show();
      }
    }
    The JavaFX DK comes bundled with JDK 7u2 and above.

    Here's the JavaFX documentation home: JavaFX 2 Documentation Home | JavaFX 2.0 Tutorials and Documentation

    db

    edit If you're married to Swing, WebView can be displayed in a JFXPanel.

    edit2 Hmm, married to swing

    edit3 Shortened the code
    Last edited by DarrylBurke; 02-14-2012 at 06:20 PM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: The <sup> tag doesn't work properly

    Quote Originally Posted by DarrylBurke View Post
    edit If you're married to Swing, WebView can be displayed in a JFXPanel.

    edit2 Hmm, married to swing
    Would that make him Frank Sinatra?

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: The <sup> tag doesn't work properly

    Looks like Jos doesn't like JavaFX :(

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: The <sup> tag doesn't work properly

    Quote Originally Posted by DarrylBurke View Post
    Looks like Jos doesn't like JavaFX :(
    I think I do but I feel a bit reluctant to add yet another complete framework/set of classes for just this. Originally I was hoping that just a few classes needed to be extended and fiddled with a bit to accomplish this stupid multi-level <sup> tag ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: The <sup> tag doesn't work properly

    I feel a bit reluctant to add yet another complete framework/set of classes for just this.
    The FX SDK is already bundled with the JDK. My guess is that it won't be long -- latest by Java SE 8 release -- before the FX runtime is bundled with the JRE and FX becomes a part of Core. And with the inclusion of closures in Java 8, FX, which is designed to use bindings, will trump Swing on one more front (it already uses a superior rendering engine, and supports CSS styling).

    Multi level <sup> isn't going to be possible with any set of classes that uses javax.swing.text.StyleConstants -- unless you extend it to add a setSuperscript(int) overload, and then extend all classes of interest -- probably HTMLDocument and/or HTMLEditorKit and a few other classes in javax.swing.text.html -- to make use of the new method. But, extending Swing stuff is dicey; the easiest of customizations are often stalled by the predominance of private and package-private stuff.

    Good luck!

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: The <sup> tag doesn't work properly

    I just went through (parts of) the FX API documentation; it looks promising; I think it'll be a major overhaul to change everything from Swing to those FX components though ... are you sure FX becomes part of the core set of Java? If so, that probably'll be the end of Swing ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Help, I can't make the JButton to work properly
    By furieux in forum New To Java
    Replies: 11
    Last Post: 01-07-2012, 05:13 PM
  2. actions dont work properly, calculator
    By lordjb in forum New To Java
    Replies: 14
    Last Post: 02-03-2011, 07:00 AM
  3. class that wont work out properly.
    By vampire-elf in forum New To Java
    Replies: 7
    Last Post: 09-07-2010, 01:39 AM
  4. Replies: 3
    Last Post: 05-06-2009, 12:11 AM
  5. Eclipse Documentation does not work properly.
    By krawetko in forum Eclipse
    Replies: 0
    Last Post: 10-05-2008, 10:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •