I'm still very new to the world of programming and have recently noticed, that whenever I tell the programm to idle for some seconds between the code, it instead sleeps at the start and then goes through the remaining code.
I've tried various ways like thread.sleep()
or Timers but I never get what I want.
Here is an example:
public void Console(){
Console.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Console.setSize(500, 500);
Console.setLocationRelativeTo(null);
Console.setResizable(false);
Console.setVisible(true);
Console.setTitle("Console");
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
Console.setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.setBackground(new Color(47, 79, 79));
cinput.setBounds(10, 442, 353, 20);
contentPane.add(cinput);
cinput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
in();
cinput.requestFocus();
}
});
cinput.setColumns(10);
cinput.requestFocus();
JButton Enter = new JButton("Enter");
Enter.setBounds(373, 439, 111, 23);
contentPane.add(Enter);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 10, 474, 421);
contentPane.add(scrollPane);
cmd.setEditable(false);
cmd.setFont(new Font("Courier New", Font.PLAIN, 18));
cmd.setForeground(Color.GREEN);
cmd.setText("CONSOLE\n");
cmd.setBackground(Color.BLACK);
cmd.setLineWrap(true);
cmd.setWrapStyleWord(true);
scrollPane.setViewportView(cmd);
Enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
in();
cinput.requestFocus();
}
});
}
private void addText(JTextArea textArea, String text) {
String input = textArea.getText();
String output = input + text;
textArea.setText(output);
}
private void in()
{
String input = cinput.getText();
cinput.setText("");
String text;
text = input;
addText(cmd, "> " + text + "\n");
if(text.equals("start"))
{
addText(cmd, "1");
// SLEEP HERE
Thread.sleep(1000);
// -------------------
addText(cmd, "2");
}
else if(text.equals("exit"))
{
Console.dispose();
}
}
It should look something like this:
In this very basic 'Console', whenever I type 'start' in the textbox and hit enter, I want the number '1' to appear first and after 1000 mseconds the number '2' should appear, which it doesn't!
Is there any way to tell the programm to sleep between statements instead of always sleeping at the beginnig of the function?
Thanks in advance