JScrollPane does not scroll down to properly display JTextPane called from ActionListener

I want to refresh my ColorPane defined as follow:

public class ColorPane extends JTextPane{
    /* FYI, The function below allows me to add text with ANSI Coloring */
    public void appendANSI(String s) {
        // ...
    }

    // ...
}

In this next while
statement, I write line
in a console-style window. So as a console, I want to refresh and scroll down automatically
at bottom when a new line is added to textPane
.

I want to launch the following loop by pressing a mainButton
.

public class mainButton implements ActionListener  {
    public void actionPerformed (ActionEvent e) {
        ColorPane textPane = new ColorPane();

        /* FYI, in is a BufferedReader */
        while ((line = in.readLine()) != null) {

            /* Add automatically in the console window */
            textPane.appendANSI(line+"n");

            /* Last possible workaround I tried
               Which as the same effect than commenting these lines.. */
            JScrollBar vertical = scrollPane.getVerticalScrollBar();
            vertical.setValue(vertical.getMaximum());

            /* I also tried : */
            // textPane.setCaretPosition(textPane.getDocument().getLength());

            /* Or this : */
            // Rectangle rec = GenerationWindow.textPane.getVisibleRect();
            // rec.setLocation((int) (rec.getX() + 1000), (int) rec.getY());
            // textPane.scrollRectToVisible(rec);

            /* Refresh the display */
            textPane.update(textPane.getGraphics());
        }
    }
}

The thing is, my window is scrolled down, but
only when exiting actionPerformed

. Why ?

In one hand I can see that the text is updated at each loop turn, but in the other hand, the JScrollPane scrollPane
is scrolled down on end of the function…

I think I miss some swing philosophy here..

I’ve been roaming for hours on Oracle Documentation and StackO topics, trying different “solutions”, but I’m a bit desperate now..

A console not automatically scrolling down is.. well.. not a console..

textPane.update(textPane.getGraphics());

Don’t manually invoke update(…) on a components. Swing will determine when the component needs to be repainted.

The thing is, my window is scrolled down, but only when exiting actionPerformed. Why ?

You code is executing in a loop which is executing on the Event Dispatch Thread which is the Thread that paints the GUI. The GUI can’t repaint itself until the loop is finished.

The long running code needs to execute on a separate Thread and then when you update the text pane the GUI is free to repaint itself.

I would probably use a Swing Worker
for this thread then you can “publish” the results as they become available. Read the section from the Swing tutorial on Concurrency
for more information and examples of using a SwingWorker.

Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » JScrollPane does not scroll down to properly display JTextPane called from ActionListener

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录