Jay
Jay

Reputation: 2454

JTextPane : How do you update it after adding it to a layout?

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?

Upvotes: 2

Views: 1726

Answers (1)

Jay
Jay

Reputation: 2454

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!

Upvotes: 1

Related Questions