Update Jtexpane (display scrollbar) when inserting information

Asked

Viewed 70 times

0

I have a JTextPane where I enter some values. Components are added directly in the Netbeans IDE, not created on run, instantiating components. What I’d like to achieve is: As the values are added, the scrollbar is displayed, so you can see what my "log" would be. I’ve made a few attempts, I’m working on these two next.

Attempt 1: insert the data into JTextPane but does not update (barra de rolagem is not displayed as data is entered)

   public void updateAppend(String toApeend, Document document, Style defaultStyle, JTextPane jTextPane1) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                document.insertString(document.getLength(),"\n"+ toApeend, defaultStyle);
                 jTextPane1.update(jTextPane1.getGraphics());
            } catch (BadLocationException ex) {
                Logger.getLogger(TesteFat.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
}

Attempt 2: Exception occurs, apparently when trying to update the component:

public void update(String toApend, Document document, Style defaultStyle, JTextPane jTextPane1) {
        Runnable updateGUI = new Runnable() {
            @Override
            public void run() {
                try {
                    document.insertString(document.getLength(),"\n"+ toApend, defaultStyle);
                   jTextPane1.update(jTextPane1.getGraphics());
                } catch (BadLocationException ex) {
                    Logger.getLogger(TesteFat.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        Thread t = new Thread(updateGUI);
        t.start();
    }

Exception:

Exception in thread "Thread-2" java.lang.NullPointerException
    at javax.swing.text.FlowView$FlowStrategy.layoutRow(FlowView.java:563)
    at javax.swing.text.FlowView$FlowStrategy.layout(FlowView.java:477)
    at javax.swing.text.FlowView.layout(FlowView.java:201)
    at javax.swing.text.BoxView.setSize(BoxView.java:397)
    at javax.swing.text.BoxView.updateChildSizes(BoxView.java:366)
    at javax.swing.text.BoxView.setSpanOnAxis(BoxView.java:348)
    at javax.swing.text.BoxView.layout(BoxView.java:708)
    at javax.swing.text.BoxView.setSize(BoxView.java:397)
    at javax.swing.plaf.basic.BasicTextUI$RootView.setSize(BasicTextUI.java:1722)
    at javax.swing.plaf.basic.BasicTextUI$RootView.paint(BasicTextUI.java:1433)
    at javax.swing.plaf.basic.BasicTextUI.paintSafely(BasicTextUI.java:737)
    at javax.swing.plaf.basic.BasicTextUI.paint(BasicTextUI.java:881)
    at javax.swing.plaf.synth.SynthEditorPaneUI.paint(SynthEditorPaneUI.java:179)
    at javax.swing.plaf.synth.SynthEditorPaneUI.update(SynthEditorPaneUI.java:167)
    at javax.swing.JComponent.paintComponent(JComponent.java:780)
    at javax.swing.JComponent.paint(JComponent.java:1056)
    at javax.swing.JComponent.update(JComponent.java:965)
    at view.TesteFat$10.run(TesteFat.java:1980)
    at java.lang.Thread.run(Thread.java:748)
Exception in thread "Thread-44" Exception in thread "Thread-23" java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at javax.swing.text.BoxView.updateLayoutArray(BoxView.java:213)
    at javax.swing.text.BoxView.replace(BoxView.java:186)
    at javax.swing.text.View.append(View.java:417)
    at javax.swing.text.FlowView$FlowStrategy.layout(FlowView.java:473)
    at javax.swing.text.FlowView.layout(FlowView.java:201)
    at javax.swing.text.BoxView.setSize(BoxView.java:397)
    at javax.swing.text.BoxView.updateChildSizes(BoxView.java:366)
    at javax.swing.text.BoxView.setSpanOnAxis(BoxView.java:348)
    at javax.swing.text.BoxView.layout(BoxView.java:708)
    at javax.swing.text.BoxView.setSize(BoxView.java:397)
    at javax.swing.plaf.basic.BasicTextUI$RootView.setSize(BasicTextUI.java:1722)
    at javax.swing.plaf.basic.BasicTextUI$RootView.paint(BasicTextUI.java:1433)
    at javax.swing.plaf.basic.BasicTextUI.paintSafely(BasicTextUI.java:737)
    at javax.swing.plaf.basic.BasicTextUI.paint(BasicTextUI.java:881)
    at javax.swing.plaf.synth.SynthEditorPaneUI.paint(SynthEditorPaneUI.java:179)
    at javax.swing.plaf.synth.SynthEditorPaneUI.update(SynthEditorPaneUI.java:167)
    at javax.swing.JComponent.paintComponent(JComponent.java:780)
    at javax.swing.JComponent.paint(JComponent.java:1056)
    at javax.swing.JComponent.update(JComponent.java:965)
    at view.TesteFat$10.run(TesteFat.java:1980)
    at java.lang.Thread.run(Thread.java:748)

I also tried to separate the part that makes the update from the JTextPane but it also didn’t work properly. Call revalidate(),repaint() also.

public static void threadUpdate(JTextPane jTextPane1) throws InterruptedException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                jTextPane1.update(jTextPane1.getGraphics());
//                jTextPane1.setCaretPosition(jTextPane1.getDocument().getLength());
            }
        }).start();
         Thread.sleep(2000);
    }
  • 1

    Can provide a [mcve] so that it is possible to test?

  • So I got what I wanted by putting all my code inside one Runnable. So as values are added the components are updated immediately. There snippets like this jTextPane1.update(jTextPane1.getGraphics()); are unnecessary. Thank you

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.