This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

replaceSelection() in TextPane

 
prathana balaji
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class ListenerThread extends Thread{
private Socket client;
public static String recdData = new String();

public ListenerThread_2(Socket client){
this.client = client;
new Thread().start();
}


public void run(){
String line;
String recdData=" ";
BufferedReader in = null;
PrintWriter out = null;
JOptionPane optionPane = new JOptionPane();

while(true){
try{

byte[] buf = new byte[10000];
HMIClient.init.GlobalFile.socket.getInputStream().read(buf);
recdData = new String(buf);

if(recdData.equals(" ")){
System.out.println("Message not Received");
}

else {

change_color(recdData.trim());

}


}catch(Exception ioe){

optionPane.showMessageDialog(new JFrame(),"Application Server is not started");
System.exit(0);
}
}
}

public static void append_textPane(Color c, String s) {
StyleContext stycxt = StyleContext.getDefaultStyleContext();
AttributeSet attrset = stycxt.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
logTextPane.setCaretPosition(logTextPane.getDocument().getLength());
logTextPane.setCharacterAttributes(attrset, false);
logTextPane.replaceSelection(s);
}
public static void change_color(String log){
String logs=log;
if(logs.contains("TXT"))
{
append_textPane(Color.GREEN,logs);
}
else {
append_textPane(Color.ORANGE,logs);
}
}
}

In this logTextPane is a JTextPaneComponent where the text is to be printed in different colors,replaceSelection is not working in this context...But I treied replaceSelection() in simple JTextPane Components in the JFrame it is working perfetly Fine,but replaceSelection() is not working here.The OUTPUT of the code....Nothing is printed in the JTextPane

I have tried with setText() also...but doing that the JTEXTPANE is not getting appened but it is overwritten

Please someone help me.
Prathana
 
Brian Cole
Author
Ranch Hand
Posts: 902
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prathana balaji:
But I treied replaceSelection() in simple JTextPane Components in the JFrame it is working perfetly Fine,but replaceSelection() is not working here.The OUTPUT of the code....Nothing is printed in the JTextPane

I have tried with setText() also...but doing that the JTEXTPANE is not getting appened but it is overwritten


The code in append_textPane() looks good to me, so I wonder if you might be having threading issues. Does it work any better if you replace

change_color(recdData.trim());

with

final String s = recdData.trim();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
change_color(s);
}
});
 
prathana balaji
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi brian,

I have tried it...it is not working,Actually nothing is printed in the TextPane.actually my purpose id to change the color of the text according to somecondition(is particular string is present)and simultaneouly append it into the textpane from the socket

advance thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic