1,105,242 Community Members

Ability to 'highlight' or 'select' row of text

Member Avatar
musikluver4
Junior Poster in Training
82 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am using JFrame for my GUI and I want to be able to select a row of text of a bunch of rows of text, one at a time. Like in Microsoft Outlook, you select an email and it selects the whole line for you of all the different fields, email address, date/time, size, etc. across the screen. How do I go about doing that in Java? What's the method called? I can't google it correctly, it doesn't know what I am looking for so I came on here hoping someone can help me.

I want to be able to just highlight or select a row of text, not necessarily do anything with it, just select it and have it give out a yellow color or something like that.

To have the ability to select, do I need to use a table like JTable?

Member Avatar
peter_budo
Code tags enforcer
7,934 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
0
 

What you are looking for is Caret, here is example

Member Avatar
musikluver4
Junior Poster in Training
82 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Does that do one clicks? I one click on a row of text and automatically highlights the whole row of text in a JTextArea box? The JTextArea is setEditable(false). The JTextArea is a box as a result of what a user inputted for information up above with JButtons and JTextFields. Then it calculated everything and display the results in the JTextArea. I want to be able to select one of those rows of texts.
I am doing a Mortgage Calculator, say select the first month of that section, highlighting the current months principal amount, the interest, the payment amount, the total payment amount and the endPrincipal amount before the next line is the start of the next month, etc. All clicking on that row of text with one click of the mouse.

Member Avatar
musikluver4
Junior Poster in Training
82 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
Member Avatar
musikluver4
Junior Poster in Training
82 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
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);
	}
}

Does anyone know how to tell this program to highlight text when there is text, instead of having a highlight at the beginning of the line to the end of the text. It does highlighting till the END of the text, but not for at the beginning of the text in the code above.

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

This sounds to me like you should be using a JTable, not a JTextArea.

Question Answered as of 3 Years Ago by peter_budo and JamesCherrill
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article