#JTextArea laggy

45 messages · Page 1 of 1 (latest)

lilac barn
#

My JTextArea is becoming very laggy with large prime sequences. It is beeing updated every time the user hits BURN... any tips to improve it? I was also thinking in invoking (if possible) the windows CMD if the prime sequence is big...

Button BURN:

    private void buttonBurnActionPerformed(java.awt.event.ActionEvent evt) {
        outputAreaS.setText("");
        outputAreaS.setText("Calculando....\n");
        String inputString = inputNumbers.getText();
        inputString = inputString.replace(".", "");
        int number;
        try {
            number = Integer.parseInt(inputString);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Inválido!");
            return;
        }
        sieve.calculate(number);
        StringBuilder sb = new StringBuilder();
        sb.append(sieve.getList());
        outputAreaS.setText(sb.toString());
    }
mighty estuaryBOT
#

This post has been reserved for your question.

Hey @lilac barn! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

stiff trout
#

Hi, how do you calculate it? Also from what you have posted I could assume that the computation is done in AWT thread. That is not good, because for large numbers it will take some time to compute all values and GUI will freeze.

patent crow
#

You should do it in EDT

lilac barn
#

I'm using the classic Sieve of Eratostenes...

Now it's working fine, i guesss... I'm using a different thread

patent crow
#

SwingUtilities.invokeLater

#

that would help you stop using the main thread for the whole output

lilac barn
#
    private void buttonBurnActionPerformed(java.awt.event.ActionEvent evt) {
        outputAreaS.setText("");
        outputAreaS.setText("Calculando....\n");
        // Start the calculation in a separate thread
        SwingWorker<String, Void> worker = new SwingWorker<String, Void>() {
            @Override
            protected String doInBackground() throws Exception {
                String inputString = inputNumbers.getText();
                inputString = inputString.replace(".", "");
                int number;
                try {
                    number = Integer.parseInt(inputString);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Inválido!");
                    return "";
                }
                sieve.calculate(number);
                return sieve.getList();
            }
    
            @Override
            protected void done() {
                try {
                    // Update the JTextArea with the result
                    String result = get();
                    outputAreaS.setText("Calculando....\n" + result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        worker.execute();
    }
lilac barn
patent crow
#

...done by who?

lilac barn
patent crow
#

Yikes

lilac barn
#

Didnt know how to make this

patent crow
#

My suggestion could've worked too, although SwingWorker isn't far off from being a choice.

#

I would've probably used SwingWorker as a first too

#

SwingUtilities would be my second choice if SwingWorker doesn't suffice to my needs

lilac barn
#

I see

patent crow
#

SwingUtilities has one weakness and it doesn't allow you to run something in background

#

It will freeze any GUI you have while something is done inside it (e.g. a HTTP request)

lilac barn
#

Still studying GUI's and JavaFX, this is new to me

patent crow
#

That's Swing, not JavaFX

lilac barn
#

Oh ok

#

lol

patent crow
#

Don't rely on bing for things you don't know.

lilac barn
#

I never do, but this time I did

#

AI makes programmers dumber

stiff trout
#

And do you understand what is going on in that code?

lilac barn
patent crow
#

If used properly

lilac barn
#

Beginners, like me, shouldn't use this

#

i had no choice

patent crow
#

If I make myself an in-house library, I usually use a mixture of copilot and CGPT to write Javadocs for it. I don't want it to sound like It was written by a 12-year-old, so CGPT being the MVP of the English language compared to me can get me around

stiff trout
lilac barn
#

Could also be helpful for writing documentation

#

My english is not that good, i'm not even american. It would help me a lot

mighty estuaryBOT
# lilac barn Thank you!

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

lilac barn
#

I'll close the thread

#

Thank you guys

#

Bye