Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-28-2007, 11:16 AM
Member
 
Join Date: May 2007
Posts: 10
Rep Power: 0
Valeriano is on a distinguished road
Red face How to select/highlight an entire row in JTextArea
In my JTextArea, the text consists of lines of text. When I click on a particular point in the JTextArea and if there is a line of text across
that point, I want that line of text is selected and high-lighted. How
to do this?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 05-28-2007, 11:19 AM
Member
 
Join Date: May 2007
Posts: 6
Rep Power: 0
karma is on a distinguished road
Default
Hi Valeriano,

Please try the following code:

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
 
public class TextAreaSelectLine extends JFrame implements MouseListener
{
	JTextArea textArea;
	Action selectLine;
 
	public TextAreaSelectLine()
	{
 
		textArea = new JTextArea( "one two\nthree four", 10, 30 );
		textArea.addMouseListener( this );
 
		JScrollPane scrollPane = new JScrollPane( textArea );
		getContentPane().add( scrollPane, BorderLayout.SOUTH );
		getContentPane().add( new JTextArea() );
 
		selectLine = getAction(DefaultEditorKit.selectLineAction);
 
	}
 
	private Action getAction(String name)
	{
		Action action = null;
		Action[] actions = textArea.getActions();
 
		for (int i = 0; i < actions.length; i++)
		{
			if (name.equals( actions[i].getValue(Action.NAME).toString() ) )
			{
				action = actions[i];
				break;
			}
		}
 
		return action;
	}
 
	public void mouseClicked(MouseEvent e)
	{
 
		if ( SwingUtilities.isLeftMouseButton(e)  && e.getClickCount() == 1)
		{
			selectLine.actionPerformed( null );
 
 
 
		}
	}
 
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
 
	public static void main(String[] args)
	{
		TextAreaSelectLine frame = new TextAreaSelectLine();
		frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
		frame.pack();
		frame.setVisible(true);
	}
}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-28-2007, 11:20 AM
Member
 
Join Date: May 2007
Posts: 10
Rep Power: 0
Valeriano is on a distinguished road
Default
Thanks Karma, it worked perfectly
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select Count Apple2 JavaServer Pages (JSP) and JSTL 1 04-29-2008 09:02 AM
How to synchronize blocks instead of entire methods Java Tip java.lang 0 04-09-2008 06:39 PM
Select specific cell Echilon New To Java 1 01-01-2008 07:47 AM
Adding custom highlight to JEditorPane andrewb AWT / Swing 0 06-22-2007 06:48 PM
GNU Source-highlight 2.7 levent Java Announcements 0 06-12-2007 08:39 AM


All times are GMT +2. The time now is 05:41 PM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org