Results 1 to 4 of 4
- 05-23-2011, 08:43 PM #1
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.
- 05-23-2011, 08:59 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
You can use a JTextPane for this.
Using Text Components (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
- 05-26-2011, 01:01 AM #3
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.
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?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"));Last edited by madroadbiker; 05-26-2011 at 01:07 AM.
- 05-26-2011, 02:24 AM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
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
-
What every developer should know about fonts
By david@windward.net in forum Java 2DReplies: 0Last Post: 12-03-2010, 11:08 PM -
want to know how to add new fonts in ireport 2.04
By sudhakarrs in forum Advanced JavaReplies: 0Last Post: 10-02-2010, 05:59 PM -
Strings and fonts
By TheGoodGuy in forum New To JavaReplies: 12Last Post: 09-05-2010, 02:01 AM -
Changing text fonts and colours for specific files.
By breako in forum EclipseReplies: 0Last Post: 01-27-2010, 03:52 PM -
Fonts (Changing Fonts and Color's)
By dbashby in forum New To JavaReplies: 10Last Post: 04-06-2009, 01:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks