简体   繁体   中英

Unable to scroll on scrollpane - Java Swing

I have a Swing application that is updating the contents of a Textarea with strings as soon as they are passed to the GUI.

I have the strings updating in the textarea in real time but the problem im now facing is I am unable to scroll the the textarea while the process is still running.

Anyone know whats going on here or have a solution

Attempt 1 -

public static void appendToJTextArea(String message){
    String currentText = GUI.textLogArea.getText();
    String newString = message;
    String newTextToAppend = currentText + "\n" + newString;
    GUI.textLogArea.setText(newTextToAppend);
    GUI.frame.update(GUI.frame.getGraphics());
}

Attempt 2 -

    public static void appendToJTextArea(String message){
        Runnable runnable = new LogThread(message);
        Thread thread = new Thread(runnable);
        thread.start();

    /**
        String currentText = GUI.textLogArea.getText();
        String newString = message;
        String newTextToAppend = currentText + "\n" + newString;
        GUI.textLogArea.setText(newTextToAppend);
        GUI.frame.update(GUI.frame.getGraphics());
        **/
    }
}

    class LogThread implements Runnable {
        private String test;

        public LogThread(String message){
            test = message;
        }
        public void run() {
            update(test);
        }

    private void update(String message) {
        System.out.println("BBB"+GUI.textLogArea.getText());
        System.out.println("AAA"+message);

        String currentText = GUI.textLogArea.getText();
        String newString = message;
        String newTextToAppend = currentText + "\n" + newString;
        GUI.textLogArea.append(newTextToAppend);
    }

GUI class with scrollpane

JPanel panel_1 = new JPanel();
panel_1.setBounds(361, 40, 390, 305);
contentPane.add(panel_1);
panel_1.setLayout(null);

JLabel lblScraperLogs = new JLabel("Scraper Logs");
lblScraperLogs.setBounds(0, 0, 390, 31);
lblScraperLogs.setFont(new Font("Cooper Black", Font.PLAIN, 13));
lblScraperLogs.setHorizontalAlignment(SwingConstants.CENTER);
panel_1.add(lblScraperLogs);        

textLogArea = new JTextArea();
textLogArea.setEditable(false);
textLogArea.setBounds(10, 23, 380, 282);

JScrollPane scroll = new JScrollPane(textLogArea);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setBounds(10, 23, 380, 282);
panel_1.add(scroll);

but the problem im now facing is I am unable to scroll the the textarea while the process is still running

Sounds like your long running process is executing on the Event Dispatch Thread (EDT) . This Thread is responsible for painting the GUI and therefore the GUI can't repaint itself until the process is finished executing. Don't run your code on the EDT.

Instead to need to create a separate Thread for the long running process. Maybe use a SwingWorker which creates a separate Thread and allows you to "publish" data as it becomes available. Read the section from the Swing tutorial on Concurrency for more information about the EDT and a SwingWorker.

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