help with regex...and marking up text in JTextpane with html

Reply

Join Date: Sep 2004
Posts: 7
Reputation: joebanks is an unknown quantity at this point 
Solved Threads: 0
joebanks joebanks is offline Offline
Newbie Poster

help with regex...and marking up text in JTextpane with html

 
0
  #1
Oct 27th, 2004
having trouble getting this match and replace to work, im trying to just find a word and put html bold tags around it on the button click and put that text into a new jtextpane. anyone have any suggestions??? any help would be most appreciative

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.StringTokenizer;
  4. import java.util.regex.*;
  5. import javax.swing.*;
  6.  
  7. public class Assn4 extends JFrame{
  8.  
  9.  
  10. private JTextPane textPane1, textPane2;
  11. private JButton copyButton;
  12. ;
  13.  
  14. public Assn4()
  15. {
  16. super( "Code Format" );
  17.  
  18. Box box = Box.createHorizontalBox();
  19.  
  20. StringBuffer buffer1 = new StringBuffer( "Testsearchingstring" );
  21.  
  22. textPane1 = new JTextPane();
  23. textPane1.setText( buffer1.toString() );
  24. box.add( new JScrollPane( textPane1 ) );
  25.  
  26. String str = "searching";
  27. Pattern pattern = Pattern.compile(str, Pattern.CASE_INSENSITIVE);
  28. Matcher matcher = pattern.matcher( buffer1.toString() );
  29. if (matcher.matches() )
  30. {
  31. String result = matcher.replaceAll("<b>$1</b>");
  32. textPane1.setText( result );
  33.  
  34. }
  35.  
  36.  
  37.  
  38. copyButton = new JButton( "Copy >>>");
  39. copyButton.addActionListener(
  40.  
  41. new ActionListener()
  42. {
  43.  
  44. public void actionPerformed( ActionEvent event )
  45. {
  46.  
  47. textPane2.setText( textPane1.getText() );
  48.  
  49. }
  50. }
  51. );
  52.  
  53. box.add( copyButton );
  54.  
  55.  
  56. textPane2 = new JTextPane();
  57. textPane2.setEditable(true);
  58. box.add( new JScrollPane( textPane2 ) );
  59.  
  60. Container container = getContentPane();
  61. container.add( box );
  62.  
  63. this.setLocationRelativeTo( null );
  64. this.setSize( 600, 300 );
  65. this.setVisible( true );
  66. }
  67.  
  68. public static void main( String args[] )
  69. {
  70. Assn4 application = new Assn4();
  71.  
  72. application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
  73. }
  74.  
  75. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC