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?

What you are looking for is Caret, here is example

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.

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.