Results 1 to 5 of 5
  1. #1
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default JEditorPane, HTML 4.0, CSS, custom Tags

    Hello!

    I recently discovered the HTML Feature of JTextPane and JEditorPane. This features comes in handy for one problem I have. I have to highlight some areas/words in a text and I thought of HTML.

    But there is a problem: as we all want to use standards I wanted to use css for styling. And I thought about a custom tag in the text. And there is a problem:

    The code below does not render the text properly
    However, the browser (firefox) renders the content right. and of course i can use the <font> tag instead, but this is deprecated. How to solve this problem?

    Thanks in advance,
    Andre

    Java Code:
    private static String webContent = "<html>" +
    			"<head>" +
    			"<style type=\"text/css\">mytag {color:red}</style>" +
    			"</head>" +
    			"<body>" +
    			"<p>Hello <mytag>World</mytag></p>" +
    			"</body>" +
    			"</html>";
    
    	public static void main(String[] args) {
    		JFrame f = new JFrame("HTML Example");
    
    		JEditorPane jep = new JEditorPane();
    		jep.setEditorKit(new HTMLEditorKit());
    		jep.setText(webContent);
    
    		f.add(jep);
    
    		f.pack();
    		f.setSize(300, 200);
    		f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    		f.setVisible(true);
    	}
    Last edited by AndreB; 12-21-2009 at 05:40 PM.

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Sorry, Java's HTML support does not deal with CSS.
    But it has quite a few capabilities.
    I was able to get the following to work.
    Note that the <body> tag is not needed.
    Java Code:
    "<html>"
    	+ "<img src='http://physpics.com/images/twomugs-small.gif'/><br/>"
    	+ "<ul><li><div align='center'>"
    	+ "<font color='red' size='6' face='verdana,helvetica'>red</font>"
    	+ "</div></li>"
    	+ "<li><table><tr><td bgcolor='yellow'>"
    	+ "<font color='green' size='4' face='verdana,helvetica'>green on yellow</font>"
    	+ "</td></tr></table></li></ul>"
    	+ "</html>"

  3. #3
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    Thanks!
    It's a pity :-( ...
    First you got this candy an then its salt and pepper ;-)

    Well the font tag will do. But are there some libraries which do the right thing? I thinking about 3rd party swing widget that have the desired functionality.

    Greetz

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Yecch, did I get that one wrong.

    Stylesheets are supported in JTextComponents via HTMLEditorKit.
    Newly invented tags don't work, at least not out of the box.
    And styles don't work in Labels and buttons.

    The code below outputs this, with a JLabel on the left and a JEditorPane on the right.

    Java Code:
    package Test;
    
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.Document;
    import javax.swing.text.html.HTMLEditorKit;
    import javax.swing.text.html.StyleSheet;
    
    public class TestHTML extends JFrame {
    	public TestHTML() {
    		super("Test HTML");
    
    		String htmlText = "<html>"
    				+ "<img src='http://physpics.com/images/twomugs-small.gif'/><br/>"
    				+"<h2>h2</h2>"
    				+ "<div class='mytag'>div class=mytag,  .mytag selector</div>"
    				+ "<div id='mytag'>div id=mytag,  #mytag selector</div>"
    // these two don't work
    				+ "<mytag>mytag,  selector mytag</mytag>"   //   displays the tags
    				+ "<div mytag='mytag'>div mytag=mytag, selector [mytag]</div>"
    
    				+ "<ul><li><div align='center'>"
    				+ "<font color='red' size='6' face='verdana,helvetica'>red</font>"
    				+ "</div></li>"
    				+ "<li><div style='text-align: right; color: blue; font-weight: bold;'>"
    				+ "blue"
    				+ "</div></li>"
    				+ "<li><table><tr><td bgcolor='yellow'>"
    				+ "<font color='green' size='4' face='verdana,helvetica'>green on yellow</font>"
    				+ "</td></tr></table></li></ul>"
    				+ "</html>";
    		JLabel label = new JLabel(htmlText);
    
    //// http://www.devdaily.com/blog/post/jfc-swing/how-add-style-stylesheet-jeditorpane-example-code
    
    		HTMLEditorKit hed = new HTMLEditorKit();
    		StyleSheet ss = hed.getStyleSheet();
    		ss.addRule("h2 {color : green; font-weight: bold;}");
    		ss.addRule("#mytag {color :  #bb0022; font-style:italic;}");
    		ss.addRule(".mytag {color : rgb(0,128,25); font-weight: bold;}");
    		ss.addRule("[mytag]{color : purple; font-family: monospace;}"); // not selected
    		ss.addRule("mytag {color : purple; text-size:14pt;}");  // not selected
    		Document doc = hed.createDefaultDocument();
    
    		JEditorPane editor = new JEditorPane();
    		editor.setEditorKit(hed);
    		editor.setDocument(doc);
    		editor.setText(htmlText);
    
    		Box box = Box.createHorizontalBox();
    		box.add(label);
    		editor.setBorder(BorderFactory.createMatteBorder(0, 2, 0, 0, Color.BLACK));
    		box.add(editor);
    		getContentPane().add(box);
    
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setPreferredSize(new Dimension(600, 270));
    		pack();
    		setLocationRelativeTo(null);
    	}
    
    	public static void main(String[] args) throws Exception {
    		(new TestHTML()).setVisible(true);
    	}
    }

  5. #5
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    Ah, now I got it, thank you !

    Java Code:
    	private static String webContent = "<html>" +
    			"<head>" +
    			"<style type=\"text/css\">.mytag {color:red} #mytag {color:green}</style>" +
    			"</head>" +
    			"<body>" +
    			"<p>Hello <span class='mytag'>World</span> <span id='mytag'>:-)</span> </p>" +
    			"</body>" +
    			"</html>";
    
    	public static void main(String[] args) {
    		JFrame f = new JFrame("HTML Example");
    
    		JEditorPane jep = new JEditorPane();
    		jep.setEditorKit(new HTMLEditorKit());
    		jep.setText(webContent);
    
    		f.add(jep);
    		f.setSize(300, 200);
    
    		f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    
    		f.setVisible(true);
    	}
    Its not elegant like custom tags but follows some standard ;-)
    Thanks again

Similar Threads

  1. problem related to custom tags in jsp
    By garima in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-20-2009, 12:04 PM
  2. HTML tags anyone?
    By tim in forum Suggestions & Feedback
    Replies: 2
    Last Post: 06-29-2008, 04:49 AM
  3. Html tags within XML- need help
    By iamhappy in forum XML
    Replies: 2
    Last Post: 03-27-2008, 04:21 PM
  4. jeditorpane help parsing html
    By asifsolkar in forum Advanced Java
    Replies: 4
    Last Post: 12-14-2007, 05:23 AM
  5. Adding custom highlight to JEditorPane
    By andrewb in forum AWT / Swing
    Replies: 0
    Last Post: 06-22-2007, 06:48 PM

Tags for this Thread

Posting Permissions

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