Solved

Keep track of line number next to JTextArea

Posted on 2004-09-16
12
330 Views
Last Modified: 2012-06-27
I'm working on a simple java IDE for personal use, but I'm having trouble when it comes to displaying line numbes for the file you're editing.

Here's how the IDE I currently use displays line numbers:
http://gaming.cyntaks.com/lines.gif

And here's a screen of the IDE i'm writing:
http://gaming.cyntaks.com/RDE.gif

I can't seem to figure out how I'm going to go about displaying the line numbers next to my JTextArea.  When I was designing my IDE, the plan was to subclass a JPanel, and simply draw the line numbers in the area to the left of the textarea displaying, and then offset the y when you scroll the textarea.  However, I am worried that my line numbers will get offset, because I don't think that the row height in JTextArea's are consistant across operating systems.

One other option that I considered was to use another JTextArea to display the line numbers.  That way, the rows for both of the JTextArea's will line up. When I scroll the textarea containing the code, the textarea containing the line numbers would scroll accordingly.  However, I don't want to see a scrollbar next to the line numbers.  If I set the scrollbar's visibility to false, will the textarea still reserve room for it, and can I still scroll the textarea up and down?

Thanks
0
Comment
Question by:Breadstick
  • 4
  • 3
  • 2
  • +1
12 Comments
 
LVL 18

Expert Comment

by:armoghan
ID: 12081943
Use JEditorPane instead
this link may help
http://www.developer.com/java/other/article.php/3318421
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12081946
0
 
LVL 2

Author Comment

by:Breadstick
ID: 12089360
Thanks, I got it working, but I'm still having a problem.

Everything's working well, except long lines of text are wrapping, and it's counting the wrapped text as another line... Here's a screenshot of what I mean:
http://gaming.cyntaks.com/RDE_2.gif

And here's some of my code:

[code]
 class NumberedEditorKit extends StyledEditorKit
  {
      public ViewFactory getViewFactory()
      {
          return new NumberedViewFactory();
      }
  }

  class NumberedViewFactory implements ViewFactory
  {
      public View create(Element elem)
      {
          String kind = elem.getName();
          if (kind != null)
              if (kind.equals(AbstractDocument.ContentElementName))
              {
                  return new LabelView(elem);
              }
              else if (kind.equals(AbstractDocument.ParagraphElementName))
              {
  //              return new ParagraphView(elem);
                  return new NumberedParagraphView(elem);
              }
              else if (kind.equals(AbstractDocument.SectionElementName))
              {
                  return new BoxView(elem, View.Y_AXIS);
              }
              else if (kind.equals(StyleConstants.ComponentElementName))
              {
                  return new ComponentView(elem);
              }
              else if (kind.equals(StyleConstants.IconElementName))
              {
                  return new IconView(elem);
              }
          // default to text display
          return new LabelView(elem);
      }
  }

  class NumberedParagraphView extends ParagraphView
  {
    public short NUMBERS_WIDTH=30;
 
    public NumberedParagraphView(Element e)
    {
        super(e);
        short top = 0;
        short left = 0;
        short bottom = 0;
        short right = 0;
        this.setInsets(top, left, bottom, right);
    }
 
    protected void setInsets(short top, short left, short bottom,short right)
    {
      super.setInsets(top, (short)(left+NUMBERS_WIDTH), bottom, right);
    }
 
    public void paintChild(Graphics g, Rectangle r, int n)
    {
        super.paintChild(g, r, n);
        Graphics2D g2 = (Graphics2D)g;
        int previousLineCount = getPreviousLineCount();
        int numberX = r.x - getLeftInset();
        int numberY = r.y + r.height - 5;
        String str = Integer.toString(previousLineCount + n + 1);
        // draw the line number, align it to the right
        g2.drawString(str, numberX - g2.getFontMetrics().stringWidth(str) + 25, numberY);
        g2.setColor(Color.lightGray);
        //draw a vertical line next to it
        g2.drawLine(r.x-3, r.y, r.x-3, r.y + r.height);
    }
 
    public int getPreviousLineCount()
    {
        int lineCount = 0;
        View parent = this.getParent();
        int count = parent.getViewCount();
        for (int i = 0; i < count; i++)
        {
            if (parent.getView(i) == this)
            {
                break;
            }
            else
            {
                lineCount += parent.getView(i).getViewCount();
            }
        }
        return lineCount;
    }
  }
...
        JEditorPane fileText = new JEditorPane();
        fileText.setEditorKit(new NumberedEditorKit());
        JScrollPane fileContainer = new JScrollPane(fileText,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
...

[/code]

The JEditorPane is in a JScrollPane, but it's not using the horizontal scrollbar, even though i've explicitly turned it on.

I need to either get the text to not wrap, or I need lines that are a continuation of one long line to not be counted when displaying the line count.

Any ideas?
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 6

Expert Comment

by:expertmb
ID: 12102563
public NumberedParagraphView(Element e) {
        super(e);
        short top = 0;
        short left = 0;
        short bottom = 0;
        short right = 0;//+1000;
        this.setInsets(top, left, bottom, right);
    }

0
 
LVL 2

Author Comment

by:Breadstick
ID: 12105762
Thanks, but that doesn't help me.  I ended up using a different approach involving the JScrollPane's row header.
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12108789
>>I ended up using a different approach involving the JScrollPane's row header.
please post the code, curious to know how it solved your problem.

mb...
0
 
LVL 2

Author Comment

by:Breadstick
ID: 12109911
   fileContainer.setRowHeaderView(new LineNumberView(fileText));

Where fileContainer is the JScrollPane I'm using, fileText is my JTextPane, and LineNumberView is a component written by somebody named "uncle_alice" that paints the line numbers.  I found help at:
http://forum.java.sun.com/thread.jsp?forum=57&thread=555641&tstart=15&trange=15
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12109956
0
 
LVL 2

Author Comment

by:Breadstick
ID: 12153199
>> plz post the working complete code. as i posted

Here's the thread with the complete working LineNumberView code I'm using for the row header in my JScrollPane:
http://forum.java.sun.com/thread.jsp?forum=57&thread=547941

This is where I use the LineNumberView:
theScrollPaneMyTextComponentIsIn.setRowHeaderView(new LineNumberView(myTextComponent));

Here's the thread where I originally asked the question and was walked through getting it to work:
http://forum.java.sun.com/thread.jsp?forum=57&thread=555641

Here's a screenshot of it working:
http://gaming.cyntaks.com/RDE3.gif

Thanks for your help, but I solved my problem using a completely different approach, which was more convenient for what I was trying to accomplish.
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 12154912
Closed, 95 points refunded.
modname
Community Support Moderator
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 44
Windows 10 IE Certificate Issue 10 47
by zero exception 10 41
mysql jsp example issue 32 38
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question