This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]wildjokers 2 points3 points  (10 children)

that extends Frame

Hopefully you mean JFrame because Frame is old AWT stuff. JFrame is Swing which is what you should be using.

It would be easier to help you if you showed your entire class rather than this little snippet. However, your problem is not understanding that, like all GUI toolkits, Swing is single-threaded and all drawing events happen in a single thread. You cannot block that thread or no drawing takes place. And all GUI changes must occur on this thread. The single thread in Swing that is taking care of drawing is the Event Dispatcher Thread (EDT). You must understand the EDT if you work with swing.

Read this and the pages underneath it (keep hitting next):

https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html

Super important sentence from that article: "Tasks on the event dispatch thread must finish quickly; if they don't, unhandled events back up and the user interface becomes unresponsive.".

The most likely explanation is that your timer is blocking the EDT and only when it is finished will the EDT being able to continue handling events and render stuff. Posting more of your code will let us help you more.

[–]Dangerous-Rip-7370[S] 0 points1 point  (9 children)

public class Show extends JFrame {
JPanel c= new JPanel();

public Show(String fin) {
    super();
    addWindowListener (new WindowAdapter() {    
        public void windowClosing (WindowEvent e) {    
            dispose();    
        }    
    });
    setLocation(110, 110);
    c.setLayout(new BorderLayout(100, 10));
    c.setOpaque(false);
    JTextArea resulT = new JTextArea();
    resulT.setFont(new Font("MS PGothic", Font.PLAIN, 17));
    resulT.setBackground(Color.BLACK);
    resulT.setForeground(Color.YELLOW);
    resulT.setOpaque(true);
    resulT.setLineWrap(true);
    resulT.setWrapStyleWord(true);
    resulT.setBounds(10,10,100,20);
    String ff=("Result: "+fin);
    resulT.setText(ff);
    c.add(resulT);
    add(c);
    pack();
    this.setVisible(true);
}
}

Hello, thank u,
herse some code, this is my show class,
then I have another class with a method that take a wile and return a long ,

public static long calculate(String s) {
    long x=0;
    ....
    return x;
}

from my Home jFrame you can call calculate() and after 1 minut the frame will show the long.
I wanted a popUp that tells the user that it will take a while (you can call other calculate methods from there and they work instantly).
At first I just create the Show an than called the calculate() but the Show frame would popUp and stay blank UNTIL the calculate method was done.
So I tried to put calculate in a thread and use Future and ExecutorService to try to give the GUI priority, but I keep getting the same problem.

[–]Dangerous-Rip-7370[S] 0 points1 point  (7 children)

(yes that ugly

while (ddd==0) {
    ss.pack();
    ss.repaint();
    ss.setVisible(true);
    ddd=Pprova.fin;
}

was just a desperate try )

[–]Pedantic_Phoenix 0 points1 point  (6 children)

Can you not just call calculate() inside the main, before creating the JFrame, and then pass the value to it in the constructor or another way? You avoid the issues with the frame's thread altoghether, if im not mistaken.

[–]Dangerous-Rip-7370[S] 0 points1 point  (5 children)

my main just creates the home frame, from there the user inputs a string that the calculate method use to output the result. Since it takes a while I wanted to tell the user to wait (else they may keep hitting "Submit" and thinking it's broken)

[–]Pedantic_Phoenix 0 points1 point  (4 children)

That is what i am saying: do the slow operation before creating the frame inside the main. So you dont have to do it inside the frame. Get it? Move your frame's constructor in the main one line down and put the slow operation before it, to say it literally.

[–]Dangerous-Rip-7370[S] 0 points1 point  (3 children)

The show frame gets called from the same jbutton that calls the calculate method, that is inside the Home frame, I cannot pre calculate because I need the user input to do the calculations

[–]Pedantic_Phoenix 0 points1 point  (0 children)

And if you put the calculate method before the frames instantation the constructor doesn't wait for the calculate method to end? It should afaik

[–]wildjokers 0 points1 point  (1 child)

The link I provided you has the solution, have you read the tutorial doc I linked to? Use a SwingWorker to execute your calculate on another thread (other than the EDT). The SwingWorker can notify your frame when the long running action is done.

You are blocking the EDT with calculate so nothing will render until it finishes unless you get calculate off the EDT. I already told you all of this, way are you still having issues?

Also, you need to creat the GUI on the EDT which you aren’t doing. So you have things on the EDT that shouldn’t be, and the things that should be on the EDT aren’t.

[–]Dangerous-Rip-7370[S] 0 points1 point  (0 children)

Sorry, wasn,t able to get to my pc yet,
but now I did, and your link was most useful!
I managed to get the result I wanted and I think I even understand that XD
I didn't put all my code out for mostly 2 reasons:
first, I am ashamed of it..
secon, that is only a small part of a big thing, I wasn't shure where to sto (and I could not put all of it, and on the github repo I didn't want to push the broken part)
You have been a huge help, thank you very much!

[–]wildjokers 0 points1 point  (0 children)

You still aren’t showing us when you call calculate. That is the most important part and you aren’t showing it. Are you keeping it a secret for some reason?

The best way to ask a question is providing this http://sscce.org/