?
Solved

i would like to time the actionperformed

Posted on 2005-03-19
21
Medium Priority
?
237 Views
Last Modified: 2010-03-31
i would like to time the actionperformed so that when the start button is pressed the action takes place with the interval of 5 seconds.

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
      private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
            jTextField5.setText(jTable1.getValueAt(0,0)+"");
            jTextField7.setText(jTable1.getValueAt(0,0)+"");
            jTextField2.setText(jTable1.getValueAt(0,0)+""+jTable1.getValueAt(0,1) +""+jTable1.getValueAt(0,2));
            jTextField6.setText(jTable1.getValueAt(0,0)+""+jTable1.getValueAt(0,1) +""+jTable1.getValueAt(0,2));


0
Comment
Question by:mnninrod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
  • 3
  • +1
21 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13583726
Since the time taken to execute the code you have should be negligible you can do

try {
      Thread.sleep(5000);
}
catch(Exception e) {
      e.printStackTrace();
}


after that code, but be aware it will lock the gui from access in the meantime
0
 
LVL 92

Expert Comment

by:objects
ID: 13583764
> so that when the start button is pressed the action takes place with the interval of 5 seconds.

not a good idea, swing is single thread so you'll block the gui.
what exactly is it you are trying to achieve.
0
 
LVL 92

Expert Comment

by:objects
ID: 13583767
Have a alook at the Timer class if you need to include any timeing in your code

http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Author Comment

by:mnninrod
ID: 13584997
copying the values from the jtable to a textarea within a time frame of 5 seconds when the start button is pressed. How can i archive this.
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 13585114

In the jButton2ActionPerformed method start a new thread, and call sleep in that method and execute the copying of the values after sleep.

If you are setting values of swing methods from a new thread, its also a good idea to do it from SwingUtilities.invokeLater(), because swing is not thread safe and doing it this way, makes it ok threadwise.

This is how your method should look like then:

  public void jButton2ActionPerformed(ActionEvent e)
  {
    Thread thread = new Thread(new Runnable(){
      public void run()
      {
        try
        {
          Thread.sleep(5000);
        }
        catch (InterruptedException e1)
        {
          e1.printStackTrace();
        }
        SwingUtilities.invokeLater(new Runnable()
        {
          public void run()
          {
            jTextField5.setText(jTable1.getValueAt(0,0)+"");
            jTextField7.setText(jTable1.getValueAt(0,0)+"");
            jTextField2.setText(jTable1.getValueAt(0,0)+""+jTable1.getValueAt(0,1) +""+jTable1.getValueAt(0,2));
            jTextField6.setText(jTable1.getValueAt(0,0)+""+jTable1.getValueAt(0,1) +""+jTable1.getValueAt(0,2));
          }
        });
      }
    });
    thread.start();
  }
0
 

Author Comment

by:mnninrod
ID: 13585276
What this methods does is that when you press start the whole action delays for five seconds then they are all copied at the same time. the values need to copied one at a time in the interval of 5 seconds.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13585479
Try the following:


String firstCell = jTable1.getValueAt(0,0) + "";
String secondCell = jTable1.getValueAt(0,1) + "";
String thirdCell = jTable1.getValueAt(0,2) + "";
String onePlusTwoPlusThree = firstCell + secondCell + thirdCell;

JTextComponent[] fields = new JTextComponent[] { jTextField5, jTextField7, jTextField2, jTextField6 };
String[] values = new String[] { firstCell, firstCell, onePlusTwoPlusThree, onePlusTwoPlusThree };


// This in your actionPerformed

new Thread(new SteppedTextFieldSetter(fields, values, 5000)).start();

// Inner class

class SteppedTextFieldSetter implements Runnable {
      private JTextComponent[] fields;
      private String[] values;
      private long totalInterval;

      public SteppedTextFieldSetter(JTextComponent[] fields, String[] values, long totalInterval) {
            this.fields = fields;
            this.values = values;
            this.totalInterval = totalInterval;
      }

      public void run() {
            // May need some range-checking here
            for(int i = 0;i < fields.length;i++) {
                  if (fields[i] != null && values[i] != null) {
                        fields[i].setText(values[i]);
                  }
                  try {
                        Thread.sleep(totalInterval / (values.length - 1));
                  }
                  catch(InterruptedException e) {
                        e.printStackTrace();
                  }
            }
      }
}
0
 

Author Comment

by:mnninrod
ID: 13585827
i have imported java.awt but then then i getting an  error  on the the JTextComponent and SteppedTextFieldSetter.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13585845
import javax.swing.text.JTextComponent ;

SteppedTextFieldSetter, as mentioned in my comment, should be placed as an inner class

0
 

Author Comment

by:mnninrod
ID: 13587300
new Thread(new SteppedTextFieldSetter(fields, values, 5000)).start(); this line is giving me thre errors on the steppedtextfieldsetter and fields and values.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13587317
Please post the errors
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13587319
I have tested the code and it works fine here
0
 

Author Comment

by:mnninrod
ID: 13587389
these are the errors and the segment code

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
            //JTextComponent[] fields = new JTextComponent[] { jTextField5, jTextField7, jTextField2, jTextField6 };
        //String [] values = new String[] { firstCell, firstCell, onePlusTwoPlusThree, onePlusTwoPlusThree };
            
            new Thread(new SteppedTextFieldSetter(fields, values, 5000)).start();
            
            class SteppedTextFieldSetter implements Runnable {
            //new Thread(new SteppedTextFieldSetter(fields, values, 5000)).start();
         JTextComponent[] fields;
        public String[] values;
        private long totalInterval;

     public SteppedTextFieldSetter(JTextComponent[] fields, String[] values, long totalInterval) {
          this.fields = fields;
          this.values = values;
          this.totalInterval = totalInterval;
     }

     public void run() {
          // May need some range-checking here
          for(int i = 0;i < fields.length;i++) {
               if (fields[i] != null && values[i] != null) {
                    fields[i].setText(values[i]);
               }
               try {
                    Thread.sleep(totalInterval / (values.length - 1));
               }
               catch(InterruptedException e) {
                    e.printStackTrace();
               }
          }
     }
}


symbol  : class SteppedTextFieldSetter
location: class javaapplication4.microprocessor
        new Thread(new SteppedTextFieldSetter(fields, values, 5000)).start();
Z:\JavaApplication4\src\javaapplication4\microprocessor.java:219: cannot resolve symbol
symbol  : variable fields
location: class javaapplication4.microprocessor
        new Thread(new SteppedTextFieldSetter(fields, values, 5000)).start();
Z:\JavaApplication4\src\javaapplication4\microprocessor.java:219: cannot resolve symbol
symbol  : variable values
location: class javaapplication4.microprocessor
        new Thread(new SteppedTextFieldSetter(fields, values, 5000)).start();
3 errors



0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13587395
>>class SteppedTextFieldSetter implements Runnable {

That should be an inner class as i mentioned. At the moment it's inside a method. It should not be inside any other block
0
 

Author Comment

by:mnninrod
ID: 13587429
i am still getting the errors on  the variable  fields and variable  values

symbol  : variable fields
location: class javaapplication4.microprocessor
        new Thread(new SteppedTextFieldSetter(fields, values, 5000)).start();

symbol  : variable values
location: class javaapplication4.microprocessor
        new Thread(new SteppedTextFieldSetter(fields, values, 5000)).start();
2 errors
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13587437
Well if it's the same code (apart from the latest adjustment) as the last posting, you've commented out those variables for some reason
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 900 total points
ID: 13587443
This should be your actionPerformed:


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

      String firstCell = jTable1.getValueAt(0,0) + "";
      String secondCell = jTable1.getValueAt(0,1) + "";
      String thirdCell = jTable1.getValueAt(0,2) + "";
      String onePlusTwoPlusThree = firstCell + secondCell + thirdCell;

      JTextComponent[] fields = new JTextComponent[] { jTextField5, jTextField7, jTextField2, jTextField6 };
      String[] values = new String[] { firstCell, firstCell, onePlusTwoPlusThree, onePlusTwoPlusThree };
      
      new Thread(new SteppedTextFieldSetter(fields, values, 5000)).start();

}
0
 

Author Comment

by:mnninrod
ID: 13587465
here is my code i dont think i have commented anything out but it is still giving me the two errors

import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import javax.swing.*;
import java.awt.*;
import javax.swing.text.JTextComponent ;

public class microprocessor extends javax.swing.JFrame {
   
    /** Creates new form NewJFrame */
    public microprocessor() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jFrame1 = new javax.swing.JFrame();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextArea1 = new javax.swing.JTextArea();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jTextField7 = new javax.swing.JTextField();
        pause = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jTable1 = new javax.swing.JTable();
            setTitle("microprocessor");
            String firstCell = jTable1.getValueAt(0,0) + "";
        String secondCell = jTable1.getValueAt(0,1) + "";
        String thirdCell = jTable1.getValueAt(0,2) + "";
        String onePlusTwoPlusThree = firstCell + secondCell + thirdCell;

JTextComponent[] fields = new JTextComponent[] { jTextField5, jTextField7, jTextField2, jTextField6 };
String[] values = new String[] { firstCell, firstCell, onePlusTwoPlusThree, onePlusTwoPlusThree };


            //Timer timer;


        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("continue");
        jButton1.setText("continue");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 470, -1, -1));

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 70, 120, 20));

        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        getContentPane().add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 200, 110, 20));

        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });

        getContentPane().add(jTextField3, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 70, 110, 20));

        getContentPane().add(jTextArea1, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 140, -1, -1));

        jTextField4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField4ActionPerformed(evt);
            }
        });

        getContentPane().add(jTextField4, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 110, 190, 80));

        jTextField5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField5ActionPerformed(evt);
            }
        });

        getContentPane().add(jTextField5, new org.netbeans.lib.awtextra.AbsoluteConstraints(660, 100, 80, 20));

        jTextField6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField6ActionPerformed(evt);
            }
        });

        getContentPane().add(jTextField6, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 430, 170, 20));

        getContentPane().add(jTextField7, new org.netbeans.lib.awtextra.AbsoluteConstraints(570, 200, 90, 20));

        pause.setText("pause");
        pause.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pauseActionPerformed(evt);
            }
        });
        pause.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                pauseMousePressed(evt);
            }
        });

        getContentPane().add(pause, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 470, -1, -1));
            jButton2.setMnemonic('G');

        jButton2.setText("start");
        jButton2.addActionListener(new  java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                        //if (evt.getSource()==jButton2)
                        //{
                              //jTextField5.setText((String)jTable1.getModel().getValueAt(1, 2));
                        //}
               jButton2ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 470, -1, -1));

        jLabel1.setText("A");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 50, -1, -1));

        jLabel2.setText("mainmemory");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 240, -1, -1));

        jLabel3.setText("ir");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 410, 10, 10));

        jLabel4.setText("MDR");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 180, -1, -1));

        jLabel5.setText("B");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 50, -1, -1));

        jLabel6.setText("MAR");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(610, 180, -1, -1));

        jLabel7.setText("PC");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(700, 80, -1, -1));

        getContentPane().add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

            jTable1.setModel(new javax.swing.table.DefaultTableModel(
                                                                                                 
                                                                                     
            new Object [][] {
                {new Integer(000), new Integer(01), new Integer(999)},
                {new Integer(001), new Integer(20) ,new Integer(997)},
                {new Integer(002), new Integer(02), new Integer(999)},
                        {new Integer(003), new Integer(0), new Integer(0)},
                        {new Integer(0), new Integer(0), new Integer(0)},
                        {new Integer(997), new Integer(01), new Integer(200)},
                        {new Integer(998), new Integer(00), new Integer(300)},                                                                        
                      {new Integer(997), new Integer(01), new Integer(807) }},
         //   },
                  //String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
             new String [] {
                                           "Title 1", "Title 2", "Title 3"}
            //}
        ));
        getContentPane().add(jTable1, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 260, -1, -1));

        pack();
    }//GEN-END:initComponents

    private void pauseMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pauseMousePressed
        // TODO add your handling code here:
    }//GEN-LAST:event_pauseMousePressed

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField2ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
            
            new Thread(new SteppedTextFieldSetter(fields, values, 5000)).start();
            
                  
      }      
    private void jTextField6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField6ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField6ActionPerformed

    private void pauseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pauseActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_pauseActionPerformed

    private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {
            //jTextField5.setText((String)jTable1.getModel().getValueAt(1, 2));
            //getContentPane().setDefaultButton(jButton2);
            
            //jText.setText((String)jTable1.getModel().getValueAt(0, 0));
            
            //GEN-FIRST:event_jTextField5ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField5ActionPerformed

    private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField4ActionPerformed

    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new microprocessor().setVisible(true);
            }
        });
    }
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JButton pause;
    // End of variables declaration//GEN-END:variables
}  
class SteppedTextFieldSetter implements Runnable {
            //new Thread(new SteppedTextFieldSetter(fields, values, 5000)).start();
         JTextComponent[] fields;
        public String[] values;
        private long totalInterval;

     public SteppedTextFieldSetter(JTextComponent[] fields, String[] values, long totalInterval) {
          this.fields = fields;
          this.values = values;
          this.totalInterval = totalInterval;
     }

     public void run() {
          // May need some range-checking here
          for(int i = 0;i < fields.length;i++) {
               if (fields[i] != null && values[i] != null) {
                    fields[i].setText(values[i]);
               }
               try {
                    Thread.sleep(totalInterval / (values.length - 1));
               }
               catch(InterruptedException e) {
                    e.printStackTrace();
               }
          }
     }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13587478
That's because your jButton2ActionPerformed code is not the same as the code i just posted
0
 
LVL 92

Expert Comment

by:objects
ID: 13587600
Far easier to use a Timer :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13587614
:-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question