Results 1 to 5 of 5
Thread: Highlighting line in JTextArea
- 01-15-2011, 12:39 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
Highlighting line in JTextArea
Hello
I would like to highlight the line on which mouse clicked, something like in notepad++ and other code editors. I'm not quite sure how to do it. I'm new to Java and googled lots of pages but haven't found anything written in language for begginiers, could anybody direct me how to do it?
Thank you
-
You can get the clicked line via the viewToModel(Point p), getLineOfOffset(int offset), getLineStartOffset(int line), and getLineEndOffset(int line) methods. I'll have to review highlighting as it's been a while since I've done this. Of course you would need a MouseListener too. I suggest you construct an SSCCE that we can work with if you still need help. Please check out the link to tell you how to do this.
Last edited by Fubarable; 01-15-2011 at 12:54 PM.
- 01-15-2011, 01:37 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
Camickr (the Swing wizard) has put a bunch of great tools/classes online. One of them is this one; it does exactly what you want. Credits go to Camickr (and his site worth bookmarking).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
Edit: I was wrong. You wouldn't use the getLineOfOffset(int offset), getLineStartOffset(int line), and getLineEndOffset(int line) methods but rather the viewToModel(Point p) and modelToView(int offset) methods. For example:
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.BadLocationException; @SuppressWarnings("serial") public class HighlightTextAreaLine extends JPanel { private JTextArea tArea = new JTextArea(20, 40); public HighlightTextAreaLine() { tArea.setWrapStyleWord(true); tArea.setLineWrap(true); for (int i = 0; i < 400; i++) { tArea.append("abcdefg "); } add(new JScrollPane(tArea)); tArea.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { tAreaMousePressed(evt); } }); } private void tAreaMousePressed(MouseEvent evt) { try { int offset = tArea.viewToModel(evt.getPoint()); Rectangle rect = tArea.modelToView(offset); int startRow = tArea.viewToModel(new Point(0, rect.y)); int endRow = tArea.viewToModel(new Point(tArea.getWidth(), rect.y)); System.out.printf("Selected Offsets: [%d, %d]%n", startRow, endRow); tArea.select(startRow, endRow); } catch (BadLocationException e) { e.printStackTrace(); } } private static void createAndShowUI() { JFrame frame = new JFrame("HighlightTextAreaLine"); frame.getContentPane().add(new HighlightTextAreaLine()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
- 01-15-2011, 01:39 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
JTextArea How To get one Line only..?
By Dinesh_rockz in forum AWT / SwingReplies: 2Last Post: 01-12-2011, 05:25 PM -
JtextArea autoamtic update the last line
By sircurse in forum NetBeansReplies: 1Last Post: 10-28-2010, 07:08 PM -
Highlighting and retrieving current row text of jtextarea
By simmi in forum AWT / SwingReplies: 1Last Post: 04-25-2009, 03:50 PM -
[SOLVED] Line Highlighting
By Doctor Cactus in forum New To JavaReplies: 2Last Post: 03-26-2009, 03:55 PM -
How to always show the last line in my JTextArea?
By Ashley in forum New To JavaReplies: 1Last Post: 05-26-2007, 01:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks