• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

i would like to time the actionperformed

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
mnninrod
Asked:
mnninrod
  • 10
  • 7
  • 3
  • +1
1 Solution
 
CEHJCommented:
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
 
objectsCommented:
> 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
 
objectsCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mnninrodAuthor Commented:
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
 
Dejan PažinHead of SW DevelopmentCommented:

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
 
mnninrodAuthor Commented:
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
 
CEHJCommented:
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
 
mnninrodAuthor Commented:
i have imported java.awt but then then i getting an  error  on the the JTextComponent and SteppedTextFieldSetter.
0
 
CEHJCommented:
import javax.swing.text.JTextComponent ;

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

0
 
mnninrodAuthor Commented:
new Thread(new SteppedTextFieldSetter(fields, values, 5000)).start(); this line is giving me thre errors on the steppedtextfieldsetter and fields and values.
0
 
CEHJCommented:
Please post the errors
0
 
CEHJCommented:
I have tested the code and it works fine here
0
 
mnninrodAuthor Commented:
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
 
CEHJCommented:
>>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
 
mnninrodAuthor Commented:
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
 
CEHJCommented:
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
 
CEHJCommented:
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
 
mnninrodAuthor Commented:
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
 
CEHJCommented:
That's because your jButton2ActionPerformed code is not the same as the code i just posted
0
 
objectsCommented:
Far easier to use a Timer :)
0
 
CEHJCommented:
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 10
  • 7
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now