2

I've posted a question similar to this before, but anyway, I guess this explains my problem better. Please refer to this link. If you notice, all examples suggest that your textpane content has to prepared before it is added to the content pane! Why is this so?

For instance, this piece of code:

    public class PaneInsertionMethods {
  public static void main(String[] args) {
    final JTextPane pane = new JTextPane();
    pane.replaceSelection("text");
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(pane, BorderLayout.CENTER);
    frame.setSize(360, 180);
    frame.setVisible(true);
  }
}

works well. But, if I try to do something like this:

public class PaneTest extends JFrame {
private JTextPane pane;
public PaneTest() {
   initComponents();
}
private void initComponents() {
    pane = new JTextPane();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(pane, BorderLayout.CENTER);
    setSize(360, 180);
    setVisible(true);
}
public void populatePane() {
    pane.replaceSelection("text");  
   //or something like this.. doesn't work
   //pane.revalidate(); pane.repaint();

}
public static void main(String args[]) {
    PaneTest test = new PaneTest();
    test.populatePane();
    //or even something like this doesn't work:
    //SwingUtilities.invokeLater(new Runnable() {
    // public void run() {
    //     test.populatePane();
    //   }});
}}

All I get to see is a empty textpane in the second example. What am I doing wrong?

Jay
  • 2,394
  • 11
  • 54
  • 98
  • Well first of all, there is not really ever a good reason to subclass JFrame. replaceSelection is meant to replace text selected by the user. Are you sure you are not looking for the setText (http://docs.oracle.com/javase/7/docs/api/javax/swing/JEditorPane.html#setText%28java.lang.String%29) method? – thatidiotguy Feb 02 '12 at 14:43
  • in my actual code a JDialog is being subclassed. The idea is to update caret position to existing text position - something like this: textpane.setCaretPosition(textPane.getDocument().getLength()) and then call textpane.replaceSelection() which actually does an insert. – Jay Feb 02 '12 at 14:47
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Feb 02 '12 at 15:57

1 Answers1

1

I just figured that replaceSelection doesn't insert text when the textpane is not editable (in my case it was not editable!) - I can't believe I read the javadoc on this like 50 times, but I kept missing that one line. Anyway, problem solved now!

Jay
  • 2,394
  • 11
  • 54
  • 98
  • ok.. it works when I do this: test.populatePane(); test.setVisible(true); and not when test.setVisible(true);test.populatePane(); I even tried calling test.populatePane() from SwingUtilities.InvokeLater() - doesn't work. – Jay Feb 02 '12 at 15:13
  • looks like I have to always populate pane first and then display - why is this so? – Jay Feb 02 '12 at 15:14
  • found my answer again - the JDialog cannot be modal - if it is modal, you cannot modify a textpane inside it after you make the dialog visible!! – Jay Feb 02 '12 at 15:28
  • yes you can, but your code isn't called before the dialog is closed. – stryba Feb 02 '12 at 16:04