Results 1 to 6 of 6

Thread: How to write a Caret Listener in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    253

    How to write a Caret Listener in Java?

    Hi Friends,
    I not have much knowledge about the Java. Now I want to get the focus where the user will type the next character and for that I want to use Caret Listener in my coding.!! But I don't know about that. So please tell me how to write a Caret Listener in Java.?? Also please tell me the methods that are used by the caret listener, if any. Hoping that someone would help me faster.!!

  2. #2
    Join Date
    Nov 2008
    Posts
    996

    Re: How to write a Caret Listener in Java?

    You should first think about the logic for using the caret listener so that you can use easily whenever you want. The carets are the point which tell user where the next character will insert. It is same as cursor in Windows. And the caret events occurs when the text component moves. The event also takes place when the selection in a text component changes. In short, if there is change of anything that is related with caret then that event takes place.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: How to write a Caret Listener in Java?

    I think that you should be knowing about the carets. So i though that providing you with the Interface and Class of carets would be more useful. First lets look at the CaretListener Interface. CaretListener has only one method. The following is the method for that :
    1. caretUpdate(CaretEvent) - This method is called when the caret in the listened-to component moves. It is also called when the selection in the listened-to component changes.

    Now lets look at the CaretEvent Class. The following are the methods used into that :
    1. int getDot() - This method returns the current location of the caret.
    2. int getMark() - This method returns the other end of the selection.
    3. Object getSource() - This method is used when you have to return the object that fired the event.

  4. #4
    Join Date
    Aug 2006
    Posts
    235

    Re: How to write a Caret Listener in Java?

    You can also detect the caret changes by another way. In that method you would have to attach a listener directly to the caret object. This method is useful because there is no need to attach that to the text component that manages the caret which was described in the earlier post. The important thing you will have to note is that the caret fires change events and not the caret events. So there is absolutely no need t write a caret listener. You would need to write a change listener rather than a caret listener.
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to write a Caret Listener in Java?

    I have provided you with a coding for the caret event handling from an application called TextComponentDemo :
    Code:
    ...
    CaretListenerLabel caretListenerLabel =
        new CaretListenerLabel("Status_Caret");
    ...
    textPane.addCaretListener(caretListenerLabel);
    ...
    protected class CaretListenerLabel extends JLabel
                                       implements CaretListener
    {
        ...
        public void caretUpdate(CaretEvent e) {
            displaySelectionInfo(e.getDot(), e.getMark());
        }
    
        protected void displaySelectionInfo(final int dot,
                                            final int mark) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    if (dot == mark) { 
                        ...
                    }
                });
            }
        }
    }

  6. #6
    Join Date
    Jul 2006
    Posts
    442

    Re: How to write a Caret Listener in Java?

    A caret listener must implement one method, caretUpdate. The following code is an example of CaretListenerLabel implementation of caretUpdate :
    Code:
    public void caretUpdate(CaretEvent e) {
        int dot = e.getDot();
        int mark = e.getMark();
        if (dot == mark) { 
            try {
                Rectangle caretCoords = textPane.modelToView(dot);
                setText("caret: text position: " + dot +
                        ", view location = [" +
                        caretCoords.x + ", " + caretCoords.y + "]" +
                        newline);
            } catch (BadLocationException ble) {
                setText("caret: text position: " + dot + newline);
            }
         } else if (dot < mark) {
            setText("selection from: " + dot + " to " + mark + newline);
         } else {
            setText("selection from: " + mark + " to " + dot + newline);
         }
    }
    "When they give you ruled paper, write the other way..." J.R.J.

Similar Threads

  1. How to write a Mouse Listener in Java?
    By PsYcHo 1 in forum Software Development
    Replies: 4
    Last Post: 13-02-2010, 03:47 AM
  2. How to write a Key Listener in Java?
    By N I C K in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 01:52 AM
  3. How to write an Item Listener in Java?
    By shivendra in forum Software Development
    Replies: 4
    Last Post: 13-02-2010, 01:13 AM
  4. How to write a Focus Listener in Java?
    By The Recruiter in forum Software Development
    Replies: 4
    Last Post: 12-02-2010, 07:05 AM
  5. How to write a Change Listener in Java?
    By Samarth in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 06:10 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,726,855,628.81243 seconds with 16 queries