简体   繁体   中英

JScrollPane won't 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.

As @camickr suggested, run the content of your actionPerformed() in a SwingWorker. Its contents should contain something like this:

    Runnable runner = new Runnable() {
        @Override
        public void run() {
           textPane.appendANSI(line+"\n");
           textpane.setCaretPosition(textpane.getDocument().getLength());
        }
    }
    while ((line = in.readLine()) != null) {
        SwingUtilities.invokelater( runner );
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM