JEditorPane updates

I’m currently using a JEditorPane for my chat area (because I want colors and stuff). When I update my text I call setText() on the editor with a maintained string. However, this has to two problems

  1. Its pretty expensive to reset all the text
  2. It doesn’t reset the scroll to the bottom of the screen.

Does anyone have ways round this?

Kev

ahh… seems I could do something with JEditorPane.getDocument().insertString().

Any one got any examples of using AttributeSets to colour text?

Kev

Hi,
In the Lost Newless Clubies I posted an example… :-/
Just wait until I return to home and I’ll post it again… (It was used to simulate Yahoo’s Messenger look).

Rafael

There is the code:

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

public class TextExample extends JFrame{ 
  //Interface 
  //message area 
  JTextPane messages; 
  Document doc; 
  JTextField talk; 
  JButton send; 
  JLabel stat; 
  String user; 
 
  public TextExample(String u){ 
    user = u; 
    create(); 
  } 
  public void create(){ 
    setTitle("Yo te busco - "+user); 
    getContentPane().setLayout(new BorderLayout()); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    messages = new JTextPane(); 
    //With this the pane cannot be edited!! 
    messages.setEditable(false); 
    //this is used to create styles for te text... 
    //there are options to change the font. 
    //first, the deafult style. 
    Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); 
    //then I make the first style for the TextPane "text", derivate from def. 
    Style text = messages.addStyle("text", def); 
    //the second style is "bold", derivate from  
    Style bold = messages.addStyle("bold", text); 
    //set the attribute bold to the style. 
    StyleConstants.setBold(bold, true); 
    //make a new type. 
    Style img1 = messages.addStyle("img1", text); 
    StyleConstants.setIcon(img1, new ImageIcon("solo.gif")); 
    //make a new type. 
    Style img2 = messages.addStyle("img2", text); 
    StyleConstants.setIcon(img2, new ImageIcon("varios.gif")); 
    //I want a reference to the TestPane's document (it's the model of this component). 
    doc = messages.getStyledDocument(); 
 
    talk = new JTextField(15); 
    send = new JButton("Send");     
    stat = new JLabel(); 
    ActionListener ac = new ActionListener(){ 
 public void actionPerformed(ActionEvent ae){ 
   /********************************************************* 
     This creates the message header, Yahoo Messenger's Style 
   **********************************************************/ 
   Calendar c = Calendar.getInstance(); 
   int h = c.get(Calendar.HOUR_OF_DAY); 
   int m = c.get(Calendar.MINUTE); 
   String hora = ((h<10)?"0":"")+h+":"+((m<10)?"0":"")+m; 
   String he = user+"["+hora+"]"+":"; 
   int lh = he.length(); 
   /*************************************************/ 
   //here the text is added to the textpane 
   add(he,"bold"); //this with the 'bold' style
   String text = talk.getText();
   if(text.equals("solo"))
     add("\n","img1"); //this is the image
   else if(text.equals("varios"))
     add("\n","img2"); //this is the image
   else 
     add(talk.getText()+"\n","text"); //this with the 'text' style 
   talk.setText("");  
 } 
    }; 
    talk.addActionListener(ac); 
    send.addActionListener(ac); 
    JPanel pan = new JPanel(new FlowLayout()); 
    JPanel pan2 = new JPanel(new GridLayout(2,1)); 
    pan.add(talk); 
    pan.add(send); 
    pan2.add(pan); 
    pan2.add(stat); 
    getContentPane().add(new JScrollPane(messages),BorderLayout.CENTER); 
    getContentPane().add(pan2,BorderLayout.SOUTH); 
    setSize(300,500); 
    setVisible(true);      
  } 
  private void add(String s,String t){ 
    try{ 
 doc.insertString(doc.getLength(),s,messages.getStyle(t)); 
    } 
    catch(BadLocationException ble){ 
     
    } 
  } 
  public static void main(String[] args){ 
    if(args.length!=1){ 
 System.out.println("Usage: TextExample username"); 
 System.exit(0); 
    } 
    TextExample t = new TextExample(args[0]); 
  } 
}

The code needs two images ‘solo.gif’ and ‘varios.gif’ they are used like ‘emoticons’, when you type ‘solo’ or ‘varios’ as the message.

Rafael