• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextArea problem  RSS feed

 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I am trying to write a Chat Server/Client program which is some what similar to the Yahoo chat. I have a general/public chat as well. What problem is, is it possible to change the font attributes row by row other than all the texts in the JTextArea getting changed according to the new font attributes. If you have any other suggestion please do tell me.
Thank you very much....
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Swing forum...
 
Timo von Oertzen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ransika,

usually, if you want to format single lines, you don't use JTextArea but either JTextPane or JEditorPane; I prefer the latter since it allows editing also, if needed, and formatting can be done by html. Following code should look like you want it:

JFrame f =new JFrame();
JEditorPane ep = new JEditorPane("text/html","<html><p><font face=\"Roman\">This paragraph in Roman</font></p><p><font face=\"Serif\">This paragraph in Serif</font></p></html>");

f.setContentPane(ep);
f.show();

Hope it helps, best regards,

Timo
 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I understood what you guys say. Actually I am writting a Chat server/client program. I am having a main/General chat area in it. When a user sends a message, the font attributes(size, color, type) go with the message to the server. What I am trying to accomplish is to send the exact message to all the other clients. With the same font, font color, font size etc. So the new message should get appended to the earlier messages in the main chat area, as a new row. That is what I meant by adding messages row by row. Basically I want to do the same as the Yahoo.com main chat area.
Hope you understand what I am trying to explain. So could you please give me an answer.
Thank you...
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Timo implied but did not state explicitly, JTextArea doesn't support styled text, while JEditorPane, and especially JTextPane, do. You simply can't do what you want with JTextArea.

There are some helpful examples of using JTextPane in the Java Developer's Almanac.
 
Timo von Oertzen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ransika,

following method should do what you want:

******
public void newLine(String fontFace, String size, String color, String text)
{
String s = editorPane.getText(); // where editorPane is the editorPane
// defined somewhere globally as in my
// last reply
s += "<p><font face=\""+fontFace+"\" size=\""+size+"\" color=\""+color+"\">"+text+"</font></p>";
editorPane.setText(s);
}
******

Sorry, I'm a bit in a hurry and didn't try it, but it should work, I think. Hope it helps,

Timo
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!