1

I seem to have run into a snag replacing text in a JTextPane. I have a couple of JTextPanes that I need to change the exiting text. I can do this with initial text but when I call to change the document, I see this change in the call but the panel will not update.

switch(module.getCurrentQuestionNumber())
{
    case 1:
        doc = module8.loadQuestion1();
        udoc = module8.loadQuestion1();

        codeTextPane.setDocument(doc);
        uCodeTextPane.setDocument(udoc);

        toolPane.add(module.loadQuestion1Panel(outputTextPane));
        uToolPane.add(module.loadQuestion1Panel(uOutputTextPane));
        break;

    case 2:
        doc = module8.loadQuestion2();
        udoc = module8.loadQuestion2();

        codeTextPane.setDocument(doc);
        uCodeTextPane.setDocument(udoc);

        toolPane.add(module.loadQuestion2Panel(outputTextPane));
        uToolPane.add(module.loadQuestion2Panel(uOutputTextPane));
        break;
}

When module.getCurrentQuestionNumber() returns 1 the panels load and everything works as intended. When module.getCurrentQuestionNumber() returns 2, both doc & udoc has the correct information in it (I stepped through the program and when I get to the call in case 2 the docs show the updated question), but this will not update the codeTextPane, nor will the toolPane load the new panels. I tried to call repaint() and update() and neither seems to work. Am I missing something?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
jbolt
  • 688
  • 3
  • 16
  • 37
  • Make sure you're not doing this from a listener. For example, if you've set up a `Listener` that executes when you press a button, but you run the above code while you're still in the listener, it might not work. I'm not 100% sure I'm right about this, though. – ajb Nov 20 '13 at 21:58
  • I'm not calling this from within a listener. I call a method in my main gui from the listener. Essentially I wanted to be able to build stand alone modules in separate classes and then have the main gui display them when called. I thought this would be the best approach. I create styled documents in the module, return them to the gui and have the gui display them in a jtextpane. I have the jtextpane nested inside a jscrollpane that is nested inside of a jsplitpane. I believe that is the proper way to set this up but once I write to the textpane I can not get it to update a with a new doc. – jbolt Nov 21 '13 at 05:02

1 Answers1

1

I tried to call repaint() and update() and neither seems to work. Am I missing something?

When adding (or removing) a component from a visible GUI the general form is:

panel.add(...);
panel.revalidate();
panel.repaint();

By default components have a zero size, so you need the revalidate() to invoke the layout manager so the component is assigned a size.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • That doesn't seem to work either. I tried to create a totally new document (doc9) instead of reassigning doc8 with a new styled document returned from the module, but that also doesn't work. The new data exists at the point where I need to assign it to the jtextpane but just doesn't update with the command `codeTextPane.setDocument(doc8);` – jbolt Nov 21 '13 at 05:11
  • I also tried to remove the existing document first with `doc.remove(0, doc.getLength());` call to remove the existing text but that doesn't work either which seems strange like I'm missing a key part of the puzzle. The call to create the pane is 'public JTextPane codeTextPane;` and is created with `codeTextPane = new JTextPane();`. Looking at other tutorials looks like it should work. – jbolt Nov 21 '13 at 05:24
  • @jbolt, The easiest way is to just create the JTextPane at design time. Then you u Then as long as you have a reference to the text pane you can access and update the Document as needed. Dynamically replacing a component is not a good idea. – camickr Nov 21 '13 at 06:06
  • Agreed. But you should be able to change the underlying document out at will. – jbolt Nov 22 '13 at 03:20
  • @jbolt, That is what I just said. Changing/updating the Document is easier than creating a new JTextPane. The problem is with your code, not Swing. Since you haven't posted a `SSCCE`, there is no way we can offer specific help. – camickr Nov 22 '13 at 04:06