• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multi Color-Font in JEditorPane  RSS feed

 
Pratik Khetia
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to display multi-color / font in JEditorPane. I am using it to display chat messages and the formate is RTF. How do I get this functionality ?

Thanks
Pratik
 
Jennifer Garrett
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to insert html tags in the text that goes in the JEditorPane. In my code, I wait for the user to click the ok button and then I make some words red if there is a problem with those words. If the data is changing often, you probably want to use setText() to change the text in the JEditorPane, otherwise you can create a file and use setPage() to tell the JEditorPane where to find the text.
This is part of the code from something similar that I did recently.

final JEditorPane myEditorPane = new JEditorPane();
myEditorPane.setContentType("text/html");
final HTMLEditorKit editor =
(HTMLEditorKit)myEditorPane.getEditorKit();
myEditorPane.setPreferredSize(new Dimension(100, 100));
JScrollPane myScrollPane = new JScrollPane(myEditorPane);
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// do whatever you want when the button is pushed
String newData = myEditorPane.getText();
StringBuffer myDataBuffer = new StringBuffer(newData);
// change the data, insert html tags
myEditorPane.setText(myDataBuffer.toString());
}
});
myPanel.add(myScrollPane);
myPanel.add(okButton);
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!