943,932 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3710
  • Java RSS
Oct 27th, 2004
0

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

Expand Post »
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

Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joebanks is offline Offline
7 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: purpose of exception handling 'finally' clause
Next Thread in Java Forum Timeline: Random Number Generator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC