Results 1 to 3 of 3
  1. #1
    Valeriano is offline Member
    Join Date
    May 2007
    Posts
    10
    Rep Power
    0

    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?

  2. #2
    karma is offline Member
    Join Date
    May 2007
    Posts
    6
    Rep Power
    0

    Default

    Hi Valeriano,

    Please try the following code:

    Java 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);
    	}
    }

  3. #3
    Valeriano is offline Member
    Join Date
    May 2007
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Select Count
    By Apple2 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-29-2008, 09:02 AM
  2. Replies: 0
    Last Post: 04-09-2008, 06:39 PM
  3. Select specific cell
    By Echilon in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 07:47 AM
  4. Adding custom highlight to JEditorPane
    By andrewb in forum AWT / Swing
    Replies: 0
    Last Post: 06-22-2007, 06:48 PM
  5. GNU Source-highlight 2.7
    By levent in forum Java Software
    Replies: 0
    Last Post: 06-12-2007, 08:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •