Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to get cursor position within text area  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

i want to keep track of cordinates of cursor in textarea
is some method there with keylistener interface

regards
srijan
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srijan,
look at the following example..it's not 100 percent what you want, but I think you can use it if you rebuild it some...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Ex3.2 extends JApplet
{
// Fields ------------------------------------------
JLabel theRedLabel;
JLabel theGreenLabel;
JLabel theYellowLabel;
Font theFont;
Color theBrightRed;
Color theBrightGreen;
Color theBrightYellow;
Color theBlack;
Container theContainer;
// The init() method -------------------------------
public void init()
{
// 1. Create a font
theFont = new Font("SansSerif", Font.BOLD, 32);
// 2. Create a red, green and blue label ----
theRedLabel = new JLabel("Red");
theRedLabel.setFont(theFont);
theBrightRed = new Color(255, 0, 0);
theRedLabel.setForeground(theBrightRed);
theRedLabel.setBounds ( 50, 0, 200, 100);
// The green label --------------------------
theGreenLabel = new JLabel("Green");
theGreenLabel.setFont(theFont);
theBrightGreen = new Color(0, 255, 0);
theGreenLabel.setForeground(theBrightGreen);
theGreenLabel.setBounds (150, 100, 200, 100);
// The yellow label -------------------------
theYellowLabel = new JLabel("Output");
theYellowLabel.setFont(theFont);
theBrightYellow = new Color(255, 255, 0);
theYellowLabel.setForeground(theBrightYellow);
theYellowLabel.setBounds(250, 200, 200, 100);
// 3. Get the applet's container ------------
theContainer = this.getContentPane();
// 4. Set the container layout to null ------
theContainer.setLayout(null);
// 5. Create a new Color object (black) -----
theBlack = new Color(0, 0, 0);
// 6. Set the background of the container ---
theContainer.setBackground(theBlack);
// 7. Add each of the three JLabels ---------
theContainer.add(theRedLabel);
theContainer.add(theGreenLabel);
theContainer.add(theYellowLabel);
// 2. Send mouse messages to the MouseHandler class
theRedLabel.addMouseListener(new MouseHandler());
theGreenLabel.addMouseListener(new MouseHandler());
theYellowLabel.addMouseListener(new MouseHandler());
}
// 3. The MouseHandler class must extend MouseAdapter
class MouseHandler extends MouseAdapter
{
// 4. MouseHandler only responds to mouse clicks
public void mouseClicked(MouseEvent evt)
{
int x = evt.getX();
int y = evt.getY();

//I use getComponent here instead of getSource,cuz
// I know the event is fired by a component and because
// else i would have to define source as an Object
// and Object doesnt have a method getLocationOnScreen()
Component source = evt.getComponent();
Point p = source.getLocationOnScreen();
int i = p.x;
int j = p.y;
int m = x + i;
int n = y + j;
theYellowLabel.setText("" + m + "," + n);
System.out.println("" + m + "," + n);
}
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!