Results 1 to 5 of 5
  1. #1
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default can't get JTextField to update

    (I'm a very novice programmer!) All I'm trying to do is see seconds counting away in a JTextField. I use my JButton secondButton simply to start myTimer. Here's the relevant bits of my code:

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class CountTime extends JFrame
    {
    private int myCounter = 0;
    private Container cp;
    private JButton exitButton = new JButton("Exit");
    private JButton firstButton = new JButton("First");
    private JButton secondButton = new JButton("Second");
    private JTextField myOutput = new JTextField("output here",30);
    public Graphics myGraphics;
    private int delay = 1000; //milliseconds
    ActionListener taskPerformer = new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    myOutput.setText("myCounter = " + (myCounter + 1));
    myOutput.repaint();
    }
    };
    Timer myTimer = new Timer(delay, taskPerformer);


    public CountTime(String title)
    {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100,100,700,400);
    cp = getContentPane(); cp.setLayout(new GridLayout(2,2));
    cp.add(exitButton); cp.add(firstButton); cp.add(secondButton);
    cp.add(myOutput);
    exitButton.addActionListener(new ExitButtonWatcher());
    firstButton.addActionListener(new FirstButtonWatcher());
    secondButton.addActionListener(new SecondButtonWatcher());
    myGraphics = getGraphics();
    }

    private class SecondButtonWatcher implements ActionListener
    {
    public void actionPerformed(ActionEvent a)
    {
    myTimer.start();
    }
    } // end of: private class SecondButtonWatcher implements ActionListener


    } // end of: public class CountTime extends JFrame

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Perhaps you want to increment the counter variable in the Timer, and then set the JTextField with it, otherwise counter will never change, e.g.:
    Java Code:
    public void actionPerformed(ActionEvent evt) {
       myCounter = myCounter + 1;  // or myCounter++; works as well.
       myOutput.setText("myCounter = " + myCounter);
       
    }
    Last edited by Fubarable; 11-29-2010 at 03:32 AM.

  3. #3
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default silly me!

    Of course; silly me!

    Thank you.

    But now I've got another problem (probably sill again): my counter is indeed incrementing, but it's going up by two each time, not one. Can't see why.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rippon View Post
    But now I've got another problem (probably sill again): my counter is indeed incrementing, but it's going up by two each time, not one. Can't see why.
    Neither can I (without the updated code that is).

    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]

  5. #5
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Thanks, but I seem to have sorted my problem. Now I'm just going to try that [code] tag thing.

    Java Code:
        public Graphics myGraphics;
        private int delay = 1000; //milliseconds
              ActionListener taskPerformer = new ActionListener() {
                public void actionPerformed(ActionEvent evt) {//...Perform a task...
                    myCounter++;
                    myOutput.setText("myCounter = " + myCounter);
                    //myOutput.repaint();
                                                             }
                                                                  };
            Timer myTimer = new Timer(delay, taskPerformer);

Similar Threads

  1. JTextField
    By gancio in forum AWT / Swing
    Replies: 20
    Last Post: 08-26-2009, 04:11 PM
  2. JTextField won't update from subclass
    By Edward in forum New To Java
    Replies: 9
    Last Post: 06-29-2009, 06:45 AM
  3. Replies: 1
    Last Post: 01-30-2009, 07:44 PM
  4. JtextField
    By kashifu in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 05:25 PM
  5. help with JTextfield
    By gary in forum New To Java
    Replies: 4
    Last Post: 07-11-2007, 02:58 PM

Tags for this Thread

Posting Permissions

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