[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Text Area Mouse Position

Posted on 2006-04-09
8
Medium Priority
?
308 Views
Last Modified: 2008-03-06
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
0
Comment
Question by:BrianGEFF719
  • 4
  • 3
8 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 16411097
0
 
LVL 13

Accepted Solution

by:
Kelvin_King earned 1000 total points
ID: 16411530
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
0
 
LVL 13

Expert Comment

by:Kelvin_King
ID: 16411538
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Author Comment

by:BrianGEFF719
ID: 16413271
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
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 16413411
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
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 16413527
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( ));
   }

     
}
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 16413542
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( ));
   }

     
}
0
 
LVL 13

Expert Comment

by:Kelvin_King
ID: 16416813
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
    }
}
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question