Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2004

Tip: Looking for answers? Try searching our database.

Move cursor after insert in JTextPane

Thread view: 
Ike - 22 Feb 2004 18:17 GMT
I have a JDialog with a JList on it wherin if something is selected in the
JList, and a JButton ("Add") is clicked, it inserts the String selected in
the JList into a JTextPane residing in a componenent which invoked my
JDialog. All works fine. However, after inserting the text into the
JTextPane, I am trying to advance the cursor to right after the inserted
text. For some reason, I am not able to do this using the following code.

This shouldn't be so hard, should it, lol? Does anyone see where I am
screwing this up? -Ike

class MyActionListener implements ActionListener{
       public void actionPerformed(ActionEvent ae){
           //called when "add" button hit
           if(ae.getActionCommand().equals("OK")){
               int i = wordprocessorpanel.m_monitor.getCaretPosition();
               String s = (String)jList1.getSelectedValue();
               MoveCursor ob=new MoveCursor(wordprocessorpanel,i,s);
               SwingUtilities.invokeLater(ob);
         }
   }

class MoveCursor implements  Runnable{
       Thread t;
       WordProcessorPanel wordprocessorpanel;
//wordprocessorpanel.m_monitor is the JTextPane
       int i;
       String s;
       MoveCursor(WordProcessorPanel wordprocessorpanel, int i, String s){
           this.wordprocessorpanel=wordprocessorpanel;
           this.i=i;
           this.s=s;
           t = new Thread(this, "MoveCursor");
       }
       public void run(){
           try{
               wordprocessorpanel.m_monitor.grabFocus();

wordprocessorpanel.m_monitor.getStyledDocument().insertString(i,s,null);
               int e = i+s.length();
               wordprocessorpanel.m_monitor.moveCaretPosition(e);//creates
a selection from i to e

wordprocessorpanel.m_monitor.moveCaretPosition(e);//selection is now null
               wordprocessorpanel.m_monitor.setCaretPosition(e);
               System.out.println("moving fro "+i+" to "+e);
           }catch(javax.swing.text.BadLocationException bel){
               JOptionPane.showMessageDialog(wordprocessorpanel, "You must
specify a location in the document to insesrt this field.",
               "File Insert",
               JOptionPane.OK_OPTION);
           }
       }
   }
ak - 22 Feb 2004 21:10 GMT
> I have a JDialog with a JList on it wherin if something is selected in the
> JList, and a JButton ("Add") is clicked, it inserts the String selected in
[quoted text clipped - 35 lines]
> wordprocessorpanel.m_monitor.getStyledDocument().insertString(i,s,null);
>                 int e = i+s.length();

wordprocessorpanel.m_monitor.moveCaretPosition(e);//creates
> a selection from i to e
>
[quoted text clipped - 9 lines]
>         }
>     }

your first big mistake is that you create new Thread - no need for this,
throw "Thread t" away.
second mistake is that you 2 times uses moveCaretPosition() - pointless
since you don't want to select text.

so after some clean up your code should be:

class MoveCursor implements  Runnable{
        WordProcessorPanel wordprocessorpanel;
//wordprocessorpanel.m_monitor is the JTextPane
        int i;
        String s;

        MoveCursor(WordProcessorPanel wordprocessorpanel, int i, String s){
            this.wordprocessorpanel=wordprocessorpanel;
            this.i=i;
            this.s=s;
        }
        public void run(){
            try{
               wordprocessorpanel.m_monitor.grabFocus();

wordprocessorpanel.m_monitor.getStyledDocument().insertString(i,s,null);
               int e = i+s.length();
                wordprocessorpanel.m_monitor.setCaretPosition(e);
           }catch(javax.swing.text.BadLocationException bel){
                JOptionPane.showMessageDialog(wordprocessorpanel, "You must
specify a location in the document to insesrt this field.",
                "File Insert",
                JOptionPane.OK_OPTION);
            }
        }
    }

I am not sure also if all this really needed - because MoveCursor seems to
be called from EventDispatchThread - so you could insert text and set caret
position without SwingUtilities.invokeLater()

____________

http://reader.imagero.com the best java image reader.
Ike - 22 Feb 2004 21:20 GMT
> > I have a JDialog with a JList on it wherin if something is selected in the
> > JList, and a JButton ("Add") is clicked, it inserts the String selected in
> > the JList into a JTextPane residing in a componenent which invoked my
> > JDialog. All works fine. However, after inserting the text into the
> > JTextPane, I am trying to advance the cursor to right after the inserted
> > text. For some reason, I am not able to do this using the following code.

> your first big mistake is that you create new Thread - no need for this,
> throw "Thread t" away.
[quoted text clipped - 10 lines]
>
>          MoveCursor(WordProcessorPanel wordprocessorpanel, int i, String
s){
>              this.wordprocessorpanel=wordprocessorpanel;
>              this.i=i;
[quoted text clipped - 9 lines]
>             }catch(javax.swing.text.BadLocationException bel){
>                  JOptionPane.showMessageDialog(wordprocessorpanel, "You
must
> specify a location in the document to insesrt this field.",
>                  "File Insert",
[quoted text clipped - 6 lines]
> be called from EventDispatchThread - so you could insert text and set caret
> position without SwingUtilities.invokeLater()

But it wont do it - which is why I moved it into a separate thread!
ak - 22 Feb 2004 22:24 GMT
> > I am not sure also if all this really needed - because MoveCursor seems to
> > be called from EventDispatchThread - so you could insert text and set
> caret
> > position without SwingUtilities.invokeLater()
>
> But it wont do it - which is why I moved it into a separate thread!

first - you didn't moved it in separate thread -
SwingUtilities.invokeLater() runs your code in EventDispatchThread -
this is the same thread with code from ActionListener (called after button
press).

second - you asked where you "screwing this up" - I just answered, but you
can do it as you like.

--
____________

http://reader.imagero.com the best java image reader.
Ike - 22 Feb 2004 22:34 GMT
Yes, the problem I am having, i.e. moving the caret, is independent of the
other problem(s) you pointed out. Once corrected, I still cannot move the
caret. -Ike

> > > I am not sure also if all this really needed - because MoveCursor seems
> to
[quoted text clipped - 16 lines]
>
> http://reader.imagero.com the best java image reader.
ak - 23 Feb 2004 16:13 GMT
> I have a JDialog with a JList on it wherin if something is selected in the
> JList, and a JButton ("Add") is clicked, it inserts the String selected in
[quoted text clipped - 5 lines]
> This shouldn't be so hard, should it, lol? Does anyone see where I am
> screwing this up? -Ike

your problem does not exist - try this example

import javax.swing.*;
import javax.swing.text.BadLocationException;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TextPaneTest {

   public static void main(String[] args) {
       final JTextPane tp = new JTextPane();
       JFrame frame = new JFrame();
       JButton insert = new JButton("insert");
       insert.setFocusable(false);

       insert.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               int start = tp.getCaretPosition();
               try {
                   tp.getDocument().insertString(start, "insertString ",
null);
               } catch (BadLocationException e1) {
                   e1.printStackTrace();
               }
           }
       });

       frame.getContentPane().add(tp);
       frame.getContentPane().add(insert, BorderLayout.SOUTH);
       frame.pack();
       frame.show();
   }
}

--

____________

http://reader.imagero.com the best java image reader.
Ike - 24 Feb 2004 14:37 GMT
At first I thought it may be because I am using a custom L&F....removed
it...same problem. I've now via your good help and lots of elimination,
discovered it is something wrong with using RTF Editor. Thanks for your help
with this nasty thing. -Ike

> > I have a JDialog with a JList on it wherin if something is selected in the
> > JList, and a JButton ("Add") is clicked, it inserts the String selected in
[quoted text clipped - 46 lines]
>
> http://reader.imagero.com the best java image reader.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.