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.
-
possible duplicate of [How to change text color in the JtextArea?](http://stackoverflow.com/questions/9650992/how-to-change-text-color-in-the-jtextarea) – Suma Jan 08 '15 at 13:21
4 Answers
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) {}

- 321,443
- 19
- 166
- 288
-
hach ... didn't read to the end of the answers :-) +1 for fitting into my expectation – kleopatra Sep 07 '11 at 09:03
-
yess i finally managed to understand and used your code but i guess i must count all the characters to insert string at the end. :) – quartaela Sep 07 '11 at 11:55
-
@user743898, To insert at the end you use `textPane.getDocument().getLength()`. – camickr Sep 07 '11 at 15:43
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.

- 4,073
- 3
- 36
- 66
-
well then i guess i must learn a litte HTML :). and this code is helpful for me. thanks mate. – quartaela Sep 06 '11 at 17:09
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.

- 80,601
- 10
- 150
- 186
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.
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);
}
});
}
}

- 168,117
- 40
- 217
- 433
-
-
@mKorbel It's laziness, really - a few lines shorter than making a `JFrame`. ;) – Andrew Thompson Sep 06 '11 at 18:04
-
well this a few complex for me :D. but i hope i will learn from that tutorial :) – quartaela Sep 06 '11 at 21:18
-
-
(shrugs) I'm *pretty* sure that any solution involving a `JTextPane` would also need to call `setPreferredSize()` at some point. Feel free to prove me wrong. – Andrew Thompson Sep 07 '11 at 08:55
-
not with an powerful enough LayoutManager :-) But that's a different story - what I wanted to nitpick here is that the shortcut with the optionPane (which I love, btw) moves the hard-coded sizing from the top-level container down to a child – kleopatra Sep 07 '11 at 08:58
-
curious (and back to the problem): why do you all recommend html? Isn't the OPs requirement a clean case for some custom styles in a textPane? – kleopatra Sep 07 '11 at 09:02
-
@kleopatra OK - I'll concede that last point. It was only after carefully re-reading the OP's question that it occurred to me the HTML/JEP route was not especially good for constantly changing text, which I think the question implies. The `JTextPane` is an excellent choice for this use-case. – Andrew Thompson Sep 07 '11 at 09:50
-
@kleopatra *"powerful enough LayoutManager"* OK - I'll bite. Let's not limit this to J2SE. What layout manager can properly size* a `JEditorPane` or `JTextPane` without use of `setPreferredSize()`, magic numbers (specifying a size) fed to the layout constraints or constructor, or HTML with CSS specifying the width? ( * And I do mean, 'short of scrying or haruspication'. ;) – Andrew Thompson Sep 07 '11 at 09:59
-
hach .. I knew you would :-) As mentioned elsewhere, it's okay to feed the _LayoutManager_ instead of the component, in whatever units, constraints are necessary. A powerful manager makes that easy enough. – kleopatra Sep 07 '11 at 10:30
-
I think the question is only about JTextField. We know there are other components. – Mehdi Dec 02 '16 at 16:54