Problems Making a variable known in the whole script
I am trying to make a simple stop watch, i'm just going to post my code here first, then explain my problem in the buttom:
Code:
package gui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
public class hovedpanel {
private JFrame frame;
private JTextField textField;
protected long startTime;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
hovedpanel window = new hovedpanel();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public hovedpanel() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 395, 154);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("Start");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//What happens when start button is pressed:
long startTime = System.nanoTime();
System.out.println(startTime);
}
});
btnNewButton.setToolTipText("Start time");
btnNewButton.setBounds(66, 97, 91, 23);
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("Stop");
btnNewButton_1.setToolTipText("Stop time");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//What happens when stop button is pressed:
Long endTime = System.nanoTime();
System.out.println("time: " + (float)(endTime - startTime) / 1E9);
}
});
btnNewButton_1.setBounds(230, 97, 91, 23);
frame.getContentPane().add(btnNewButton_1);
textField = new JTextField();
textField.setEditable(false);
textField.setBounds(151, 11, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
}
}
I am pretty sure my problem happens at line 67 where I endTime - startTime, but the varibale "startTime" is unknown/equals 0. because it's in a different action-listener?
But I am in no way sure.
To create the gui I used window-builder.
If anyone can explain me what I am doing wrong, you would save my day!
Re: Problems Making a variable known in the whole script
On line 53 you declare startTime again by using "long" in front of it. That makes it a local variable to the inner class. Delete the "long" part and it will assign it to the class variable.
Re: Problems Making a variable known in the whole script
Quote:
Originally Posted by
SurfMan
On line 53 you declare startTime again by using "long" in front of it. That makes it a local variable to the inner class. Delete the "long" part and it will assign it to the class variable.
Thanks for the help! It works now, crazy that such a little thing can really make a that big of a difference in programming.
Re: Problems Making a variable known in the whole script
Quote:
Originally Posted by
Joaim
Thanks for the help! It works now, crazy that such a little thing can really make a that big of a difference in programming.
It's not a little thing, it's an important concept: the scope (visibility) of a variable is the block where it's declared. You can declare a variable at class level (which then usually are called fields). But the "int i" in for( int i=0; i<9; i+ ) {} limits the scope of the "i" variable to the for-loop itself, not outside of it.
Re: Problems Making a variable known in the whole script
I see, I got it to the previous to work. But now I have a new problem, I would like my stopwatch to update real-time. The only thing I see that would be suitable, is a while loop, looping all the time updating the Seconds, but it doesn't really work: Here's my new code:
Code:
package gui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
public class hovedpanel {
private JFrame frame;
private JTextField textField;
protected long startTime;
private JButton btnReset;
public boolean turn = false;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
hovedpanel window = new hovedpanel();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public hovedpanel() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 395, 154);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("Start");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//What happens when start button is pressed:
startTime = System.nanoTime();
turn = true;
while (turn = true){
Long endTime = System.nanoTime();
textField.setText((float)(endTime - startTime) / 1E9 + " sekunder er der gået");
}
}
});
btnNewButton.setToolTipText("Start timer");
btnNewButton.setBounds(66, 97, 91, 23);
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("Stop");
btnNewButton_1.setToolTipText("Stop timer");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//What happens when stop button is pressed:
turn = false;
}
});
btnNewButton_1.setBounds(230, 97, 91, 23);
frame.getContentPane().add(btnNewButton_1);
textField = new JTextField();
textField.setText("Tryk start for at starte stopuret");
textField.setEditable(false);
textField.setBounds(10, 11, 367, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
btnReset = new JButton("Reset");
btnReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("Tryk start for at starte stopuret");
turn = false;
}
});
btnReset.setBounds(149, 55, 91, 23);
frame.getContentPane().add(btnReset);
}
}
Re: Problems Making a variable known in the whole script
Checking for equality uses == and no =, in line 58. But then again this problem doesn't need a loop. Remove the turn variable, it's not needed. What you really want to use here is a Swing Timer (How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)). It's made for what you want. A basic Timer looks like this, with your textField used:
Code:
Timer t = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField.setText((float)(Systen.nanoTime() - startTime) / 1E9 + " sekunder er der gået");
}
});
The 100 means fire actionPerformed every 100 ms. Use btnNewButton to set the startTime and start the Timer, use btnNewButton_1 to stop the Timer.
Also, rename the btnNewButton_1 to stopButton or btnStop or anything more sensible. That will help tremendously when reading your own code.
Re: Problems Making a variable known in the whole script
Wow thanks! I will try it out, and let you know here later.