Results 1 to 5 of 5
  1. #1
    misiom1 is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by misiom1 View Post
    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?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

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

  5. #5
    misiom1 is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the link, I've just found it a few minutes ago and already implemented, it works perfectly !

Similar Threads

  1. JTextArea How To get one Line only..?
    By Dinesh_rockz in forum AWT / Swing
    Replies: 2
    Last Post: 01-12-2011, 05:25 PM
  2. JtextArea autoamtic update the last line
    By sircurse in forum NetBeans
    Replies: 1
    Last Post: 10-28-2010, 07:08 PM
  3. Replies: 1
    Last Post: 04-25-2009, 03:50 PM
  4. [SOLVED] Line Highlighting
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 03-26-2009, 03:55 PM
  5. How to always show the last line in my JTextArea?
    By Ashley in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:01 PM

Posting Permissions

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