Text Area Mouse Position

I need the following code:

A text area to be created that is 60col x 20 rows, I need the font in this text area to be fixed width!! I need to know that at all times the most 1 line can contain is 60bytes.

I need to code to figure out where the mouse is in this text area when the mouse moves over it, I need to know the column and row where the mouse is, not the x,y coordinates. I also need the Column and Row when the user clicks somewhere in the text area.

I'm not familiar with Java, however, I cant image this being all that difficult, calculate width of fixed width font we are using, do same with height, then once you know x,y coordinates in the text area, figuring out Column and Row cant be too difficult.


-Brian
LVL 19
BrianGEFF719Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
Kelvin_KingCommented:
How do you intend to split up your textarea into rows and columns ? As far as I know, there isn't any easy way to divide a textarea.

Basically what you'll need to achieve this is to use a mouseLIstiner to capture the mouseEvents, like double-click, cursor position etc.

http://java.sun.com/docs/books/tutorial/uiswing/events/mousemotionlistener.html

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kelvin_KingCommented:
mouseListiner is this link, the previous one was motionListiner, sorry. But you might want to implement both to make your application more robust.

http://java.sun.com/docs/books/tutorial/uiswing/events/mouselistener.html
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

BrianGEFF719Author Commented:
Can anyone provide me with code? Like I said I am not a Java Programmer. I plan to use a two dimensional array to store a 'copy' of the text area, thats why I'm conconcered with columns and rows.


Brian
BrianGEFF719Author Commented:
Can anyone just show me how to create an applet that contains a text area that is 60x20? I have NO java experience and your links to classes do not help me...


Brian
BrianGEFF719Author Commented:
Here is the code I've patched together from examples....it does not work.


import java.applet.Applet;
import java.awt.*;          
import java.awt.event.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;


public class mathDesign extends Applet implements MouseListener
{  
   Button    drawLineButton           = new Button("Draw Line");
   TextArea  textCanvas               = new TextArea(
                                            "",
                                            20,
                                            60,
                                            TextArea.SCROLLBARS_NONE
                                          );
     
   public void init( )
   {      
   
      add(drawLineButton);
      addNewLine( );
     
      textCanvas.setEditable(false);
      add(textCanvas);
      addNewLine( );

      drawLineButton.addActionListener(new drawLineListner( ));
      textCanvas.addMouseListener( this );
   }
 
   
   class drawLineListner implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
      }                  
   }
   
   private void addHorizontalLine(Color c)
   {  
      Canvas line = new Canvas( );
      line.setSize(10000,1);
      line.setBackground(c);
      add(line);
   }
   
   private void addNewLine( )
   {  
      addHorizontalLine(getBackground( ));
   }

     
}
BrianGEFF719Author Commented:
OK...Can you modify this so that the text area uses a fixed width font that will work on browsers using IE, Firefox, Netscape, etc...And can you show me how to get the width of that font so I can calculate rows & cols. Secondly, can you get the mouse listener working so I know where the user clicks (x,y) and where they move the mouse...I need both clicks and mouse movement...


import java.applet.Applet;
import java.awt.*;          
import java.awt.event.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.FontMetrics;
import java.awt.Font;


public class mathDesign extends Applet  
{  
   
   int numRows = 20;
   int numCols = 60;
   int txtWidth;
   int txtHeight;
   
   
   
   Button    drawLineButton           = new Button("Draw Line");
   TextArea  textCanvas               = new TextArea(
                                            "",
                                            numRows,
                                            numCols,
                                            TextArea.SCROLLBARS_NONE
                                          );
   
   
     
   public void init( )
   {      
   
      add(drawLineButton);
      addNewLine( );
     
      textCanvas.setEditable(false);
      add(textCanvas);
      addNewLine( );

      drawLineButton.addActionListener(new drawLineListner( ));
     // textCanvas.addMouseListener( this );
   }
 
   
   class drawLineListner implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
      }                  
   }
   
   private void addHorizontalLine(Color c)
   {  
      Canvas line = new Canvas( );
      line.setSize(10000,1);
      line.setBackground(c);
      add(line);
   }
   
   private void addNewLine( )
   {  
      addHorizontalLine(getBackground( ));
   }

     
}
Kelvin_KingCommented:
Basically, when you set the row and column of a textarea, each square is roughly the size of a "M" in that font, so I'm still not too sure what you are trying to achieve, perhaps you could elobrate. For the time being, I have helped you implement a mouseListener and mouseMotionListener and also show you how to change the font in your text area. Have a look at maybe from there you can achieve what you set out in the begining.


///------------------ CODE STARTS HERE ----------------------------

import java.applet.Applet;
import java.awt.*;          
import java.awt.event.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.FontMetrics;
import java.awt.Font;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;


public class mathDesign extends Applet  implements MouseListener, MouseMotionListener
{  
   
   int numRows = 20;
   int numCols = 60;
   int txtWidth;
   int txtHeight;
   
   Font font;
   FontRenderContext frc;
   Button    drawLineButton           = new Button("Draw Line");
   TextArea  textCanvas               = new TextArea(
                                            "",
                                            numRows,
                                            numCols,
                                            TextArea.SCROLLBARS_NONE
                                          );
   
   
     
   public void init( )
   {      
   
        font = new Font("Verdana", Font.BOLD, 12);  //set font to Verdana
        frc = new FontRenderContext(font.getTransform(), false, false);
      add(drawLineButton);
      addNewLine( );
      textCanvas.setEditable(false);
        textCanvas.addMouseListener(this);
        textCanvas.addMouseMotionListener(this);
        textCanvas.setFont(font);
      add(textCanvas);
      addNewLine( );

      drawLineButton.addActionListener(new drawLineListner( ));
     // textCanvas.addMouseListener( this );
   }
 
   
   class drawLineListner implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
      }                  
   }
   
   private void addHorizontalLine(Color c)
   {  
      Canvas line = new Canvas( );
      line.setSize(10000,1);
      line.setBackground(c);
      add(line);
   }
   
   private void addNewLine( )
   {  
      addHorizontalLine(getBackground( ));
   }
   
   public void mousePressed(MouseEvent e) {
  }

  public void mouseReleased(MouseEvent e) {
  }

  public void mouseEntered(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
  }

  public void mouseClicked(MouseEvent e) {
  //you should implement your own functionlity here
   textCanvas.append("(" + e.getX() + ", " + e.getY() + ")");
  }
 
    public void mouseMoved(MouseEvent e) {
       textCanvas.append("Mouse moved"); //you should implement your own functionlity here
    }

    public void mouseDragged(MouseEvent e) {
       textCanvas.append("Mouse dragged");//you should implement your own functionlity here
    }
}
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.