aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Getting the line-number of a text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Getting the line-number of a text" Watch "Getting the line-number of a text" New topic
Author

Getting the line-number of a text

Lars Tode
Ranch Hand

Joined: Feb 07, 2002
Posts: 45
Hi,
I've a JTextArea with 20 rows. In that JTextArea I've more than 20 rows of text. I'd place the JTextArea in a JScrollPane so that I can scroll the area.
Now I want to know when I'm click with my mouse in the area which row I hit (the real row-number of that text, mean that the number can be higher than 20).
Is it possible to get it?
greetx
Lars
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Lars,
It might be possible, but I have never done it. Possible way:
1. Use ViewToModel to get text position
2. Count how many newline chars are in the textArea up to that point
Good Luck,
Manfred.
Lars Tode
Ranch Hand

Joined: Feb 07, 2002
Posts: 45
Hi Manfred,
thanks for your help, i'll tried that way.
May be i post that code if i done it.
greetx
Lars
Premila
Greenhorn

Joined: Sep 09, 2001
Posts: 12
Hi,
I have pasted below the code
for getting the line number of the text
in a JTextArea added to a JScrollPane.
Hope this serves your purpose.
Best Wishes,
premila
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class LineCount extends javax.swing.JFrame {
JTextArea ta=new JTextArea();
JScrollPane spane=new JScrollPane();

public LineCount() {
initComponents();
this.getContentPane().setLayout(null);
spane.setBounds(20,20,200,100);
ta.setLineWrap(true);
ta.setEditable(false);
String s="I've a JTextArea with 20 rows. In that JTextArea I've more than 20 rows of text. I'd place the JTextArea in a JScrollPane so that I can scroll the area.Now I want to know when I'm click with my mouse in the area which row I hit (the real row-number of that text, mean that the number can be higher than 20).Is it possible to get it? ";
ta.setText(s);
ta.addMouseListener(new MouseListen());
spane.getViewport().add(ta);
spane.setAutoscrolls(true);
this.getContentPane().add(spane);

}


private void initComponents() {//GEN-BEGIN:initComponents

addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

pack();
}//GEN-END:initComponents


private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
System.exit(0);
}//GEN-LAST:event_exitForm


public static void main(String args[]) {
LineCount lc=new LineCount();
lc.setSize(300,300);
lc.show();
}

class MouseListen implements MouseListener {

public void mouseExited(java.awt.event.MouseEvent mouseEvent) {
}

public void mouseReleased(java.awt.event.MouseEvent mouseEvent) {
}

public void mousePressed(java.awt.event.MouseEvent mouseEvent) {
}

public void mouseClicked(java.awt.event.MouseEvent me) {
int x=me.getX();
int y=me.getY();
int pos=ta.viewToModel(new java.awt.Point(x,y));
Font f=ta.getFont();
FontMetrics fm=ta.getFontMetrics(f);
int height=fm.getHeight();
System.out.println("Current LineNumber is "+( (y/height)+1 ));
}

public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {
}

}

// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables

}
Lars Tode
Ranch Hand

Joined: Feb 07, 2002
Posts: 45
Hi Premila,
I got a solution few days before
thanks 4 ur help

greetx
Lars
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting the line-number of a text
 
Similar Threads
TextArea
Restricting the Text Area to contain 256 characters using JS
can't change rows or columns in JTestArea
Tile constraints
Finding current line number - textarea