I am trying to highlight just one specific row in JTextArea
, but I have no idea as to going about it. I need to get the specific row and then highlight it. I've read the other posts, but I still do not understand how to bring it together to solve my problem...help would be much appreciated.
Asked
Active
Viewed 1.3k times
0

user1329572
- 6,176
- 5
- 29
- 39

Jeffrey Odametey
- 21
- 1
- 1
- 3
-
3Had you tried this wonderful POST, [How to highlight by @mKorbel](http://stackoverflow.com/questions/9650992/how-to-change-text-color-in-the-jtextarea/9651404#9651404) – nIcE cOw Apr 17 '12 at 13:01
-
2-1 for not researching this. First thing that popped on google was a code snippet that worked. – Youssef G. Apr 17 '12 at 13:01
-
I tried those but they do not solve my problem of highlighting a specific row , i'm trying to get a specific row and highlight it – Jeffrey Odametey Apr 17 '12 at 13:15
-
3*"still do not understand"* Do you understand how to ask a question? If so, please add one, and make it specific. *"they do not solve my problem of highlighting a specific row"* That only makes sense if the lines do not word-wrap. As an aside, if you 'cannot get it working' you are best off posting an [SSCCE](http://sscce.org/) of your best effort. I can't see that anyone capable of doing so, will spoon-feeding the answer to this very specific question. – Andrew Thompson Apr 17 '12 at 13:18
-
The question is Highlight one specific row/line in JTextArea – Jeffrey Odametey Apr 17 '12 at 13:34
-
I've tried those links you posted up guys already before coming here, @AndrewThompson – Jeffrey Odametey Apr 17 '12 at 13:35
-
4*"I've tried those links you posted"* 1) Nobody but me was notified of that comment. 2) **We are not psychic.** For that reason, it is a damn good idea to mention what you have tried, the threads you looked at, why they did not fulfill the spec. (etc.). ***SO is not a help-desk, but a Q&A site. The better specified question gets the better answer.*** – Andrew Thompson Apr 17 '12 at 13:41
-
@Jeffrey Odametey what did you talking about ???, do you joking ??? what do you expecting ???, I leaving this one for whales ... – mKorbel Apr 17 '12 at 13:46
-
Any more help will be much appreciated thnks guys. I'm a newbie at the graphics, i'm just trying to solve a little problem out of my program.I have no clue as to how to solve this one. – Jeffrey Odametey Apr 17 '12 at 13:54
3 Answers
10
Try your hands on this code example, and do ask if something is not clear :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class TextHighlight
{
private JTextArea tarea;
private JComboBox cbox;
private JTextField lineField;
private String[] colourNames = {"RED", "ORANGE", "CYAN"};
private Highlighter.HighlightPainter painter;
private void createAndDisplayGUI()
{
final JFrame frame = new JFrame("Text HIGHLIGHT");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5), "Highlighter JTextArea"));
tarea = new JTextArea(10, 10);
JScrollPane scrollPane = new JScrollPane(tarea);
contentPane.add(scrollPane);
JButton button = new JButton("HIGHLIGHT TEXT");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
int selection = JOptionPane.showConfirmDialog(
frame, getOptionPanel(), "Highlighting Options : ", JOptionPane.OK_CANCEL_OPTION
, JOptionPane.PLAIN_MESSAGE);
if (selection == JOptionPane.OK_OPTION)
{
System.out.println("OK Selected");
int lineNumber = Integer.parseInt(lineField.getText().trim());
try
{
int startIndex = tarea.getLineStartOffset(lineNumber);
int endIndex = tarea.getLineEndOffset(lineNumber);
String colour = (String) cbox.getSelectedItem();
if (colour == colourNames[0])
{
System.out.println("RED Colour");
painter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
tarea.getHighlighter().addHighlight(startIndex, endIndex, painter);
}
else if (colour == colourNames[1])
{
System.out.println("ORANGE Colour");
painter = new DefaultHighlighter.DefaultHighlightPainter(Color.ORANGE);
tarea.getHighlighter().addHighlight(startIndex, endIndex, painter);
}
else if (colour == colourNames[2])
{
System.out.println("CYAN Colour");
painter = new DefaultHighlighter.DefaultHighlightPainter(Color.CYAN);
tarea.getHighlighter().addHighlight(startIndex, endIndex, painter);
}
}
catch(BadLocationException ble)
{
ble.printStackTrace();
}
}
else if (selection == JOptionPane.CANCEL_OPTION)
{
System.out.println("CANCEL Selected");
}
else if (selection == JOptionPane.CLOSED_OPTION)
{
System.out.println("JOptionPane closed deliberately.");
}
}
});
frame.add(contentPane, BorderLayout.CENTER);
frame.add(button, BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel getOptionPanel()
{
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 2, 5, 5));
JLabel lineNumberLabel = new JLabel("Enter Line Number : ");
lineField = new JTextField(10);
JLabel colourLabel = new JLabel("Select One Colour : ");
cbox = new JComboBox(colourNames);
panel.add(lineNumberLabel);
panel.add(lineField);
panel.add(colourLabel);
panel.add(cbox);
return panel;
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TextHighlight().createAndDisplayGUI();
}
});
}
}
Here is the output of it :

nIcE cOw
- 24,468
- 7
- 50
- 143
-
1thanks for your example code this is what I did instead. `code RectanglePainter red = new RectanglePainter( Color.RED ); try { f1.textarea.getHighlighter().addHighlight(f1.textarea.getLineStartOffset(iline), f1.textarea.getLineEndOffset(iline), red);// } catch (BadLocationException ex) { ex.printStackTrace(); } ` – Jeffrey Odametey Apr 17 '12 at 15:00
3
If you are unable to select TextArea to TextField reason is button click causes the JTextArea to lose focus and thus not show its selection.
on button click event use btnImport.transferFocusBackward();
to resolve issue.

Andro Selva
- 53,910
- 52
- 193
- 240

Vishal Chaudhari
- 403
- 1
- 6
- 13
0
do like that : this is the text area swing of java
JTextArea area = new JTextArea();
int startIndex = area.getLineStartOffset(2);
int endIndex = area.getLineEndOffset(2);
painter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
area.getHighlighter().addHighlight(startIndex, endIndex, painter);