开发者

change font of specific lines in JTextArea

开发者 https://www.devze.com 2023-04-03 14:17 出处:网络
hi there ı am working on a chat application and i want that user can change the font which he/she is writing. there is a setFont() function but it changes font of all strings in the TextArea. so i ju

hi there ı am working on a chat application and i want that user can change the font which he/she is writing. there is a setFont() function but it changes font of all strings in the TextArea. so i just want to change only my font.i appreciated if you can help me开发者_运维技巧.


well then i guess i must learn a litte HTML

I wouldn't use HTML. I find it easier to just use attributes when dealing with a text pane. Attributes are much easier to change then trying to manipulate HTML.

SimpleAttributeSet green = new SimpleAttributeSet();
StyleConstants.setFontFamily(green, "Courier New Italic");
StyleConstants.setForeground(green, Color.GREEN);

//  Add some text

try
{
    textPane.getDocument().insertString(0, "green text with Courier font", green);
}
catch(Exception e) {}


You should work with JTextPane. JTextPane allows you to use HTML. Check the following example:

this.text_panel = new JTextPane();
this.text_panel.setContentType("text/html");
this.text_panel.setEditable(false);
this.text_panel.setBackground(this.text_background_color);
this.text_panel_html_kit = new HTMLEditorKit();
this.text_panel.setEditorKit(text_panel_html_kit);
this.text_panel.setDocument(new HTMLDocument());

Here you are enabling HTMLEditorKit, which will allow you to use HTML in your TextPane. Here is another peice of code, where you can add colored text to the panel:

public void append(String line){
    SimpleDateFormat date_format = new SimpleDateFormat("HH:mm:ss");
    Date date = new Date();

    line = "<div><font size=3 color=GRAY>[" + date_format.format(date) + "]</font><font size=3 color=BLACK>"+ line + "</font></div>";

    try {
        this.text_panel_html_kit.insertHTML((HTMLDocument) this.text_panel.getDocument(), this.text_panel.getDocument().getLength(), line, 0, 0, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}


Hope this helps,
Serhiy.


You can't do that with JTextArea, but you can do it with its fancier cousin, JTextPane. It's unfortunately not trivial; you can learn about this class here.


A variety of Swing components will render basic HTML (version 3.2), including JLabel & JEditorPane. For further details see How to Use HTML in Swing Components in the Java Tutorial.

Here is a simple example using the latter.

change font of specific lines in JTextArea

import java.awt.*;
import javax.swing.*;

class ShowFonts {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                GraphicsEnvironment ge = GraphicsEnvironment.
                    getLocalGraphicsEnvironment();
                        String[] fonts = ge.getAvailableFontFamilyNames();
                String pre = "<html><body style='font-size: 20px;'><ul>";
                StringBuilder sb = new StringBuilder(pre);
                for (String font : fonts) {
                    sb.append("<li style='font-family: ");
                    sb.append(font);
                    sb.append("'>");
                    sb.append(font);
                }
                JEditorPane ep = new JEditorPane();
                ep.setContentType("text/html");
                ep.setText(sb.toString());


                JScrollPane sp = new JScrollPane(ep);
                Dimension d = ep.getPreferredSize();
                sp.setPreferredSize(new Dimension(d.width,200));

                JOptionPane.showMessageDialog(null, sp);
            }
        });
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号