Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2011
    Location
    Florida
    Posts
    60
    Rep Power
    0

    Default How do I mix different fonts in the same text box

    I have a JTextArea in my form which needs to be able to display a mix of normal text with symbols (as in Microsoft word "symbol" font). I can't find any information on how to do that. I have to also then be able to export the contents of the text area box to a Word document. Can someone point me in the right direction? Thanks.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

  3. #3
    Join Date
    Feb 2011
    Location
    Florida
    Posts
    60
    Rep Power
    0

    Default

    Ok I looked at the tutorials and am now using a JTextPane. I can now mix fonts and other attributes just fine. However, when I try to set the "Symbol" font so I can display symbols, it doesn't work. Just seems to ignore it. In the code snippet below, "argumentWindow" is my JTextPane and "argWindowDoc" is a StyledDocument.

    Java Code:
            argWindowDoc = argumentWindow.getStyledDocument();
            Style def = StyleContext.getDefaultStyleContext().
                            getStyle(StyleContext.DEFAULT_STYLE);
    
            Style regular = argWindowDoc.addStyle("regular", def);
            StyleConstants.setFontFamily(def, "LucidaGrande");
            StyleConstants.setFontSize(def, 40);
            Style def2 = StyleContext.getDefaultStyleContext().
                            getStyle(StyleContext.DEFAULT_STYLE);
            Style symbol = argWindowDoc.addStyle("Symbol", def2);
            StyleConstants.setFontFamily(def2, "Symbol");
            StyleConstants.setFontSize(def2, 20);
    
            // This outputs some text to test with
            argWindowDoc.insertString(argWindowDoc.getLength(), "abcdefghijklmnop", argWindowDoc.getStyle("regular"));
            argWindowDoc.insertString(argWindowDoc.getLength(), "abcdefghijklmnop", argWindowDoc.getStyle("symbol"));
    Both display the same. I tried changing the symbol font to other fonts and they work. I know the symbol font exists on my system because I displayed all available fonts and it is one of them. Am I doing something wrong?
    Last edited by madroadbiker; 05-26-2011 at 01:07 AM.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default

    I don't know much about Fonts but (on my machine) when you select the Symbol font all you get is a bunch of rectangles. So maybe Swing doesn't know how to renderer the Font?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.plaf.basic.*;
    
    public class ComboBoxFonts extends JFrame implements ItemListener
    {
    	JTextArea textArea;
    	JComboBox comboBox;
    
        public ComboBoxFonts()
        {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment ();
            Font [] fonts = ge.getAllFonts ();
            System.out.println(fonts.length);
            comboBox = new JComboBox( fonts );
            comboBox.setRenderer( new MyFontRenderer() );
            comboBox.addItemListener( this );
            getContentPane().add( comboBox, BorderLayout.SOUTH );
            textArea= new JTextArea("Some text", 3, 20);
            getContentPane().add( new JScrollPane( textArea ) );
        }
    
    	public void itemStateChanged(ItemEvent e)
    	{
    		Font font = (Font)e.getItem();
    		textArea.setFont( font.deriveFont( textArea.getFont().getSize2D() ) );
    //		comboBox.setFont( font.deriveFont( comboBox.getFont().getSize2D() ) );
    	}
    
        public static void main(String[] args)
        {
            ComboBoxFonts frame = new ComboBoxFonts();
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.pack();
            frame.setLocationRelativeTo( null );
            frame.setVisible( true );
        }
    
        class MyFontRenderer extends BasicComboBoxRenderer
        {
            public Component getListCellRendererComponent(
                JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
            {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    
                Font font = (Font)value;
    //            setFont( font.deriveFont(12.0f) );
                setText( font.getName() );
    
                return this;
            }
        }
    
    }

Similar Threads

  1. What every developer should know about fonts
    By david@windward.net in forum Java 2D
    Replies: 0
    Last Post: 12-03-2010, 11:08 PM
  2. want to know how to add new fonts in ireport 2.04
    By sudhakarrs in forum Advanced Java
    Replies: 0
    Last Post: 10-02-2010, 05:59 PM
  3. Strings and fonts
    By TheGoodGuy in forum New To Java
    Replies: 12
    Last Post: 09-05-2010, 02:01 AM
  4. Replies: 0
    Last Post: 01-27-2010, 03:52 PM
  5. Fonts (Changing Fonts and Color's)
    By dbashby in forum New To Java
    Replies: 10
    Last Post: 04-06-2009, 01:32 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
  •