Results 1 to 5 of 5
Thread: can't get JTextField to update
- 11-29-2010, 02:14 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
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
-
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 02:32 AM.
- 11-29-2010, 02:47 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
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.
-
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]
- 11-29-2010, 12:34 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
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
-
JTextField
By gancio in forum AWT / SwingReplies: 20Last Post: 08-26-2009, 03:11 PM -
JTextField won't update from subclass
By Edward in forum New To JavaReplies: 9Last Post: 06-29-2009, 05:45 AM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
JtextField
By kashifu in forum Advanced JavaReplies: 2Last Post: 06-27-2008, 04:25 PM -
help with JTextfield
By gary in forum New To JavaReplies: 4Last Post: 07-11-2007, 01:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks