JFrame: Sleep between statements - java

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

While technically the answers above are true, your problem is
You must do all Swing work on the Swing Event Dispatch Thread.
You really, really, don't want to sleep the main Swing Event Dispatch Thread!
What you want to do instead is create a helper class that wraps the javax.swing.Timer (NOT the java.util.timer) that allows you to easily run code on the event thread after a certain number of milliseconds has passed.
public abstract class LazyAction implements Runnable {
public LazyAction(int millis) {
Timer t = new Timer(millis, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try{
LazyAction.this.run();
catch(Exception e){
//Your error handling code
}
}
});
t.setRepeats(false);
t.start();
}}
Now, to use this code, you then call it by:
addText(cmd, "1");
new LazyAction(300){
#Override
public void run() {
addText(cmd, "2");
}
}
(Note if you are using Java 8 you could use Lambdas instead, in that case LazyAction would take in a Runnable rather than implementing Runnable...)

It looks like you are trying to put to sleep the main Thread which is not the best idea. Run your own Thread and put it to sleep.
Try this:
System.out.println(1);
SwingUtilities.invokeLater(() -> {
new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(2);
}
}).start();
});
You can also do this without Thread.sleep():
long tick = 1000;
long startTime = System.currentTimeMillis();
while (true) {
long now = System.currentTimeMillis();
while (now - startTime > tick) {
System.out.println("Seconds from start time: " + (int) (tick / 1000));
tick += 1000;
}
}
If it suites you just tweak the above example for your needs(it also accounts for possible lag so the number of seconds will still be correct).

Put a sleep statement before adding the first line of text:
Thread.sleep(1000);
addText(cmd, "1");
// SLEEP HERE
Thread.sleep(1000);
// -------------------
addText(cmd, "2");

Related

How to set Action Listener to 3 buttons

I'm trying to make a stopwatch with three buttons, "Start", "Pause", and "Stop". My instructor only taught us how to set Action Listeners to two buttons. How do I set up Action Listeners to three buttons? Here is my coding so far
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
JButton pauseButton = new JButton("Pause");
startButton.addActionListener(this);
stopButton.addActionListener(this);
public void actionPerformed(ActionEvent actionEvent) {
Calendar aCalendar = Calendar.getInstance();
if (actionEvent.getActionCommand().equals("Start")){
start = aCalendar.getTimeInMillis();
aJLabel.setText("Stopwatch is running...");
} else {
aJLabel.setText("Elapsed time is: " +
(double) (aCalendar.getTimeInMillis() - start) / 1000 );
}
}
I haven't made any Action Listeners for my "Pause" feature yet because I don't know how to pause the timer anyway. But I wanted to link the action to the button before I figured out how to pause.
What you are looking for is a if-then-else if-then statement.
Basically, add the ActionListener to all three buttons as you have been doing...
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
JButton pauseButton = new JButton("Pause");
startButton.addActionListener(this);
stopButton.addActionListener(this);
pauseButton.addActionListener(this);
Then supply a if-else-if series of conditions to test for each possible occurrence (you are expecting)
public void actionPerformed(ActionEvent e) {
Calendar aCalendar = Calendar.getInstance();
if (e.getSource() == startButton){
start = aCalendar.getTimeInMillis();
aJLabel.setText("Stopwatch is running...");
} else if (e.getSource() == stopButton) {
aJLabel.setText("Elapsed time is: " +
(double) (aCalendar.getTimeInMillis() - start) / 1000 );
} else if (e.getSource() == pauseButton) {
// Do pause stuff
}
}
Take a closer look at The if-then and if-then-else Statements for more details
Instead of trying to use the reference to the buttons, you might consider using the actionCommand property of the AcionEvent instead, this means you won't need to be able to reference the original buttons...
public void actionPerformed(ActionEvent e) {
Calendar aCalendar = Calendar.getInstance();
if ("Start".equals(e.getActionCommand())){
start = aCalendar.getTimeInMillis();
aJLabel.setText("Stopwatch is running...");
} else if ("Stop".equals(e.getActionCommand())) {
aJLabel.setText("Elapsed time is: " +
(double) (aCalendar.getTimeInMillis() - start) / 1000 );
} else if ("Pause".equals(e.getActionCommand())) {
// Do pause stuff
}
}
It also means that you could re-use the ActionListener for things like JMenuItems, so long as they had the same actionCommand...
Now, having said that, I would encourage you not to follow this paradigm. Normally, I would encourage you to use the Actions API, but that might be a little too advanced for where you're up to right now, instead, I would encourage you to take advantage of Java's anonymous class support, for example....
startButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
start = aCalendar.getTimeInMillis();
aJLabel.setText("Stopwatch is running...");
}
});
stopButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
aJLabel.setText("Elapsed time is: "
+ (double) (aCalendar.getTimeInMillis() - start) / 1000);
}
});
pauseButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Do pause stuff
}
});
This isolates the responsibility for each button to a single ActionListener, which makes it easier to see what's going on and when required, modify them without fear or affecting the others.
It also does away with the need to maintain a reference to the button (as it can be obtained via the ActionEvent getSource property)
If you don't want to implement ActionListener you can add anonymous listener to your button like this:
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
JButton pauseButton = new JButton("Pause");
startButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
//start action logic here
}
});
stopButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
//stop action logic here
}
});
pauseButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
//action logic here
}
});
And this solution have to work :)
All you need to add is this after the buttons creation.
startButton.setActionCommand("Start");
stopButton.setActionCommand("Stop");
pauseButton.setActionCommand("Pause");
and in actionPerformed method use this.
switch(actionEvent.getActionCommand())
{
// cases
}

Java GUI/Swing, stopping the thread, passing integers between classes

I'm trying to make a simulation of a roulette game using GUI/Swing for my upcoming exam. I have two classes, one is called GUI and is actually the code used for the components, such as JFrame, JOptionPane, JButtons etc. The other one extends Thread and is supposed to show random numbers on a small JLabel, and the run method goes something like this:
public void run() {
int k = 0;
for (int i = 0; i < 50; i++) {
k = (new Random().nextInt(37));
label.setText(k + " ");
label.setFont(new Font("Tahoma", Font.BOLD, 56));
label.setForeground(Color.yellow);
try {
sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
And in the GUI class I just want to TAKE the number from the last iteration of the above loop, and then pass it to a new int, which I'm going to use later in the GUI class.
Any ideas?
Use Swing Timer instead of Thread.sleep that sometime hangs the whole swing application.
Please have a look at How to Use Swing Timers
Timer timer = new Timer(50, new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
//next call from here
}
});
timer.setRepeats(false);
timer.start();
I just want to TAKE the number from the last iteration of the above loop, and then pass it to a new int, which I'm going to use later in the GUI class.
Just create a method (setter) in another class that accepts int and call it from this class for last call.
Sample code:
private int counter = 0;
private Timer timer;
...
final JLabel label = new JLabel();
label.setFont(new Font("Tahoma", Font.BOLD, 56));
label.setForeground(Color.yellow);
Thread thread = new Thread(new Runnable() {
public void run() {
timer = new Timer(50, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (counter++ < 50) {
int k = (new Random().nextInt(37));
label.setText(k + " ");
} else {
timer.stop();
label.setText("next call");
}
}
});
timer.setRepeats(true);
timer.start();
}
});
thread.start();
snapshot:

Java Timer usage with JButtons

I have problem that I don't understand, how to correctly use the Java timer with JButton.
The idea of what I need -
When I click on JButton with text "0" then starts the timer counting from two seconds till zero.
When button is released program checks the situation:
if timer now is 0 then it shows in JTextField sign "+", else it shows "0".
Here is my code of program. Can someone please add the things that I need to make the program work like the idea I want?
public class DialPanel extends JPanel {
private MainFrame frame;
public DialPanel(MainFrame frame) {
this.frame = frame;
this.setLocation(0, 90);
this.setSize(300, 290);
this.setLayout(null);
this.setBackground(color);
this.initContent();
}
// -------------------------------------------------------------------------
private JButton btnNumZero;
private JTextField txfNumber;
private void initContent() {
txfNumber = new JTextField();
this.add(txfNumber);
txfNumber.setSize(190, 30);
txfNumber.setLocation(30, 0);
txfNumber.setFocusable(false);
txfNumber.addActionListener(controller);
btnNumZero = new JButton();
this.add(btnNumZero);
btnNumZero.setText("0");
btnNumZero.setFocusable(false);
btnNumZero.setSize(30, 30);
btnNumZero.setLocation(10, 10);
btnNumZero.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
//Start someTimer countdown from two seconds
}
});
btnNumZero.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent e) {
//Stop someTimer
//if someTimer == 0 seconds then do this line:
txfNumber.setText("+");
//else do this line:
txfNumber.setText("0");
}
});
}
}
Excuse me if there is some unnecessary error with code. I deleted and changed a lot of things from the real one code so that this could be more understandable and clear for reading.
Instead of using a Timer, you may want to just make use of System.currentTimeMillis(). The Timer may not be a timer in the sense you are looking for, as a stopwatch type object.
You could do something like this
long startTime;
long endTime;
btnNumZero.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
startTime = System.currentTimeMillis();
}
});
btnNumZero.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent e) {
endTime = System.currentTimeMillis();
long difference = endTime - startTime;
if (difference > 2000)
txfNumber.setText("+");
else
txfNumber.setText("0");
}
});

Swing ProgressMonitor not working

I am trying to learn ProgressMonitor in Java Swing.
I created this simple test code -
public class ProgressMonitorTest extends JFrame
{
private JPanel contentPane;
private ProgressMonitor progressMonitor;
private JButton button;
private static ProgressMonitorTest frame;
private static boolean isFrameReady;
public JButton getButton()
{
return button;
}
public ProgressMonitor getProgressMonitor()
{
return progressMonitor;
}
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
frame = new ProgressMonitorTest();
frame.setVisible(true);
isFrameReady = true;
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
while(!isFrameReady)
{
//
}
frame.getButton().addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
try
{
for(int i=0;i<=10;i++)
{
final int percent = i;
SwingUtilities.invokeAndWait(new Runnable()
{
#Override
public void run()
{
frame.getProgressMonitor().setProgress(percent * 10);
frame.getProgressMonitor().setNote("Completed " + percent*10 + "%.");
}
});
try
{
Thread.sleep(1000);
}
catch(Exception ee)
{
//
}
}
}
catch(Exception es)
{
//
}
}
});
}
/**
* Create the frame.
*/
public ProgressMonitorTest()
{
isFrameReady = false;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
setTitle("Progress Monitor");
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
progressMonitor = new ProgressMonitor(frame, "Update in progress...", "", 0, 10);
button = new JButton("Click Here");
contentPane.add(button);
setContentPane(contentPane);
}
}
A few questions regarding this-
If I remove the isFrameReady check, the program says a NullPointerException at the line where I assign the button's action listener.
If I keep the above check, then clicking on the button does nothing.
Keeping the above check and then debugging this, I let it wait for some time before it gets to the line where the action listener. In this case, it works but immediately quits saying it can't call invokeAndWait from the event handling thread.
What am I missing in all this ? Can someone explain how to get this to work.
If I remove the isFrameReady check, the program says a
NullPointerException at the line where I assign the button's action
listener.
your use of isFrameReady ensures that you have created your frame successfully. inside your main, your posted request to event dispatch thread(EDT) using call EventQueue.invokeLater(new Runnable(){}): removing the check isFrameReady, you were going to call frame.getButton() in main thread but the frame have not been yet created by frame = new ProgressMonitorTest(); in the EDT and thus a NullPointerException occurs.
If I keep the above check, then clicking on the button does nothing.
you should understand by now, that above check is nothing to do with button click. The button is not doing anything because the GUI got freezed for violating swing's single threading rule. Put your incrementing for loop of the actionPerformed method inside another thread as the following code fragement shows and execute it from there. you will see that it works fine.
new Thread(){
public void run()
{
for(int i=0; i<10; i++)
{
//whatever you were doing.
}
}
}.start();
Keeping the above check and then debugging this, I let it wait for
some time before it gets to the line where the action listener. In
this case, it works but immediately quits saying it can't call
invokeAndWait from the event handling thread.
SwingUtitlies.invokeAndWait() blocks the current thread and waits until the EDT is done executing the task given to it. As actionPerformed() function is already running inside EDT, so calling SwingUtitlies.invokeAndWait() from the current thread:EDT would block the current thread:EDT which should not be allowed. Don't use invokeAndWait for this case. you should call SwingUtilities.invokeLater() instead.
However I don't think you will get anything until you understand Swing threading model. Read the javadoc and some internet resource. DO HAVE The book Filthy Rich Clients and try the example the book offered: You will have a greater knowledge in graphical effects then any other resource can provide.

Java Swing Concurrency display JTextArea

I need to execute/display a series of events from a Arraylist to a JTextArea, however, each Event gets execute with different time. Following is a quick example of my goal:
public void start(ActionEvent e)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
jTextArea.append("Test" + "\n");
try
{
Thread.sleep(3000);
} catch (InterruptedException e1)
{
e1.printStackTrace();
}
jTextArea.append("Test1" + "\n");
}
});
}
So right now, "Test" and "Test1" display on JTextArea after whole execution is completed.
How do I make "Test" display first, then 3 secs later, display "Test1"
Thank u all in advance
invokeLater schedules the runnable to run on the Event Dispatch Thread. You shouldn't sleep within it or you will starve the dispatch thread. Try using a separate worker thread instead:
Thread worker = new Thread(new Runnable(){
public void run(){
jTextArea.append("Test" + "\n");
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
jTextArea.append("Test1" + "\n");
}
});
worker.start();
If your tasks are time/cpu intensive, then yes, definitely use a background thread to do this such as a SwingWorker object or a Runnable run in a Thread. If however what you need to do is to stagger the display of something and all you are looking for is the Swing equivalent of Thread.sleep(3000), then your best option is to use a Swing Timer. There is an excellent tutorial on how to use these which you can find here: http://download.oracle.com/javase/tutorial/uiswing/misc/timer.html
For example:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Fu extends JPanel {
private static final int TIMER_DELAY = 600;
protected static final int MAX_COUNT = 20;
private JTextArea jTextArea = new JTextArea(10, 10);
private JButton startBtn = new JButton("Start");
private Timer timer;
public Fu() {
startBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startAction(e);
}
});
add(new JScrollPane(jTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
add(startBtn);
}
private void startAction(ActionEvent e) {
if (timer != null && timer.isRunning()) {
// prevent multiple instances of timer from running at same time
return;
}
timer = new Timer(TIMER_DELAY, new ActionListener() {
private int count = 0;
public void actionPerformed(ActionEvent e) {
if (count < MAX_COUNT) {
count++;
jTextArea.append("Test " + count + "\n");
} else {
jTextArea.append("Done! \n");
timer.stop();
timer = null;
}
}
});
timer.setInitialDelay(0);
timer.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Foo");
frame.getContentPane().add(new Fu());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
As pointed out, this is a bad idea, as you will block the event thread.
However, understanding the reason for this is important as well. As you seem to know, all code that affects the state of Swing components needs to happen in the event handling thread (which is the reason why invokeLater and friends should always be used).
What is a bit less better known is that paining code also executes in the event handling thread. When your call to Thread.sleep is executing, it's not only blocking the event thread, it's also blocking any painting of components. This is why the full update appears to happen in one go -- the JTextArea is updated but it can't be repainted until your run method returns.
Lots of info available here: http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

Categories

Resources