0

I need to display a text in a text field, after certain amount of time in secs which is provided in another textbox. please give me suggestions,am a newbie to this type requirement. Thanks in advance.

hey thanks for all ur replies, am sorry that it should be done in javascript and i did it. thank u..

here z the code... in javascript

<script type="text/javascript" >

            function timer(){
             var textbox3 = document.getElementById('t2');
             var temp=textbox3.value*1000;
             alert(temp);
     setTimeout('myMethod()',temp);

     }
     function myMethod()
     {

     var textbox1 = document.getElementById('t1');
     var textbox3 = document.getElementById('t3');
     textbox3.value=textbox1.value;
    //alert("hi");
     }


</script>
<form >
<input type='text' name='txt1' id="t1"></input>
<input type='text' name='txt2' id="t2"></input>
<input type='text' name='txt3'id="t3"></input>
<input type="button" name="btn" value="schedule" onclick="timer()"></input>
</form>
user1927762
  • 25
  • 2
  • 7
  • 2
    you are looking for solution in which language? – Amit Khanna Dec 25 '12 at 07:07
  • thanks for reply,,am looking in java.. @AmitKhanna – user1927762 Dec 25 '12 at 07:08
  • what I understand is that you have two text fields, when you are done entering text in one field, after a few seconds you want this value to be copied to the second field. – Amit Khanna Dec 25 '12 at 07:17
  • yes @AmitKhanna but there is one more field where we can specify the time in secs,after the specified time the text should display in the other text field. – user1927762 Dec 25 '12 at 07:20
  • @user1927762 : Welcome to SO. Can you tell us what you have tried so far? **Please show us some effort. SO is not here to write a code for you** – Fahim Parkar Dec 25 '12 at 07:41

3 Answers3

4

Check this

public class TimerExample extends JFrame {

    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JButton btnSubmit ;
    private Timer timer;
    public TimerExample() {
        super("List");
    }

    public void createAndShowGUI() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        textField1 = new JTextField(20) ; 
        textField2 = new JTextField(2) ; 
        textField3 = new JTextField(20) ; 
        btnSubmit = new JButton("Submit");
        add(textField1);
        add(textField2);
        add(btnSubmit);
        add(textField3);

        btnSubmit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int sec = Integer.parseInt(textField2.getText());
                 timer = new Timer(sec*1000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        textField3.setText(textField1.getText());
                        timer.stop();
                    }
                });
                timer.start();
            }
        });
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TimerExample gui = new TimerExample();
                gui.createAndShowGUI();
            }
        });
    }
}
vels4j
  • 11,208
  • 5
  • 38
  • 63
  • excellent, i got the exact output what i required...thank u very much...for ur valuable answer.. – user1927762 Dec 25 '12 at 08:55
  • hey i accepted, am sorry that i just came to know that it should be done in javascript..any ways thanks for the correct answer..and i did the same in javascript. – user1927762 Dec 25 '12 at 09:47
  • The only problem is, if the user clicks the button more then once within the timers active period. Better to use a single instance of the timer and use Timer#isRunning - IMHO – MadProgrammer Dec 25 '12 at 09:50
1

As you are working with gui I'm assuming you kow about threading.

So start a thread having run like this.

..run(){
..while(true){
..textbox1.setText(textbox2.getText());
..Thread.sleep(time interval);
..}
..}

time interval is in milisecond. for 1 sec write 1000.(remove the beginning dots)

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
Arpit
  • 12,767
  • 3
  • 27
  • 40
0

Add a ChangeListener to the field1 from which you want to copy the text. Also keep a Timer property in the ChangeListner. On any change in the field1, stop the earlier timer if it is active, get the time from timeField and restart the timer.

ActionListener of timer can copy the value from field1 to field2.

Community
  • 1
  • 1
Amit Khanna
  • 489
  • 4
  • 16