Member Avatar for Ralphael

I am cresting a digital clock and I am having troubles updating the interface please can someone help me with this. I have the code below:

            ********/*
             * To change this template, choose Tools | Templates
             * and open the template in the editor.
             */
            package Interface;
            /**
             *
             * @author Adetola-Ralph
             */
            import java.awt.event.ActionEvent;
            //import javax.swing.event.*;

            public class Main extends javax.swing.JFrame {
                /**
                 * Creates new form Main
                 */
                public Main() {
                    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.
                 */
                @SuppressWarnings("unchecked")
                // <editor-fold defaultstate="collapsed" desc="Generated Code">
                private void initComponents() {

                    jPanel1 = new javax.swing.JPanel();
                    hour = new javax.swing.JLabel();
                    minute = new javax.swing.JLabel();
                    seconds = new javax.swing.JLabel();
                    jLabel4 = new javax.swing.JLabel();
                    jLabel5 = new javax.swing.JLabel();

                    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
                    setTitle("Clock");

                    jPanel1.setBackground(new java.awt.Color(0, 153, 0));
                    jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
                    jPanel1.setName("");

                    hour.setFont(new java.awt.Font("Agency FB", 0, 72)); // NOI18N
                    hour.setText("00");
                    hour.setName("hour");

                    minute.setFont(new java.awt.Font("Agency FB", 0, 72)); // NOI18N
                    minute.setText("00");
                    minute.setName("minute");

                    seconds.setFont(new java.awt.Font("Agency FB", 0, 72)); // NOI18N
                    seconds.setText("00");
                    seconds.setName("seconds");

                    jLabel4.setFont(new java.awt.Font("Agency FB", 0, 72)); // NOI18N
                    jLabel4.setText(":");
                    jLabel4.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
                    jLabel4.setEnabled(false);

                    jLabel5.setFont(new java.awt.Font("Agency FB", 0, 72)); // NOI18N
                    jLabel5.setText(":");
                    jLabel5.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
                    jLabel5.setEnabled(false);

                    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
                    jPanel1.setLayout(jPanel1Layout);
                    jPanel1Layout.setHorizontalGroup(
                        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(hour)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jLabel4)
                            .addGap(9, 9, 9)
                            .addComponent(minute)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(seconds)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    );
                    jPanel1Layout.setVerticalGroup(
                        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(seconds, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                        .addComponent(hour, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                        .addComponent(minute, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                                    .addContainerGap())
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(0, 17, Short.MAX_VALUE))))
                    );

                    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                    getContentPane().setLayout(layout);
                    layout.setHorizontalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(32, 32, 32)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(36, Short.MAX_VALUE))
                    );
                    layout.setVerticalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(66, 66, 66))
                    );

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

                /**
                 * @param args the command line arguments
                 */
                public void ran() {
                    /*
                     * Set the Nimbus look and feel
                     */
                    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
                    /*
                     * If Nimbus (introduced in Java SE 6) is not available, stay with the
                     * default look and feel. For details see
                     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
                     */
                    try {
                        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                            if ("Nimbus".equals(info.getName())) {
                                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                                break;
                            }
                        }
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
                        java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                    }
                    //</editor-fold>

                    /*
                     * Create and display the form
                     */

                    java.awt.EventQueue.invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            new Main().setVisible(true);
                        }
                    }); 
                }
                 String currSec;
                 String currMin;
                 String currHour;

                 public void changeTime()
                    { 
                        function olu = new function();
                        currSec = String.valueOf(olu.second());
                        currMin = String.valueOf(olu.minute());
                        currHour = String.valueOf(olu.hour());
                        System.out.println(currHour+":"+currMin+":"+currSec);
                        hour.setText(currHour);
                    }


                // Variables declaration - do not modify
                public javax.swing.JLabel hour;
                private javax.swing.JLabel jLabel4;
                private javax.swing.JLabel jLabel5;
                private javax.swing.JPanel jPanel1;
                private javax.swing.JLabel minute;
                public javax.swing.JLabel seconds;
                // End of variables declaration
            }
        ******* `Inline Code Example Here` *

function class

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import java.util.*;
import java.util.Timer;
import javax.swing.event.*;

/**
 *
 * @author Adetola-Ralph
 */
public class function
{

    /**
     * @param args the command line arguments
     */

       public long currentTimeMillis;
       public long x;
       public long seconds;
       public long minutes;
       public long hours;
       public long days;


    public function()
    {
        currentTimeMillis = System.currentTimeMillis();
        x = currentTimeMillis / 1000;
        seconds = x % 60;
        x /= 60;
        minutes = x % 60;
        x /= 60;
        hours = x % 24;
        x /= 24;
        days = x;
    }

    public long second()
    {
        return seconds;
    }

    public  long minute()
    {
        return minutes;
    }

    public  long hour()
    {
        return hours;
    }
}

The class that is supposed to run

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clock;
import Interface.Main;

/**
 *
 * @author Adetola-Ralph
 */
public class Clock
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
//         TODO code application logic here
        Main clock = new Main();
        clock.ran();

        try {
        while (true) {
            clock.changeTime();
            Thread.sleep(1000);   
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    }
}

Recommended Answers

All 2 Replies

I am having troubles updating the interface
Please explain what your problem is.

Try getting rid of the generated GUI and creating the GUI manually.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.