1

How can I use JTextPane to style a section of text as "CODE," like you often see on forums, or you see here on stack overflow?

public static main(String[] args) {
    /**
     * Look at this Code Block, ain't it grand?
     * I wish I had something like this in my program.
     */
}

Or how I have seen done on wikipedia, where the text is like this: http://img39.imageshack.us/img39/4516/example.JPG

Thank you!

FINAL UPDATE Vishal K's answer was exactly what I needed. Not a duplicate, as was suggested.

UPDATE
Thank for you the responses. The difference between what I am looking for and what was suggested as a possible answer above is that I am interested in not only changing the font, but also adding a background (a border around said background would be a plus, but not required. I do not need to do syntax highlighting.

I think HTML tags may be the way to go, and if so, that is really the question: How can I use html to format the code in such a way? I will play with the example provided in the answers and let you know.

P.s. I already read the links to the oracle tutorials before asking this question.

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
Jefferson Hudson
  • 728
  • 1
  • 10
  • 22
  • 1
    You can set the font. However, if you want to change colors of keywords, identifiers etc, then it's going to be hard. You'll have to write a complete library to find words and change their color. – Karan Goel Mar 28 '13 at 17:22
  • http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html#textpane – Gilbert Le Blanc Mar 28 '13 at 17:24
  • possible duplicate of [How to add text different color on JTextPane](http://stackoverflow.com/questions/6068398/how-to-add-text-different-color-on-jtextpane) – Gilbert Le Blanc Mar 28 '13 at 17:25
  • http://www.java2s.com/Code/Java/Swing-JFC/TextPane.htm – Gilbert Le Blanc Mar 28 '13 at 17:26
  • 1
    I don't see it as a duplicate, this questions is a superset of the one you linked. The OP here first needs a method to determine highlight color for the text (variables, reserved words, class types, etc). – Jason Nichols Mar 28 '13 at 17:26

1 Answers1

1

How can I use JTextPane to style a section of text as "CODE," like you often see on forums, or you see here on stack overflow?

Using HTML Tags. But before this you would have to set contentType to be ("text/html").
Here is a simple Example for this :
enter image description here

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class TextPaneDemo {
  static void addIt(JTabbedPane tabbedPane, String text) {
    JPanel panel = new JPanel();
    JTextPane ta = new JTextPane();
    ta.setContentType("text/html");
    ta.setText("<HTML><BODY><CODE> import java.io.*; <br> public class MyIO{}</CODE><br></BODY></HTML>");
    JScrollPane jsp = new JScrollPane(ta);
    panel.setLayout(new BorderLayout());
    panel.add(jsp);
    tabbedPane.addTab(text, panel);
  }

  public static void main(String args[]) {
    JFrame f = new JFrame("JTabbedPane Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = f.getContentPane();
    JTabbedPane tabbedPane = new JTabbedPane();
    addIt(tabbedPane, "Tab One");
    content.add(tabbedPane, BorderLayout.CENTER);
    f.setSize(300, 200);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}
Vishal K
  • 12,976
  • 2
  • 27
  • 38
  • You can do Everything that you want to using HTML Tags within the JTabbedPane. If you are quite well versed in HTML rendering then it wont't be a big problem for you to achieve what you want using HTML tags. – Vishal K Mar 28 '13 at 18:23
  • I notice that you didn't close the tag. Is there a reason? – Jefferson Hudson Mar 28 '13 at 18:23