954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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

import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;
import java.util.regex.*;
import javax.swing.*;

public class Assn4 extends JFrame{
	

	private JTextPane textPane1, textPane2;
	private JButton copyButton;
;
	
	public Assn4()
	{
		super( "Code Format" );
		
		Box box = Box.createHorizontalBox();
		
		StringBuffer buffer1 = new StringBuffer( "Testsearchingstring" );
	
		textPane1 = new JTextPane();
		textPane1.setText( buffer1.toString() );
		box.add( new JScrollPane( textPane1 ) );
			
		String str = "searching";
		Pattern pattern = Pattern.compile(str, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher( buffer1.toString() );
		if (matcher.matches() )
		{
				String result = matcher.replaceAll("<b>$1</b>");
				textPane1.setText( result );
			
		}
		
		
				
		copyButton = new JButton( "Copy >>>"); 
		copyButton.addActionListener(
			
			new ActionListener()
			{
		
				public void actionPerformed( ActionEvent event )
				{
					
					textPane2.setText( textPane1.getText() );	
					
				}
			}
		);
		
		box.add( copyButton );
		
		
		textPane2 = new JTextPane();
		textPane2.setEditable(true);
		box.add( new JScrollPane( textPane2 ) );
		
		Container container = getContentPane();
		container.add( box );
		
		this.setLocationRelativeTo( null );
		this.setSize( 600, 300 );
		this.setVisible( true );	
	}
	
	public static void main( String args[] )
	{
		Assn4 application = new Assn4();
		
		application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
	}

}
joebanks
Newbie Poster
7 posts since Sep 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You