| Author |
replaceSelection() in TextPane
|
prathana balaji
Ranch Hand
Joined: Oct 07, 2007
Posts: 30
|
|
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
Joined: Sep 20, 2005
Posts: 852
|
|
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); } });
|
bitguru blog
|
 |
prathana balaji
Ranch Hand
Joined: Oct 07, 2007
Posts: 30
|
|
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
|
 |
 |
|
|
subject: replaceSelection() in TextPane
|
|
|