Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java: Line numbers in a text area

Options
  • 05-02-2004 2:27pm
    #1
    Posts: 0


    Hey all,

    I have a simple text editor with basic read/write/save functionality, using a text area as the main text component.

    However, I now want to have each line start with the line number, then show that line...is there a "good" way to do this?

    I've been looking through the Sun's website on styled documents, and wonder would this be useful for my requirements?

    I was thinking of just adding the number to each line, with a '\t' to separate them, but I can't help thinking there is a more robust approach to such a problem.

    I'm not looking for homework done, just either pointers in the right direction or an answer, so i may implement it myself.

    Thanks all for the help.


Comments

  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Check out the source code for Jext (http://www.jext.org)

    Especially take a look at the JEditTextArea and Gutter classes. The Gutter is where the line numbers are painted.

    Hope this helps! Open Source software is so good for learning this type of thing.


  • Registered Users Posts: 1,865 ✭✭✭Syth


    Maybe you could create another, seperate text box or label on the side, which would show the line numbers. You'd have to make sure it matches up with the main.


  • Posts: 0 [Deleted User]


    THanks for the tips lads, i'll look into that, with the jext program.

    Cheers! :)


  • Registered Users Posts: 885 ✭✭✭clearz


    If you wand to code your own here is something to get you started. It is far from complete and full of bugs so it should be a challange

    [php]

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class LineNumberTest extends JFrame
    {
    public LineNumberTest()
    {
    NumberedTextArea ja = new NumberedTextArea(20,20);
    try
    {
    setSize(800,800);
    getContentPane().setLayout(new FlowLayout());
    getContentPane().add(ja);
    show();
    }
    catch(Exception e){System.out.println(e);};
    }
    public static void main(String [] args)
    {
    new LineNumberTest();
    }

    }


    class NumberedTextArea extends JPanel implements KeyListener
    {
    JTextArea jta;
    NumberPanel np;
    public NumberedTextArea()
    {
    super();
    addKeyListener(this);
    }
    public NumberedTextArea(int w, int h)
    {
    jta = new JTextArea(w,h);
    np = new NumberPanel();
    setLayout(new BorderLayout());
    this.add(np, BorderLayout.WEST);
    this.add(jta, BorderLayout.CENTER);
    jta.addKeyListener(this);
    }

    public void keyTyped(KeyEvent e)
    {
    if((int)e.getKeyChar() == 10)
    {
    np.setRows(jta.getLineCount());
    }
    else if((int)e.getKeyChar() == 8 && jta.getLineCount() !=1)
    {
    np.setRows(jta.getLineCount()-1);

    }
    else if((int)e.getKeyChar() == 127 && jta.getLineCount() !=1)
    {
    np.setRows(jta.getLineCount()-1);

    }

    }
    public void keyPressed(KeyEvent e)
    {

    }
    public void keyReleased(KeyEvent e)
    {
    try
    {
    System.out.println(jta.getLineEndOffset(1));
    }
    catch(Exception ex){System.out.println(e);}
    }

    }

    class NumberPanel extends JPanel
    {
    int rows = 1;
    public NumberPanel()
    {
    this.setPreferredSize(new Dimension(20,100));
    setBackground(Color.white);
    }
    public void setRows(int r)
    {
    this.rows = r;
    repaint();
    }
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    for(int i=1;i<=rows;i++)
    g.drawString(Integer.toString(i),5,i*16-3);
    }

    }

    [/php]


Advertisement