setCaretPosition from an invoked dialog

I

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
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.

The cursor does not advance to the appropriate postion and am hoping someone
may have some insight here -Ike

class MyDialog extends JDialog {
.....
.....
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
//called when "add" button hit
if(ae.getActionCommand().equals("add")){
//wordprocessorpanel.m_monitor is the JTextPane
int i = wordprocessorpanel.m_monitor.getCaretPosition();
String s = (String)jList1.getSelectedValue();
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);
}
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top