Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5939931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:53:50+00:00 2026-05-22T15:53:50+00:00

An application I am writing consists, among others, a JButton and a JTextArea. A

  • 0

An application I am writing consists, among others, a JButton and a JTextArea. A click on the button leads to a long calculation, resulting in a text shown in the JTextArea. Even though the calculation is long, I can have middle-results on the go (think, for example, of an application which approximates pi up to 100 digits – every few seconds I could write another digit). The problem is, that even if I write (being in the ActionListener class because the button invoked the calculation) to set the text of the JTextArea to something, it isn’t shown while the calculation is done, and I can only see the end result, after the calculation is over.

Why is it so, and how can I fix it?

Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:53:51+00:00Added an answer on May 22, 2026 at 3:53 pm

    Your problem is that you’re doing a long calculation in the main Swing thread, the EDT, and this will freeze your entire GUI until the process has completed itself. A solution is to use a background thread for your calculation, and an easy way to do this it to use a SwingWorker to create a thread background to the main Swing thread, the EDT, and publish/process the interim results into the JTextArea. For more on SwingWorkers and the EDT, please look here: Concurrency in Swing

    Also, if you provide a decent sscce we can probably give you a more detailed response perhaps even with sample code.

    An example SSCCE:

    import java.awt.event.*;
    import java.text.DecimalFormat;
    import java.util.List;
    import javax.swing.*;
    
    public class InterimCalc {
       private JPanel mainPanel = new JPanel();
       private JTextField resultField = new JTextField(10);
       private JButton doItBtn = new JButton("Do It!");
       private DecimalFormat dblFormat = new DecimalFormat("0.0000000000");
       private SwingWorker<Void, Double> mySwingWorker = null;
    
       public InterimCalc() {
          mainPanel.add(doItBtn);
          mainPanel.add(resultField);
          displayResult(0.0);
    
          doItBtn.addActionListener(new DoItListener());
       }
    
       public void displayResult(double result) {
          resultField.setText(dblFormat.format(result));
       }
    
       public JPanel getMainPanel() {
          return mainPanel;
       }
    
       private class DoItListener implements ActionListener {
    
          public void actionPerformed(ActionEvent e) {
             if (mySwingWorker != null && !mySwingWorker.isDone()) {
                mySwingWorker.cancel(true);
             }
             displayResult(0.0);
             mySwingWorker = new MySwingWorker();
             mySwingWorker.execute();
          }
       }
    
       private class MySwingWorker extends SwingWorker<Void, Double> {
    
          private static final int INTERIM_LENGTH = 10000; // how many loops to do before displaying
    
          @Override
          protected Void doInBackground() throws Exception {
             boolean keepGoing = true;
             long index = 1L;
             double value = 0.0;
             while (keepGoing) {
                for (int i = 0; i < INTERIM_LENGTH; i++) {
                   int multiplier = (index % 2 == 0) ? -1 : 1;
                   value += (double)multiplier / (index);
                   index++;
                }
                publish(value);
             }
             return null;
          }
    
          @Override
          protected void process(List<Double> chunks) {
             for (Double dbl : chunks) {
                displayResult(dbl);
             }
          }
    
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("Decay Const");
          frame.getContentPane().add(new InterimCalc().getMainPanel());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a test application which consists of: 2 buttons 1 Edittext 1
I'm writing test code to test a client-server application. The application under test consists
I'm writing application which consists of server side on Google App Engine (Java) and
I'm writing an application that consists of several maven modules. All of them have
I'm writing an application in C# that basically consists of two threads. Thread 1
I am writing a simple Python web application that consists of several pages of
I am writing an application that consists of business logic and UI parts. There
I am writing an application which consists of a master server database (most probably
I am writing a console application in python that will consist of a handful
Im writing an application that supposed to send coordinates in an SMS, but I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.