show line numbers in JTextArea

does anyone know how to show line numbers in a JTextArea, or can you point me in the right direction?thanks
[121 byte] By [boblettoj99a] at [2007-11-26 19:19:36]
# 1
The right direction in this case would be toward the Swing forum. I'm amazed nobody has said that before, since all but one of your posts have been Swing related.
uncle_alicea at 2007-7-9 21:36:16 > top of Java-index,Java Essentials,New To Java...
# 2
[url http://www.googleityoumoron.com/?go=jtextarea+line+numbers]Some people are so stupid[/url]Ted.
ted_trippina at 2007-7-9 21:36:16 > top of Java-index,Java Essentials,New To Java...
# 3
I like that site!
DrLaszloJamfa at 2007-7-9 21:36:16 > top of Java-index,Java Essentials,New To Java...
# 4
i already looked in google! im not that dumb!but ok swing forum it is....thanks for the err...help
boblettoj99a at 2007-7-9 21:36:16 > top of Java-index,Java Essentials,New To Java...
# 5
I googled JTextArea line numbers and this was the first link: http://www.esus.com/docs/GetQuestionPage.jsp?uid=1326Was google broken when you tried, or what?
DrLaszloJamfa at 2007-7-9 21:36:16 > top of Java-index,Java Essentials,New To Java...
# 6

If I were going to create a web site for the sole purpose of calling people morons, I would at least make sure it worked without displaying a bunch of error messages. But maybe he's just proving his qualifications to call other people stupid, based on the theory that it takes one to know one.

uncle_alicea at 2007-7-9 21:36:16 > top of Java-index,Java Essentials,New To Java...
# 7
how do i encorporate that code into mine though?its so long and i dont know which bits to use...please, im really new to all this
boblettoj99a at 2007-7-9 21:36:16 > top of Java-index,Java Essentials,New To Java...
# 8

Don't panic. it's easy. That page gives two solutions.

Here is how to use the second, LineNumberBorder:

import javax.swing.*;

public class UsingLineNumberBorder {

public static void main(String[] args) {

JTextArea area = new JTextArea(40,60);

//HERE IT COMES

area.setBorder(new LineNumberedBorder(

LineNumberedBorder.LEFT_SIDE,

LineNumberedBorder.LEFT_JUSTIFY));

//THAT'S IT!

JFrame f = new JFrame("UsingLineNumberBorder");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new JScrollPane(area));

f.setSize(400,300);

f.setLocationRelativeTo(null);

f.setVisible(true);

}

}

DrLaszloJamfa at 2007-7-9 21:36:16 > top of Java-index,Java Essentials,New To Java...
# 9
oo coolio, thankyou
boblettoj99a at 2007-7-9 21:36:16 > top of Java-index,Java Essentials,New To Java...
# 10
aaah problem, when i use that code it overwrites the scrollpane i was using on the text area, when i put that code before the scrollpane, the scrollpane is shown but the line numbers are not!how do i get both working?
boblettoj99a at 2007-7-9 21:36:16 > top of Java-index,Java Essentials,New To Java...
# 11
Does the code in reply #8 works for you? Can you paste text into that text areaand have JScrollPane + line numbers + text work together?If so, you must be doing something different. Post a tiny (1 page)example program that demonstrates your problem.
DrLaszloJamfa at 2007-7-9 21:36:16 > top of Java-index,Java Essentials,New To Java...
# 12

here you go, sorry it took a while:

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.undo.*;

import javax.swing.text.*;

import java.util.*;

class Problem extends JFrame

{

final JTabbedPane jtp = new JTabbedPane();

final JScrollPane scrollPane;

public Problem()

{

super("TextED");

JTextArea textArea = new JTextArea(10, 10);

textArea.setBorder(new LineNumberedBorder(LineNumberedBorder.LEFT_SIDE, LineNumberedBorder.LEFT_JUSTIFY));

textArea.setLineWrap(true);

textArea.setWrapStyleWord(true);

scrollPane = new JScrollPane(textArea);

jtp.addTab("Untitled", scrollPane);

getContentPane().add(jtp);

}

public static void main ( String[] args )

{

Problem frm = new Problem();

frm.setSize( 560, 420 );

frm.setVisible( true );

}

}

boblettoj99a at 2007-7-9 21:36:16 > top of Java-index,Java Essentials,New To Java...
# 13

actually that bit works fine, but when i try to put it in my "new button" action listener it doesnt work!

fileNewMenuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JTextArea textArea = new JTextArea(1, 10);

textArea.setBorder(new LineNumberedBorder(LineNumberedBorder.LEFT_SIDE, LineNumberedBorder.LEFT_JUSTIFY));

textArea.setLineWrap(true);

textArea.setWrapStyleWord(true);

textAreas.add(textArea);

JScrollPane newScrollPane = new JScrollPane(textArea);

int index = jtp.getTabCount();

jtp.addTab("Untitled " + (index + 1), newScrollPane);

jtp.setSelectedIndex(index);

contents.add(jtp);

}

});

boblettoj99a at 2007-7-9 21:36:16 > top of Java-index,Java Essentials,New To Java...
# 14
You code behaves for me. What does it do wrong, specifically?
DrLaszloJamfa at 2007-7-9 21:36:16 > top of Java-index,Java Essentials,New To Java...
# 15
sorry in the latest code when i say it doesnt work i mean the numbers dont show up, not the scrollpane
boblettoj99a at 2007-7-9 21:36:18 > top of Java-index,Java Essentials,New To Java...
# 16
A guess, since you haven't posted a complete program in that last reply:Comment out the line://contents.add(jtp);
DrLaszloJamfa at 2007-7-9 21:36:18 > top of Java-index,Java Essentials,New To Java...
# 17
but i used that in my "open button" action listener and it works fine...the tab is being added in the above code but the textArea border is not being shown
boblettoj99a at 2007-7-9 21:36:18 > top of Java-index,Java Essentials,New To Java...
# 18
? Post a minimal, complete example program that demonstrates your problem.
DrLaszloJamfa at 2007-7-9 21:36:18 > top of Java-index,Java Essentials,New To Java...
# 19
errr i dunno how i can keep this minimal, its pretty big!maybe a screenshot would help?
boblettoj99a at 2007-7-9 21:36:18 > top of Java-index,Java Essentials,New To Java...
# 20
> maybe a screenshot would help?Not really, it's kinda hard to debug a screenshot.
hunter9000a at 2007-7-9 21:36:18 > top of Java-index,Java Essentials,New To Java...
# 21
Inside every "big" problem, there's a small one trying to escape.
DrLaszloJamfa at 2007-7-9 21:36:18 > top of Java-index,Java Essentials,New To Java...
# 22

ok this shows my problem:

http://i41.photobucket.com/albums/e254/frahasio/screenie.jpg

And here is the code for each actionlistener(you'll understand once you've looked at the picture)

NEW:

fileNewMenuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JTextArea textArea = new JTextArea(1, 10);

textArea.setBorder(new LineNumberedBorder(LineNumberedBorder.LEFT_SIDE, LineNumberedBorder.LEFT_JUSTIFY));

textArea.setLineWrap(true);

textArea.setWrapStyleWord(true);

textAreas.add(textArea);

JScrollPane newScrollPane = new JScrollPane(textArea);

int index = jtp.getTabCount();

jtp.addTab("Untitled " + (index + 1), newScrollPane);

jtp.setSelectedIndex(index);

getContentPane().add(jtp);

}

});

OPEN (only the relevant bit):

JTextArea textArea = new JTextArea(1, 10);

textArea.setBorder(new LineNumberedBorder(LineNumberedBorder.LEFT_SIDE, LineNumberedBorder.LEFT_JUSTIFY));

textArea.setLineWrap(true);

textArea.setWrapStyleWord(true);

textArea.setText(input);

textAreas.add(textArea);

JScrollPane newScrollPane = new JScrollPane(textArea);

jtp.addTab(title, newScrollPane);

int index = jtp.getTabCount();

jtp.setSelectedIndex(index - 1);

contents.add(jtp);

boblettoj99a at 2007-7-9 21:36:18 > top of Java-index,Java Essentials,New To Java...
# 23
> getContentPane().add(jtp);isn't jtp already in the content pane?Again, I wish I could help, but ...
DrLaszloJamfa at 2007-7-9 21:36:18 > top of Java-index,Java Essentials,New To Java...
# 24
You seem to be creating new JTextAreas in your action listener there. That's a peculiar way of doing things. The normal way would be to clear the existing JTextArea and refill it with whatever data you had in mind.
DrClapa at 2007-7-9 21:36:18 > top of Java-index,Java Essentials,New To Java...
# 25
i was just trying different things when i added getContentPane().add there but i does the same thing as contents.add()i cant see what the problem could be as the code is practically identical apart from the title of the tabs
boblettoj99a at 2007-7-9 21:36:18 > top of Java-index,Java Essentials,New To Java...
# 26
> ok this shows my problem:Please get familiar with the concept of an [url= http://mindprod.com/jgloss/sscce.html]SSCCE[/url]. You'll typically get a better answer in less time.~
yawmarka at 2007-7-9 21:36:18 > top of Java-index,Java Essentials,New To Java...
# 27

> You seem to be creating new JTextAreas in your action

> listener there. That's a peculiar way of doing

> things. The normal way would be to clear the existing

> JTextArea and refill it with whatever data you had in

> mind.

i need to create a new textarea as i am putting it a new tab (see screenshot^)

boblettoj99a at 2007-7-9 21:36:18 > top of Java-index,Java Essentials,New To Java...
# 28

Here's my 30 second take on your code. It's 24 lines long, sopping wet, and it works:import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class UsingLineNumberBorder {

public static void main(String[] args) {

final JTabbedPane tabbed = new JTabbedPane();

JButton btn = new JButton("new");

btn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent evt) {

JTextArea area = new JTextArea(40,60);

area.setBorder(new LineNumberedBorder(

LineNumberedBorder.LEFT_SIDE, LineNumberedBorder.LEFT_JUSTIFY));

tabbed.addTab("...", new JScrollPane(area));

}

});

JFrame f = new JFrame("UsingLineNumberBorder");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(tabbed, BorderLayout.CENTER);

f.getContentPane().add(btn, BorderLayout.SOUTH);

f.setSize(800,600);

f.setVisible(true);

}

}

Could you at least edit this to demonstrate your problem?

DrLaszloJamfa at 2007-7-9 21:36:18 > top of Java-index,Java Essentials,New To Java...
# 29
sorry this took so long, i finally made that example....not that little...but it was as small as i could get it so that it included everything...and guess what..It worked!all i did was copy and paste bits from my code, i dont understand!
boblettoj99a at 2007-7-9 21:36:18 > top of Java-index,Java Essentials,New To Java...
# 30
prob solv
DrLaszloJamfa at 2007-7-9 21:36:22 > top of Java-index,Java Essentials,New To Java...
# 31
omg, i just copied the code from my example back into the original code (bare in mind this is e-x-a-c-t-l-y the same code), and it works now!thanks to everyone for the helphow very odd!
boblettoj99a at 2007-7-9 21:36:22 > top of Java-index,Java Essentials,New To Java...