Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

color text with HTMLDocument  RSS feed

 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,

I have a HTMLDocument and was woundering how can i set the text with a different color:

eg.,

there's a man on the moon //the word man is in blue


Thanks
Ppr
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just to be more precise, here is my code.

you will note (if you run it) that the coloring is ok but the spacing is wrong. this led me to believe that i'm doing somthing wrong with the color.


thanks for any help


Below is my code (and it looks so odd because of the spacing) I wonder how to color it.

if you could copy past it - you'll see what I'm talking about.

I guess I am doing somthing wrong with the coloring.

any help?

import javax.swing.*;
import java.awt.*;
import javax.swing.text.html.*;
import javax.swing.text.*;
import javax.swing.event.*;

public class MyHTMLEditor extends JPanel {
BorderLayout layout = new BorderLayout();
BorderLayout layoutEditor = new BorderLayout();

JTextPane Editor = new javax.swing.JTextPane();
JTextPane SourceEditor = new javax.swing.JTextPane();

protected SimpleAttributeSet attr = new SimpleAttributeSet();
HTMLEditorKit editorKit = new HTMLEditorKit();
StyledEditorKit defaultEditor = new StyledEditorKit();


JViewport viewport;
JViewport sourceviewport;
JScrollPane spSourceEditor = new JScrollPane();
JScrollPane spEditor = new JScrollPane();

JTabbedPane tpEditor = new javax.swing.JTabbedPane();

HTMLDocument document = new HTMLDocument();

public MyHTMLEditor() {
spEditor.getViewport().setLayout(layoutEditor);
Editor.setText(" ");
Editor.setEditorKit(editorKit);

SourceEditor.setText(" ");
SourceEditor.setEditorKit(defaultEditor);

this.setLayout(layout);

viewport = spEditor.getViewport();
spEditor.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
viewport.add(Editor);


//coloring
black("There is a ");
blue("man ");
black("on the moon");



tpEditor.addTab("Editor", spEditor);
tpEditor.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (tpEditor.getSelectedComponent() == spEditor) {
Editor.setText(SourceEditor.getText());
} else {
SourceEditor.setText(Editor.getText());
}
}
});
sourceviewport = spSourceEditor.getViewport();
sourceviewport.add(SourceEditor);
tpEditor.addTab("Source", spSourceEditor);

tpEditor.setTabPlacement(SwingConstants.BOTTOM);

this.add(tpEditor, BorderLayout.CENTER);

document.setPreservesUnknownTags(true);
Editor.setDocument(document);
}

public void blue(String str)
{
try{
StyleConstants.setFontSize(attr, 12);
StyleConstants.setForeground(attr, Color.BLUE);
StyleConstants.setBold(attr, true);
document.insertString(document.getLength(), str, attr);

}catch(BadLocationException e)
{
System.out.println("an error has occured x 2572 at QuotingLetter class");
}
}


public void black(String str)
{
try{
StyleConstants.setFontSize(attr, 14);
StyleConstants.setBold(attr, false);
StyleConstants.setForeground(attr, Color.BLACK);
document.insertString(document.getLength(), str, attr);

}catch(BadLocationException e)
{
System.out.println("an error has occured x 5573 QuotingLetter class");
}
}

public static void main(String[] args) {
MyHTMLEditor editor1 = new MyHTMLEditor();
JDialog dialog = new JDialog(new Frame());
dialog.getContentPane().add(editor1);
dialog.setSize(640,480);
dialog.setResizable(true);
dialog.setModal(true);
dialog.setVisible(true);
System.exit(0);
}
}
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!