Results 1 to 13 of 13
  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default What the heck is wrong with this code.

    Java Code:
    \
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    
    public class Timing {
    	JCheckBox checkbox[];
    	
    	public Timing()
    	{
    		JFrame frame = new JFrame();
    		frame.setTitle("Swing Timers");
    		
    		JPanel panel = new JPanel();
    		
    		checkbox = new JCheckBox[7];
    		for(int count=0;count<checkbox.length;count++)
    		{
    			
    			panel.add(checkbox[count]);
    			checkbox[count].setSelected(false);
    			checkbox[count].setEnabled(false);
    		}
    		
    		frame.getContentPane().add(BorderLayout.CENTER, panel);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(100,25);
    		frame.setVisible(true);
    	}
    	
    	public void doSomething()
    	{
    		Timer timer = new Timer(3000,new ActionListener()
    		{
    			int count=0;
    			public void actionPerformed(ActionEvent event)
    			{
    				checkbox[count].setSelected(true);
    				count++;
    			}
    		});
    		timer.setRepeats(true);
    		timer.start();
    		
    	}
    	
    	public static void main(String args[])
    	{
    		new Timing().doSomething();
    	}
    }
    I don't get Swing Timers. Isn't there a way to tell the blasted timer how many times you want it to carry out the actionPerformed() function. If someone could tell me a better way of writing this code, i'd really appreciate it. My brain's feeling so numb,

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Can't you just check count after you increment it, and if it's greater than or equal to a certain number, call Timer.stop()?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    I did that, it asked me to make the timer variable final. I didn't understand why that was necessary.

  4. #4
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Even if i make timer final, i get an error that timer may not have been initialized.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Quote Originally Posted by eLancaster View Post
    Even if i make timer final, i get an error that timer may not have been initialized.
    That makes sense. That's how it should be. Hint: what is the source of the ActionEvent? Have a look at the API of ActionEvent for some useful functions.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    I don't get it!

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    The first problem I have with the code you posted is that it does not execute.
    Where do you fill the array: checkbox = new JCheckBox[7];

    Your problem is not with the Timer class. It is with inner anonymous classes.
    Make a named class for the ActionListener.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Quote Originally Posted by eLancaster View Post
    I don't get it!
    Which part don't you get? Please check out the link in my signature on asking smart questions.

    Did you take a look at the API for ActionEvent? Hint: You're looking for a method so you can get the source of the event.

    Or just follow Norm's advice.
    Last edited by KevinWorkman; 06-06-2011 at 08:53 PM.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    One more Kevin and you're into 4 digits!!!

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Quote Originally Posted by Norm View Post
    One more Kevin and you're into 4 digits!!!
    Yaaay! What do I win?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Quote Originally Posted by Norm View Post
    One more Kevin and you're into 4 digits!!!
    And another 9000 will get you your opposable thumb

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Which you really don't need to work a keyboard

  13. #13
    sibernewf is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

Similar Threads

  1. What the heck??? Need urgent help!
    By Julian in forum New To Java
    Replies: 4
    Last Post: 05-07-2011, 02:26 PM
  2. What is wrong with this code?
    By Mythreadings in forum New To Java
    Replies: 38
    Last Post: 11-19-2010, 12:43 AM
  3. Please tell me what i am doing wrong in this code
    By Basit781 in forum CLDC and MIDP
    Replies: 11
    Last Post: 05-20-2010, 05:08 PM
  4. Need help! something wrong in my code
    By novak100 in forum New To Java
    Replies: 2
    Last Post: 11-18-2009, 11:59 PM

Posting Permissions

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