2 diffrent Fonts in a JTextArea?

Hello... it is probably an easy question.
Is it possible to set the Font of the JTextArea for two diffrent lines?
here is my code:
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class MainFrame extends JFrame{
      * @param args
     public MainFrame(){
          JFrame frame = new JFrame("asd");
          JPanel myPanel = new JPanel();
          Font fBolt = new Font("font1",Font.BOLD, 12);     
          Font fPlein = new Font("font2",Font.PLAIN, 12);     
          JTextArea myArea = new JTextArea();
          myArea.setFont(fBolt);
          myArea.append("NAME\n");
          myArea.setFont(fPlein);
          myArea.append("SURNAME");
          myPanel.add(myArea);
          frame.add(myPanel);
          frame.setSize(200, 200);
          frame.setVisible(true);
     public static void main(String[] args) {
          // TODO Auto-generated method stub
          MainFrame ma = new MainFrame();
}What I wanted to se was NAME in Bold and SURNAME in normal text.
Is it possibile?
Thanks in advance. Any hints could be useful
Mandy

Is it possible to set the Font of the JTextArea for two diffrent lines?Unfortunately it is not possible :(
I was joking ;-) It is possible, but not in JTextArea, you have to use JTextPane or JEditorPane for this purpose. Specially, JTextPane is ideal.
To know how to use these components, have a look at these tutorial trails:
[http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html]
[http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html]
[http://java.sun.com/docs/books/tutorial/uiswing/components/text.html]
Thanks!
Edit: A sample using JEditorPane:
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainFrame2 extends JFrame {
    public MainFrame2() {
        JFrame frame = new JFrame("asd");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel myPanel = new JPanel();
        JEditorPane myArea = new JEditorPane();
        myArea.setContentType("text/html");
        myArea.setText("<html><body>NAME:<br/><b>SURNAME:</b></body></html>");
        myPanel.add(myArea);
        frame.getContentPane().add(myPanel);
        frame.setSize(200, 200);
        frame.setVisible(true);
    public static void main(String[] args) {
        MainFrame2 ma = new MainFrame2();
}Edited by: T.B.M on Feb 13, 2009 7:40 PM

Similar Messages

  • Set Font to the JTextArea Problem

    Hi all, i've got a problem in setting the font for my JTextArea.
    Here is my piece of code.
    Display.append(Message.getText());
    Display.setFont(selected_font));
    Display is JTextArea, Message is String.
    The problem is i whenever i do this, everytime i change the Font of the Message, the whole messages in the Display font also change.
    What i want is, when i change the font of certain message, that certain message font ( instead of all message) also change in the Display.
    I know the problem is because of the Display.setFont(), it set the font for the whole content inside the Display. Any help to fulfill what i want?

    I'm not sure a JTextArea is appropriate for what you're doing. As its API documentation begins by saying "A JTextArea is a multi-line area that displays plain text. "
    You can set the font used by this component: but the point is that it uses a single font.
    Perhaps you are looking for a JEditorPane (including JTextPane). Check out Sun's tutorial for code examples etc: http://java.sun.com/docs/books/tutorial/uiswing/components/text.html

  • Changing Font Color in JTextArea

    This question may sound a bit ridiculous to more experienced programmers (I am new to this programming thing)
    but I hope you will help me anyway.
    My problem: How can I change the color of the font
    within a JTextArea or other Swing text objects??
    I could not find any method (like setFontColor) or font
    constructor like Font(Color c).
    Is it maybe possible to change the color with the UIManager ('look and feel')??
    Thank you.

    setForeground(color)

  • How to save font information in jtextarea????????????

    I am trying to develop a notepad. I have successed in changing the font in the jtextarea. But we i close the file and open it again. It show only the plain text and lose all the changes about the font. How can i handle this ?

    Check if you create the text area or another object which deals with the font newly.

  • How to set Multi-Font in one JTextArea?

    Just like:
    first line is Bold
    second is Italic

    To give a short guidance, what to use
    1. Use a JTextPane with a DefaultStyledDocument in it.
    2. Changes to this JTextPane are made to the underlying document, which you can get by the getStyledDocument() method ot JTextPane.
    3. There use the insertString(...) method of this document to make changes
    4. Use a SimpleAttributeSet to hold the styles and manipulate it using the StyleConstants class.
    Hope this will help
    greetings Marsian

  • How to print diffrent color and diffrent size of text in JTextArea ?

    Hello All,
    i want to make JFrame which have JTextArea and i append text in
    JTextArea in diffrent size and diffrent color and also with diffrent
    fonts.
    any body give me any example or help me ?
    i m thanksfull.
    Arif.

    You can't have multiple text attributes in a JTextArea.
    JTextArea manages a "text/plain" content type document that can't hold information about attributes ( color, font, size, etc.) for different portions of text.
    You need a component that can manage styled documents. The most basic component that can do this is JEditorPane. It can manage the following content types :
    "text/rtf" ==> via StyledDocument
    "text/html" ==> via HTMLDocument
    I've written for you an example of how a "Hello World" string could be colorized in a JEditorPane with "Hello" in red and "World" in blue.
    import javax.swing.JEditorPane;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyledEditorKit;
    import javax.swing.text.StyledDocument;
    import javax.swing.text.MutableAttributeSet;
    import javax.swing.text.SimpleAttributeSet;
    import java.awt.Color;
    public class ColorizeTextTest{
         public static void main(String args[]){
              //build gui
              JFrame frame = new JFrame();
              JEditorPane editorPane = new JEditorPane();
              frame.getContentPane().add(editorPane);
              frame.pack();
              frame.setVisible(true);
              //create StyledEditorKit
              StyledEditorKit editorKit = new StyledEditorKit();
              //set this editorKit as the editor manager [JTextComponent] <-> [EditorKit] <-> [Document]
              editorPane.setEditorKit(editorKit);
              StyledDocument doc = (StyledDocument) editorPane.getDocument();
              //insert string "Hello World"
              //this text is going to be added to the StyledDocument with positions 0 to 10
              editorPane.setText("Hello World");
              //create and attribute set
              MutableAttributeSet atr = new SimpleAttributeSet();
              //set foreground color attribute to RED
              StyleConstants.setForeground(atr,Color.RED);
              //apply attribute to the word "Hello"
              int offset = 0; //we want to start applying this attribute at position 0
              int length = 5; //"Hello" string has a length of 5
              boolean replace = false; //should we override any other attribute not specified in "atr" : anwser "NO"
              doc.setCharacterAttributes(offset,length,atr,replace);
              //set foreground color attribute to BLUE
              StyleConstants.setForeground(atr,Color.BLUE);
              //apply attribute to the word "World"
              offset = 5; //we include the whitespace
              length = 6;
              doc.setCharacterAttributes(offset,length,atr,replace);
    }

  • Adding diffrent colored strings to a JTextArea

    I writing a program that uses a messaging window like AIM, GAIM, Trilian etc that uses two JTextarea's to write and read the text. Thus far I cannot figure out how to add strings of diffrent colors to the JTextarea used for writing. The tutorials only explain how to set the font and color of typed text, not text i want to take form the write pane and add it to the read pane. Any ideas?

    You might start with this:
    http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html
    in the Java Tutorial
    and possibly this:
    http://javaalmanac.com/egs/javax.swing.text/pkg.html

  • Setting terminal font in JTextArea

    Hello,
    I want to print text file with table created by using terminal font.But when i open that in JTextArea with setting terminal font for JTextArea,I am not getting same formatting as I am getting notepad.
    The problem is that I can't set terminal font to JTextArea.
    I open font.properties file in jdk1.3\jre\lib and tried to edit it but not unsuccessful.
    Any one help me ?

    Hello,
    I want to print text file with table created by
    y using terminal font.But when i open that in
    JTextArea with setting terminal font for JTextArea,I
    am not getting same formatting as I am getting in
    notepad.
    The problem is that I can't set terminal font to
    o JTextArea.
    I open font.properties file in jdk1.3\jre\lib and
    tried to edit it but unsuccessful.
    Any one help me ?

  • How  to distinguish  textmodel string in two different fonts.

    Hello All,
    I have indesign cs3 textmodel with more than one font used in it, with diffrent font attributes.
    How I will come to know that form where different font use is started , and how should I retrive the attribute.
    I am able to get the fonts used in the textmodel.But how should I retrive the position in the textmodel.
    Like :-
    some text with :- Microsoft Sans Serif Bold, font size 48
    some text with :-Times new Roman Regular, font size 28
    Please reply.

    Hi ,
    Thanks for reply.
    Using IAttributestrand::GetLocalOverrides I am able to get the range of characters with same font attribute, But I am retriving font name and font style as null.
    Please reply.

  • Help!! how to access JTextArea in JScrolPane inside JTabbedPane

    hi there my problem is i want to access setFont method of JTextArea inside JScrolPane which is also inside a JTabbedPane
    here is my code in creating new tabPane:
    if(e.getSource() == createNewTabButton){
    //instance variable editorTab = new JTabbedPane
    editorTab.addTab("Title", new JScrollPane( new JTextArea(18,51) );
    how do i use this code to set the font for every JTextArea in every Tab:
    editorTab.getSelectedComponent()./* i dont know what goes here* /.setFont(new Font(....));
    thanks..

    hi there my problem is i want to access setFont
    method of JTextArea inside JScrolPane which is also
    inside a JTabbedPane
    here is my code in creating new tabPane:
    if(e.getSource() == createNewTabButton){
    //instance variable editorTab = new JTabbedPane
    ditorTab.addTab("Title", new JScrollPane( new
    JTextArea(18,51) );
    how do i use this code to set the font for every
    JTextArea in every Tab:
    editorTab.getSelectedComponent()./* i dont know
    what goes here* /.setFont(new Font(....));
    thanks..This is the only way i could get it to work....
    UIManager.put("TextArea.font", new Font("Trebuchet MS", Font.PLAIN, 32));//overrides the look and feel properties or something
    SwingUtilities.updateComponentTreeUI(editorTab.getSelectedComponent());  //this takes your selected component and applies your font settings to it

  • PDF Print - Numeric&bold Calibri font is printed not properly on Windows 7 (on XP - OK)

    In MS CRM 4.0 system I generate reports using Reporting Services (10.0.4067.0  - 2008 SP2 + MS12-070). Then I export them to PDF.
    Font which is used - Calibri. In PDF file everything is displayed properly. When I print that PDF in Adobe Reader (Windows 7), then bold&numbers are wrong (bigger, smaller captions - looks like diffrent font). When I print the same file on the same printer
    using Windows XP, then everything is ok!
    I've tried to switch Calibri font in Win7 (taken from XP) - still the same problem. I've installed Foxit Reader on that machine and print is good! Unortunately, it has to be Adobe.
    Only one case that is wrong - Win 7, Adobe Reader (X and XI). What can be the reason?

    Hi simon17,
    As per my understanding, I think this issue is caused by the Adobe Reader. I think you can try the following methods to check the issue again:
    Check for Adobe Updates.
    Try the Repair Adobe Installation
    Run Adobe as Administrator. 
    Since this issue is related to Adobe Reader, not related to Reporting Services. If this issue still exists, I suggest you post the question in Adobe Reader forum at:
    http://forums.adobe.com/community/adobe_reader_forums
    Important Note: Microsoft provides third-party contact information to help you find technical support. This contact information may change without notice. Microsoft does not guarantee the accuracy of this third-party contact information.
    Hope this helps.
    Thanks,
    Katherine Xiong
    Katherine Xiong
    TechNet Community Support

  • Font issue in Illustrator CS5

    I upgraded to CS5 a little while back and since then I have been having "font" problems. I like to type out a word and then go to the charater pallet and then scroll thru the font list to see how that word will look in diffrent fonts. Well it use to work GREAT and now it's hit or miss (more miss) when it works......now when I'm scrolling down thru them it will all the sudden jump to the bottom of the list where there is about 5 fonts and just scroll thru those but even there it will mix the fonts up un the word (so each letter is potentially a diffrent font).
    Any advise would be greatful!!
    Nick

    Hi, I'm getting the same issue, but cleaning the font caches hasn't seemed to work.
    It's only illustrator which has this problem, when scrolling font's if it comes to a font it doesn't like (even if these fonts load fine in other programs) it skips down to the bottom of the list and just cycles through them instead.
    (using CS5, on a Windows 7, 64bit HP Laptop)
    thanks,

  • Opera QT4 fonts are terrible..

    Just installed qt4 opera from aur and got it loking ok with -style cleanlooks but the fonts are terrible. I disabled core x fonts but still nothing.Heres what i mean:
    http://img372.imageshack.us/my.php?imag … ot1sf7.jpg
    Comparing the toolbar font to the tabs, it seems they are not anti-aliased. Is there a way to fix this?
    Last edited by b0uncyfr0 (2008-12-08 12:16:34)

    http://operawiki.info/OperaUserCSS
    The wiki mentions that  '~/.opera/styles/browser.css' should override the site author's settings . that doesn't work for me . I forced my settings using '/usr/share/opera/styles/browser.css' .
    sample browser.css
    a:link {
    color: #0000ff;
    a:visited {
    color: #FF00C0;
    @charset "utf-8";
    html,body,* {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    Warning : setting fonts to bold in web browsers is not preferable .
    If you really like tweaking , you can specify diffrent fonts for diffrent classes . the CSS sheet would look like
    @charset "utf-8";
    html {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    body {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    p {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    td {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    tr {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    dt {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    dd {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    span {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    li {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    ul {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    div {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    A {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    h1 {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    h2 {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    h3 {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    h4 {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    h5 {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    h6 {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;
    h7 {
    font:Andika Basic !important ;
    font-family:Andika Basic, sans-serif !important ;
    font-family:Andika Basic, serif !important ;
    font-family:Andika Basic, monospace !important ;
    font-weight: bold !important ;

  • What component Should I use?, Jpanel doesn't work

    Hi
    I need a program that has a window that can go fullscreen and windowed (I did that with a Jframe in fullscreen mode with a jpnale and a 800x600 Jpanel and destroying them with an object that maneges them), I did it with a jframe that contained a Jpanel, I used a Jpanel to avoid the flickering on the screen but I also need to change the font type and the font size, but the setFont on the Jpanel doesn't change the font (as I have realized and also read here [http://forum.java.sun.com/thread.jspa?forumID=257&threadID=150588] ), so I'm lost, I might have to redo the whole thing, but I just wanted to know what component should I use for a window that is going to have custom graphics and need diffrent fonts and diffrent font sizes and work in fullscreen without flickering
    Thank you for your attention, I would aprecciate any help given

    for example:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    public class JPanelFont extends JPanel
      private Font font = getFont();
      private String myString = "Fubars Rule";
      private int fontSize = font.getSize();
      private JSlider slider = new JSlider(0, 100, fontSize);
      public JPanelFont()
        setPreferredSize(new Dimension(620, 250));
        setLayout(new BorderLayout());
        JPanel inputPanel = createInputPanel();
        add(inputPanel, BorderLayout.SOUTH);
      private JPanel createInputPanel()
        JPanel ip = new JPanel();
        slider.setMajorTickSpacing(20);
        slider.setMinorTickSpacing(5);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);
        ip.add(slider);
        slider.addChangeListener(new ChangeListener()
          public void stateChanged(ChangeEvent evt)
            fontSize = (Integer)slider.getValue();
            repaint();
        return ip;
      @Override
      protected void paintComponent(Graphics g)
        Graphics2D g2 = (Graphics2D)g;
        Object originalHint = g2.getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING);
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        super.paintComponent(g2);
        myPaint(g2);
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, originalHint);
      private void myPaint(Graphics2D g2)
        font = font.deriveFont(Font.BOLD, fontSize);
        g2.setFont(font);
        g2.drawString(myString, 20, 100);
      private static void createAndShowUI()
        JFrame frame = new JFrame("Dynamic JPanel Font");
        frame.getContentPane().add(new JPanelFont());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      public static void main(String[] args)
        java.awt.EventQueue.invokeLater(new Runnable()
          public void run()
            createAndShowUI();
    }Edited by: Encephalopathic on May 21, 2008 9:18 PM

  • How can I set the JButton Caption in my mother tongue

    Hi,
    It is possible to me to set the JFrame caption in my mother tongue.
    Using some Key Input Method I will type the characters in my mother tongue
    in the Editor itself, while executing it will show the Title in the same language.
    If I tried for some other components like JButton. It shows [][][][][].... squares.
    What i have to do to achieve the same ?
    Thanks,
    SENTHILKUMAR R

    Language is Tamil and Java Version is 1.5.Now you say what the scriptis. I was going to guess Cherokee, Glagolitic
    or maybe Bopomofo.
    Anyway, here is the unicode:
    http://www.unicode.org/charts/PDF/U0B80.pdfNow you just have to find a font that can display those characters. The following
    works for me...
    import java.awt.*;
    import javax.swing.*;
    class MyUnicode {
        public static void main(String[] args){
            Font font=new Font("Arial Unicode MS", Font.PLAIN, 16);
            JTextArea comp=new JTextArea(20,40);
            comp.setFont(font);
            StringBuffer b = new StringBuffer();
            for(int ch=0x0b80; ch<=0x0bFF; ++ch) {
                b.append((char)ch);
                if (ch % 40 == 0)
                    b.append("\n");
            comp.setText(b.toString());
            final JFrame f = new JFrame("MyUnicode");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container cp = f.getContentPane();
            cp.add(comp);
            f.pack();
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                    f.setLocationRelativeTo(null);
                    f.setVisible(true);
    }

Maybe you are looking for

  • Urgent - Need help with Lighting Effects Filter

    Hi, I need urgent help on something regarding the Lighting Effects filter in Adobe Photoshop CS5. I need lighting effect urgently to complete a project. I am using Photoshop CS5 on a Windows Vista computer in 64-bit mode. On the net it is given that

  • Problems with jsp on 9iAS

    Hello I'm having trouble inserting rows in a table with a jsp page, I have many jsp pages that retrieves data from my tables, but when I make an insert page I have this error. oracle.jsp.provider.JspCompileException: Errores de compilacisn:d:\oracle\

  • (php) Delete being performed on page load instead of on submit-button click

    I'm building a Delete Record page in PHP using Dreamweaver's server behaviors, following the instructions in the DW Help. It's working - except the delete seems to be happening upon delete-carpet.php being loading, without the Submit button on that p

  • SRM 4.0

    Hi, We are unable to assign purchasing organization and purchase group to the users in the org plan.(PPOMA_BBP). The issue: We one user for whom we have already extended the Purchase organization & purchase Group.Now we have added few more user in th

  • Access Profile question

    On various related lists the access level choices include View and Read-Only. A good example is Account and Open Activities. What are the functional differences between View and Read-Only? Regards, Jeff