0

I have a JPanel and a element called consoleOutput. I am taking the JPanel in as a parameter in a method in a action listener that allows me to print to the console from another class. However whenever I try to print to the consoleOutput it wont print until the end of the method in my other class. Here is what my code looks like: My MainPanel Class:

public class MainPanel extends JPanel{    

private JButton submitButton;
JTextArea consoleOutput;

public MainPanel(){

Border border = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
setLayout(null);
setBackground(Color.WHITE);
Font f1 = new Font("Arial", Font.PLAIN, 14);

submitButton = new JButton("Get Cards");
submitButton.setBounds(35, 285, 107, 49);
submitButton.setFont(f1);

consoleOutput = new JTextArea();
consoleOutput.setBounds(199, 122, 375 , 210);
consoleOutput.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(3, 4, 0, 0)));
consoleOutput.setEditable(false);
consoleOutput.setFont(f1);

submitButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        String username = "Hank";
        String password = "passfunnylolxD";
        Cards cards = new Cards();
        cards.openTabs(username, password, MainPanel.this); 
        }
    });
    add(submitButton);
    add(consoleOutput);
    }   
}

My Cards Class:

public class Cards{

    private void printToConsole(String text, MainPanel panel){
        panel.consoleOutput.append(text);   
        panel.consoleOutput.updateUI();
    }  
    public class Cards{

    public void openTabs(String username, String password, MainPanel panel){
        printToConsole(username, panel); //This will be printed out 5 seconds after it is printed to the actual console
        try {
            TimeUnit.MILLISECONDS.sleep(5000);
        } 
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(username); //This will be printed out 5 seconds before it is printed to the textArea console

    }     
}    

I'm not sure why this does this. Why does it not update. What should I be doing to update it every time I want to print something? Thanks, all help is appreciated!!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Henry Jeff
  • 57
  • 8
  • Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. – Andrew Thompson Oct 12 '15 at 21:13
  • @AndrewThompson This whole thread stuff is very confusing to me because I'm kinda new to Java programming, Is there a simple way of fixing this problem that I could understand because none of these things really make much sense to me... :( – Henry Jeff Oct 12 '15 at 21:23
  • 1
    Thread and simple don't really belong in the same sentence. I would recommend that you make the time to learn the basics of concurrency, as it will help you massively in the long run – MadProgrammer Oct 12 '15 at 21:29
  • The duplicate question has a self contained source that implements a solution using `SwingWorker`. It is only 70 lines of code and in that amount of code it also implements doing the long running task *repeatedly.* If you cannot get some progress from that answer, it is a good sign that you should not yet be attempting to work with multiple threads, or with rich client desktop apps. – Andrew Thompson Oct 12 '15 at 21:29
  • Oh, and given I could diagnose the problem from the title, I missed this: `setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 12 '15 at 21:31
  • @AndrewThompson Im just trying to make a Text area that I can output the progress of my program. My text just wont update and I wasn't sure why. I had no idea it would involve multiple threads or anything like that. Its a very simple program only for desktop use that will be used by me and some of my friends. – Henry Jeff Oct 12 '15 at 21:33
  • *"Im just trying to.."* ..teach the world to sing? ;) OK.. jokes aside. Things that are easy to say or describe or not necessarily easy to program. This is one where you've seemingly bitten off more than you can chew. Come back to this project after getting more experience with simpler things (is what I recommend). – Andrew Thompson Oct 12 '15 at 21:43

0 Answers0