Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dergaderg

macrumors member
Original poster
Feb 9, 2009
48
0
my jFrame is not updated the gui with each loop. any help is appriciated

Code:
 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Gui.java
 *
 * Created on Aug 30, 2011, 11:00:43 AM
 */

package countdownproject;

/**
 *
 * @author mdergosits
 */
public class Gui extends javax.swing.JFrame {

    /** Creates new form Gui */
    public Gui() {
        initComponents();
jSlider1.setMajorTickSpacing(10);
jSlider1.setMinorTickSpacing(1);
jSlider1.setPaintTicks(true);
jSlider1.setPaintLabels(true);
jSlider2.setMajorTickSpacing(200);
jSlider2.setMinorTickSpacing(100);
jSlider2.setPaintTicks(true);
jSlider2.setPaintLabels(true);
jSlider1.setMaximum(100);
jSlider1.setMinimum(10);
jSlider2.setMaximum(1000);
jSlider2.setMinimum(100);
int valueOfSlider = jSlider2.getValue();

    
    }

    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jSlider1 = new javax.swing.JSlider();
        jSlider2 = new javax.swing.JSlider();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("jLabel1");

        jButton1.setText("Count Down");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider1StateChanged(evt);
            }
        });

        jSlider2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider2StateChanged(evt);
            }
        });

        jLabel2.setText("jLabel2");

        jLabel3.setText("jTextField1");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(50, 50, 50)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jSlider2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jSlider1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createSequentialGroup()
                        .add(59, 59, 59)
                        .add(jButton1)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 11, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jLabel2)
                        .add(jLabel1))
                    .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(65, 65, 65))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(54, 54, 54)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jSlider1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jSlider2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButton1)
                            .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(jLabel2))
                .add(123, 123, 123))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   int count = jSlider1.getValue(); 
   int delay = jSlider2.getValue();
        int valueOfSlider = jSlider2.getValue();
         int valueOfSlider2 = jSlider1.getValue();
        while  (count > 0)
        {
            String value3 = ""+count;
            jLabel3.setText(value3);
            repaint();
            count--; 
         
             try { 
                 Thread.sleep(delay);
             }
              
               catch(InterruptedException ie)
               {
                    
                }
           
        
        }
        
        
        
    }                                        

    private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {                                      
         int valueOfSlider2 = jSlider1.getValue();
        String value2 = ""+valueOfSlider2;
        jLabel1.setText(value2);
        
    }                                     

    private void jSlider2StateChanged(javax.swing.event.ChangeEvent evt) {                                      
      
        int valueOfSlider = jSlider2.getValue();
        String value = ""+valueOfSlider;
        jLabel2.setText(value);
    }                                     

   
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Gui().setVisible(true);

            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jLabel3;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JSlider jSlider2;
    // End of variables declaration                   

}
 
Last edited by a moderator:
repaint() doesn't actually repaint a component. It just adds an event to the event queue that causes a call to the component's paint method. Because your loop blocks the event thread (always a bad thing to do in a GUI), the component doesn't get repainted.

You'll need to use a timer instead that periodically triggers a method that decrements the count and updates the label.

Code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    final int startCount = jSlider1.getValue();
    final int delay = jSlider2.getValue();
    final Timer timer = new Timer(delay, null);
    timer.addActionListener(
            new ActionListener() {
                private int count = startCount;
                public void actionPerformed(ActionEvent evt2) {
                    System.out.println("Tick");
                    count--;
                    String value3 = ""+count;
                    jLabel3.setText(value3);
                    if (count < 1) {
                        timer.stop();
                        System.out.println("Brrrrrrrrrrrrrng");
                    }
                }
            }
        );
    String value3 = ""+startCount;
    jLabel3.setText(value3);
    timer.start();
}
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.