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!!