Results 1 to 7 of 7
  1. #1
    Joaim is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default 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:

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

  2. #2
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,030
    Rep Power
    4

    Default 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.

  3. #3
    Joaim is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Problems Making a variable known in the whole script

    Quote Originally Posted by SurfMan View Post
    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.

  4. #4
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,030
    Rep Power
    4

    Default Re: Problems Making a variable known in the whole script

    Quote Originally Posted by Joaim View Post
    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.

  5. #5
    Joaim is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default 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:

    Java 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);
        	}
        }

  6. #6
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,030
    Rep Power
    4

    Default 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:
    Java 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.
    Last edited by SurfMan; 02-16-2013 at 10:52 PM.

  7. #7
    Joaim is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Problems Making a variable known in the whole script

    Wow thanks! I will try it out, and let you know here later.

Similar Threads

  1. Can I pass a Java variable to bash script?
    By steve-ss in forum New To Java
    Replies: 2
    Last Post: 05-30-2012, 11:18 PM
  2. Having problems making a runnable jar
    By Xainey in forum Eclipse
    Replies: 5
    Last Post: 12-27-2011, 06:21 AM
  3. Replies: 12
    Last Post: 12-12-2010, 07:04 PM
  4. Using Script src with a variable file name
    By Sidders at CHEP in forum New To Java
    Replies: 6
    Last Post: 03-03-2009, 06:18 PM
  5. Replies: 1
    Last Post: 10-02-2008, 04:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •