r/javahelp • • Apr 25 '26

Homework Update swing GUI during runtime

Part of my coursework needs me to make a typeracer between bots. I need to visually show their progress along the passage of text. Problem is, it won't update. It just freezes for ages then finally prints everything at once. Validating the panel, scrollpane or frame after each print doesn't change it. Here's the relevant code:

private void printRace(JPanel p)

{

    System.out.print('\\u000C'); // Clear terminal

    String raceRound = "  TYPING RACE - passage length: " + passageLength + " chars \\n" 

    \+ multiplePrint('=', passageLength + 3) + "\\n ";

    raceRound = raceRound + "\\n" + printSeat(seat1Typist, p);

    raceRound = raceRound + "\\n" + printSeat(seat2Typist, p);

    raceRound = raceRound + "\\n" + printSeat(seat3Typist, p);

    raceRound = raceRound + "\\n" + multiplePrint('=', passageLength + 3) + "\\n\[\~\] = burnt out    \[<\] = just mistyped\\n ";

    JTextArea text = new JTextArea(raceRound);

    text.setEditable(false);

    text.setSize(text.getPreferredSize());

    p.add(text);

    }

    //Some other irrelevant code at this point 

    printRace(p);

    p.revalidate();

    p.repaint();

    f.revalidate();

    f.repaint();

Apologies for any formatting issues, I think it should look okay

4 Upvotes

18 comments sorted by

View all comments

Show parent comment

2

u/NonexistantObject Apr 26 '26

I'll look into them if I have time. It does get me points to turn it into a GUI based application, but it's only 7% of my overall grade at most. We were given a skeleton of the code that we had to fix and finish to make work in console, then convert to GUI based, so I assumed the while loop would be okay in Swing. It's honestly a ridiculous requirement, we were barely taught anything regarding Swing. I might just leave that bit and focus on the git implementation and other projects