0

Is there a way I can use two completley different fonts in the same swing component?

I mean like two separate .tff files (processed in any way). Only requirement is that text is editable.

I'm open for external libraries too. That would also work. I'd look for them myself, but this is a minor feature and I'd have to go trought API of every single one of them to see if it's there...

External libaraies that do super AND subscripting are also welcome, as long as I can control the size and height of the script.

Karlovsky120
  • 6,212
  • 8
  • 41
  • 94

1 Answers1

4

The option is: JTextPanet: has a Document to manage data and Editor kit to read and write text with editing capability even with styling( e.g, with font type and color foreground). You can also implement undo-redo options too.

However to satisfy your question i am writing a small snippets with StyleDocument to show how it works.

enter image description here

  StyledDocument styleDocument =  jTextPane1.getStyledDocument();
  Style primaryStyle = styleDocument.addStyle("Primary", null);
  Style secondaryStyle = styleDocument.addStyle("Secondary", primaryStyle);


  StyleConstants.setFontFamily(primaryStyle, "American Captain");
                        // use font-family from your platform
  StyleConstants.setFontSize(primaryStyle, 24);

  StyleConstants.setFontFamily(secondaryStyle, "Bira PERSONAL USE ONLY");
                       // use font-family from your platform
  StyleConstants.setFontSize(secondaryStyle, 20);
  StyleConstants.setForeground(primaryStyle, new Color(0x552AFF));
  StyleConstants.setForeground(secondaryStyle, Color.black);

  try {
      styleDocument.insertString(0, "Title with American Captain font\n\n", primaryStyle);
      styleDocument.insertString(styleDocument.getLength(), "Font demonstration with JTextPane. "
              + "Seriously, it is powerful and has the power to do all kind of styling with text. "
              + "check it out, check its mighty power and be embrassed\n", secondaryStyle);
   } catch (BadLocationException ex) {
                Logger.getLogger(JTextPaneTest.class.getName()).log(Level.SEVERE, null, ex);
   }

Every Swing Text component is associated with a model known as Document to manage data. What we have done in this code is that, we set different style instances to the StyleDocument associating with the JTextPane component. I have assumed that you know how to add the JTextPane to the JFrame and show it. The two different style primary and secondary has different foreground and font family: "American Captain" and "Bira PERSONAL USE ONLY". You can replace them with font-family available in your platform including serif and sans-serif. Remember that these style created by StyleDocument.addStyle() has parent-child hierarchy relation. The child's style attributes(Foreground, background, font size, font style) will override the parent's attributes.

However Please go through these tutorial article:

  1. Using Text Components And
  2. How to Use Editor Panes and Text Panes
Sage
  • 15,290
  • 3
  • 33
  • 38
  • I did say custom *.ttf files, didn't I? :) – Karlovsky120 Oct 20 '13 at 18:05
  • +1 You can use custom fonts; but, as shown [here](http://stackoverflow.com/a/8534162/230513), the `StyledEditorKit`'s `Action`s work better with the platform's font families, which typically have convenient unicode glyphs. – trashgod Oct 20 '13 at 18:19
  • @Karlovsky120, as `trashgod` said, you can use any font you want. Just pass the font name in place of `serif` and(or) `sans-serif`, that's all. – Sage Oct 20 '13 at 18:22
  • Let's do it this way. I have `font.ttf` file in src folder of the project. And I have Font object `customFont` in my program. Show me how to get either of these two things to work with what you+ve show above, and I'll give 50 points to whoever does. – Karlovsky120 Oct 20 '13 at 18:25
  • @Karlovsky120, what have you tried? What happens when you create your custom Font? Does the Font automatically get registered with the System? That is can you use the getFontName() method of the Font so that you can use the name with StyledConstants? – camickr Oct 20 '13 at 18:31
  • And that was the little detail I was looking for. I think. Let me check. – Karlovsky120 Oct 20 '13 at 18:32
  • @camickr, thanks, i was just about to write these in the answer. Anyway thanks for the response – Sage Oct 20 '13 at 18:38
  • @Karlovsky120, look into these [`registerFont`](http://docs.oracle.com/javase/7/docs/api/java/awt/GraphicsEnvironment.html#registerFont%28java.awt.Font%29) function of `GraphicsEnvironment` – Sage Oct 20 '13 at 18:41
  • It works, I love you all. I'll give 50 points to @Sage , since he was here longer, and he has way less points than you camickr . Thank you again. – Karlovsky120 Oct 20 '13 at 18:47