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

Error in Thread

Exception in thread "Thread-2" java.lang.NullPointerException
        at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2026)
        at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:1996)
        at java.util.Calendar.setTimeInMillis(Calendar.java:1104)
        at java.util.GregorianCalendar.<init>(GregorianCalendar.java:576)
        at java.util.GregorianCalendar.<init>(GregorianCalendar.java:552)
        at guidemo.DispTime.run(DispTime.java:43)
        at java.lang.Thread.run(Thread.java:619)






/*
 * NewDispTime.java
 *
 * Created on July 7, 2007, 9:04 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package guidemo;

/**
 *
 * @author HP_Owner
 */
import javax.swing.*;
import java.awt.Frame;
import java.awt.Container;
import java.util.Formatter;
import java.util.*;


      public class DispTime extends JFrame implements Runnable
{
   
   JLabel hours;
   JLabel minutes;
   JLabel seconds;
   JLabel amPm;
   Thread t;
   int i;
 
   DispTime()
   {

      t = new Thread(this);
      t.start();
   }


   public void run() {
        while(true) {
            clockCalendar = new GregorianCalendar(currentTimeZone);
            if(hourFormat == 24)
                currentHour = clockCalendar.get(Calendar.HOUR_OF_DAY);
            else
            currentHour = clockCalendar.get(Calendar.HOUR);
            currentMinute = clockCalendar.get(Calendar.MINUTE);
            currentSecond = clockCalendar.get(Calendar.SECOND);
          currentAmPm = clockCalendar.get(Calendar.AM_PM);
             
              hours.setText("" + currentHour);
            minutes.setText(""  + currentMinute );
            seconds.setText(""  + currentSecond);
              
              if(Calendar.AM_PM == Calendar.AM) {
                 amPm.setText("am");
            }
            else{
               amPm.setText("pm");
            }

            try {
                Thread.sleep(1000L);
            } catch(InterruptedException interruptedexception) { }
        }
    }


   public static void main(String[] args)
   {
     
      final DispTime PRN = new DispTime();
      final GuiPanel gp = new GuiPanel();
      PRN.setTimeDispListener(gp.getTimeDisplay());

      java.awt.EventQueue.invokeLater(new Runnable()
      {
         public void run()
         {
            gp.setVisible(true);
         }
      });

   }


   /**
    * @param timeDisplay
    */
    void setTimeDispListener(JLabel timeDisplay)
   {
      this.listenerTF = timeDisplay;
   }
   
   private JLabel listenerTF;
   
private TimeZone currentTimeZone;
private int currentHour;
private int currentMinute;
private int currentSecond;
private int currentAmPm;
private Thread runner;
private GregorianCalendar clockCalendar;
private int hourFormat;
   
   
}
0
Drop_of_Rain
Asked:
Drop_of_Rain
  • 17
  • 14
  • 6
6 Solutions
 
ksivananthCommented:
thats because you haven't initialized currentTimeZone var
0
 
ksivananthCommented:
also initialize the lables, hours, minutes, seconds and amPm
0
 
Drop_of_RainAuthor Commented:
How would you suggest i do this?
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.

 
ksivananthCommented:
I can suggest for the timezone obj but for the lables, I couldn't as I don't see them being used in any screen!

while(true) {
              currentTimeZone = TimeZone.getDefault() ;
            clockCalendar = new GregorianCalendar(currentTimeZone);
0
 
ksivananthCommented:
can you post the code of GuiPanel class too?
0
 
Drop_of_RainAuthor Commented:
Yes i can, I want to have a setup window that the user will click radioButtons to make choices like timeZone  and other info that will be user by that user. I have changed it a lot and use DispTime as the main for the GUI. Everything is up for change now.


*
 * GuiPanel.java
 *
 * Created on July 4, 2007, 9:29 PM
 */

package guidemo;


import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JLabel;


/**
 *
 * @author  HP_Owner
 */
public class GuiPanel extends javax.swing.JFrame {
enum Mode {
Alarm, Timer, Clock, Date, Audio
};

   private java.util.List<Mode> modes;
   private int modeIndex;
   private Mode currentMode;
   boolean alarmStarted = false;
   public String alarmTime;

/** Creates new form GuiPanel */
public GuiPanel() {
initComponents();
//initModes();
}
   
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                         
    private void initComponents() {
        timeDisplay3 = new javax.swing.JLabel();
        timeDisplay = new javax.swing.JPanel();
        monthDisplay = new javax.swing.JLabel();
        amPmDisplay = new javax.swing.JLabel();
        hoursDisplay = new javax.swing.JLabel();
        minutesDisplay = new javax.swing.JLabel();
        secondsDisplay = new javax.swing.JLabel();
        timeLabel = new javax.swing.JLabel();
        dateLabel = new javax.swing.JLabel();
        dayDisplay = new javax.swing.JLabel();
        yearDisplay = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        modeButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        hourButton = new javax.swing.JButton();
        minutesButton = new javax.swing.JButton();
        recordButton = new javax.swing.JButton();
        playButton = new javax.swing.JButton();
        upButton = new javax.swing.JButton();
        downButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        setButton = new javax.swing.JButton();
        stopButton = new javax.swing.JButton();
        modeDisplay = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        panelNameLabel = new javax.swing.JLabel();

        timeDisplay3.setFont(new java.awt.Font("Tahoma", 0, 30));
        timeDisplay3.setForeground(new java.awt.Color(204, 0, 0));
        timeDisplay3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        timeDisplay3.setText("00");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        timeDisplay.setBackground(new java.awt.Color(255, 255, 255));
        monthDisplay.setFont(new java.awt.Font("Tahoma", 0, 30));
        monthDisplay.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        monthDisplay.setText("00");

        amPmDisplay.setFont(new java.awt.Font("Tahoma", 0, 18));
        amPmDisplay.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        amPmDisplay.setText("am");

        hoursDisplay.setFont(new java.awt.Font("Tahoma", 0, 30));
        hoursDisplay.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        hoursDisplay.setText("00");

        minutesDisplay.setFont(new java.awt.Font("Tahoma", 0, 30));
        minutesDisplay.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        minutesDisplay.setText("00");

        secondsDisplay.setFont(new java.awt.Font("Tahoma", 0, 30));
        secondsDisplay.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        secondsDisplay.setText("00");

        timeLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
        timeLabel.setForeground(new java.awt.Color(0, 153, 153));
        timeLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        timeLabel.setText("Time");

        dateLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
        dateLabel.setForeground(new java.awt.Color(0, 153, 153));
        dateLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        dateLabel.setText("Date");

        dayDisplay.setFont(new java.awt.Font("Tahoma", 0, 30));
        dayDisplay.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        dayDisplay.setText("00");

        yearDisplay.setFont(new java.awt.Font("Tahoma", 0, 30));
        yearDisplay.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        yearDisplay.setText("0000");

        javax.swing.GroupLayout timeDisplayLayout = new javax.swing.GroupLayout(timeDisplay);
        timeDisplay.setLayout(timeDisplayLayout);
        timeDisplayLayout.setHorizontalGroup(
            timeDisplayLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, timeDisplayLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(dateLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(monthDisplay)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dayDisplay)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(yearDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addComponent(timeLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(hoursDisplay)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(minutesDisplay)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(secondsDisplay)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(amPmDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41))
        );
        timeDisplayLayout.setVerticalGroup(
            timeDisplayLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(timeDisplayLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(timeDisplayLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(timeDisplayLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(monthDisplay)
                        .addComponent(dayDisplay)
                        .addComponent(yearDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(dateLabel))
                    .addGroup(timeDisplayLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(hoursDisplay)
                        .addComponent(minutesDisplay)
                        .addComponent(secondsDisplay)
                        .addComponent(timeLabel)
                        .addComponent(amPmDisplay)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(0, 0, 0));
        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 459, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        modeButton.setBackground(new java.awt.Color(0, 0, 0));
        modeButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        modeButton.setForeground(new java.awt.Color(0, 153, 153));
        modeButton.setText("Mode");
        modeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                event(evt);
            }
        });

        okButton.setBackground(new java.awt.Color(0, 0, 0));
        okButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        okButton.setForeground(new java.awt.Color(0, 153, 153));
        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                event(evt);
            }
        });

        hourButton.setBackground(new java.awt.Color(0, 0, 0));
        hourButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        hourButton.setForeground(new java.awt.Color(0, 153, 153));
        hourButton.setText("Hour");
        hourButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                event(evt);
            }
        });

        minutesButton.setBackground(new java.awt.Color(0, 0, 0));
        minutesButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        minutesButton.setForeground(new java.awt.Color(0, 153, 153));
        minutesButton.setText("Minutes");
        minutesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                event(evt);
            }
        });

        recordButton.setBackground(new java.awt.Color(0, 0, 0));
        recordButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        recordButton.setForeground(new java.awt.Color(0, 153, 153));
        recordButton.setText("Record");
        recordButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                event(evt);
            }
        });

        playButton.setBackground(new java.awt.Color(0, 0, 0));
        playButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        playButton.setForeground(new java.awt.Color(0, 153, 153));
        playButton.setText("Play");
        playButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                event(evt);
            }
        });

        upButton.setBackground(new java.awt.Color(0, 0, 0));
        upButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        upButton.setForeground(new java.awt.Color(0, 153, 153));
        upButton.setText("Up");
        upButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                event(evt);
            }
        });

        downButton.setBackground(new java.awt.Color(0, 0, 0));
        downButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        downButton.setForeground(new java.awt.Color(0, 153, 153));
        downButton.setText("Down");
        downButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                event(evt);
            }
        });

        jButton1.setBackground(new java.awt.Color(0, 0, 0));
        jButton1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jButton1.setForeground(new java.awt.Color(255, 255, 255));
        jButton1.setText("Loop");

        setButton.setBackground(new java.awt.Color(0, 0, 0));
        setButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        setButton.setForeground(new java.awt.Color(0, 153, 153));
        setButton.setText("Set");

        stopButton.setBackground(new java.awt.Color(0, 0, 0));
        stopButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        stopButton.setForeground(new java.awt.Color(0, 153, 153));
        stopButton.setText("Stop");
        stopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                event(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(180, 180, 180)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(259, 259, 259))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(modeButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(hourButton))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(minutesButton)))
                .addGap(6, 6, 6)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(stopButton, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                    .addComponent(setButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(recordButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(upButton))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(playButton, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(downButton)))
                .addContainerGap())
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {downButton, hourButton, minutesButton, modeButton, okButton, playButton, recordButton, setButton, stopButton, upButton});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hourButton, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(modeButton)
                    .addComponent(recordButton)
                    .addComponent(upButton)
                    .addComponent(setButton))
                .addGap(17, 17, 17)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(minutesButton)
                    .addComponent(stopButton)
                    .addComponent(playButton)
                    .addComponent(downButton)
                    .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(41, 41, 41)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {downButton, hourButton, minutesButton, modeButton, okButton, playButton, recordButton, setButton, stopButton, upButton});

        modeDisplay.setBackground(new java.awt.Color(0, 0, 0));
        modeDisplay.setEditable(false);
        modeDisplay.setFont(new java.awt.Font("Tahoma", 1, 16));
        modeDisplay.setForeground(new java.awt.Color(255, 255, 255));
        modeDisplay.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jPanel4.setBackground(new java.awt.Color(0, 0, 0));
        panelNameLabel.setFont(new java.awt.Font("Tahoma", 0, 30));
        panelNameLabel.setForeground(new java.awt.Color(0, 153, 153));
        panelNameLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        panelNameLabel.setText("Audio Reminder Panel");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(78, 78, 78)
                .addComponent(panelNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(81, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(modeDisplay, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
                    .addComponent(timeDisplay, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(timeDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(modeDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        pack();
    }// </editor-fold>                        

   
   
    private void event(java.awt.event.ActionEvent evt) {                      
// TODO add your handling code here:
    }                      
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GuiPanel().setVisible(true);
            }
        });
    }
   
    // Variables declaration - do not modify                    
    private javax.swing.JLabel amPmDisplay;
    private javax.swing.JLabel dateLabel;
    private javax.swing.JLabel dayDisplay;
    private javax.swing.JButton downButton;
    private javax.swing.JButton hourButton;
    private javax.swing.JLabel hoursDisplay;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JButton minutesButton;
    private javax.swing.JLabel minutesDisplay;
    private javax.swing.JButton modeButton;
    private javax.swing.JTextField modeDisplay;
    private javax.swing.JLabel monthDisplay;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel panelNameLabel;
    private javax.swing.JButton playButton;
    private javax.swing.JButton recordButton;
    private javax.swing.JLabel secondsDisplay;
    private javax.swing.JButton setButton;
    private javax.swing.JButton stopButton;
    private javax.swing.JPanel timeDisplay;
    private javax.swing.JLabel timeDisplay3;
    private javax.swing.JLabel timeLabel;
    private javax.swing.JButton upButton;
    private javax.swing.JLabel yearDisplay;
    // End of variables declaration                  
   public javax.swing.JLabel getTimeDisplay()
   {
      JLabel timeDisplay = new JLabel(hoursDisplay.getText() + ":" + minutesDisplay.getText() + ":" + secondsDisplay.getText() + ":"  + amPmDisplay.getText());
      return timeDisplay;
   }

   /**
    * @param prn
    */
   public void setTimeDispProvider(DispTime prn)
   {
      dispProv = prn;
   }

   DispTime dispProv;
}
0
 
CEHJCommented:
You don't need the TimeZone actually, since you only use the default anyway
0
 
Drop_of_RainAuthor Commented:
I got some error here can you look it over as well?

C:\Documents and Settings\HP_Owner\GuiDemo\src\guidemo\DispTime.java:43: incompatible types
found   : java.util.TimeZone
required: java.util.GregorianCalendar
            currentTimeZone = TimeZone.getDefault() ;
C:\Documents and Settings\HP_Owner\GuiDemo\src\guidemo\DispTime.java:44: cannot find symbol
symbol  : constructor GregorianCalendar(java.util.GregorianCalendar)
location: class java.util.GregorianCalendar
            clockCalendar = new GregorianCalendar(currentTimeZone);
2 errors


import javax.swing.*;
import java.awt.Frame;
import java.awt.Container;
import java.util.Formatter;
import java.util.*;
import java.util.GregorianCalendar;



      public class DispTime extends JFrame implements Runnable
{
   
   JLabel hoursDisplay;
   JLabel minutesDisplay;
   JLabel secondsDisplay;
   JLabel amPmDisplay;
   Thread t;
   int i;
 
   DispTime()
   {

      t = new Thread(this);
      t.start();
   }


   public void run() {
        while(true) {
            currentTimeZone = TimeZone.getDefault() ;
            clockCalendar = new GregorianCalendar(currentTimeZone);
            if(hourFormat == 24)
                currentHour = clockCalendar.get(Calendar.HOUR_OF_DAY);
            else
            currentHour = clockCalendar.get(Calendar.HOUR);
            currentMinute = clockCalendar.get(Calendar.MINUTE);
            currentSecond = clockCalendar.get(Calendar.SECOND);
          currentAmPm = clockCalendar.get(Calendar.AM_PM);
             
              hoursDisplay.setText("" + currentHour);
            minutesDisplay.setText(""  + currentMinute );
            secondsDisplay.setText(""  + currentSecond);
              
              if(Calendar.AM_PM == Calendar.AM) {
                 amPmDisplay.setText("am");
            }
            else{
               amPmDisplay.setText("pm");
            }

            try {
                Thread.sleep(1000L);
            } catch(InterruptedException interruptedexception) { }
        }
    }


   public static void main(String[] args)
   {
     
      final DispTime PRN = new DispTime();
      final GuiPanel gp = new GuiPanel();
      PRN.setTimeDispListener(gp.getTimeDisplay());

      java.awt.EventQueue.invokeLater(new Runnable()
      {
         public void run()
         {
            gp.setVisible(true);
         }
      });

   }


   /**
    * @param timeDisplay
    */
    void setTimeDispListener(JLabel timeDisplay)
   {
      this.listenerTF = timeDisplay;
   }
   
   private JLabel listenerTF;
   
//private TimeZone currentTimeZone;
private int currentHour;
private int currentMinute;
private int currentSecond;
private int currentAmPm;
private Thread runner;
private GregorianCalendar clockCalendar;
private GregorianCalendar currentTimeZone;
private int hourFormat;
   
   
}
0
 
CEHJCommented:
Get rid of

>>private GregorianCalendar currentTimeZone;

and make

>>clockCalendar = new GregorianCalendar(currentTimeZone);

clockCalendar = new GregorianCalendar();
0
 
Drop_of_RainAuthor Commented:
>>clockCalendar = new GregorianCalendar(currentTimeZone);
I need to change this but still had to add this below
private TimeZone currentTimeZone;

it compiled but didn't work with GuiPanel, I got these errors

Exception in thread "Thread-2" java.lang.NullPointerException
        at guidemo.DispTime.run(DispTime.java:55)
        at java.lang.Thread.run(Thread.java:619)
0
 
CEHJCommented:
>>but still had to add this below
private TimeZone currentTimeZone;

I just mentioned - you don't need the TimeZone. Use the code i last posted
0
 
CEHJCommented:
Obviously, you need to get rid of

>>currentTimeZone = TimeZone.getDefault() ;

too
0
 
Drop_of_RainAuthor Commented:
Here is the run in the old class before i went to the 4 labels

   public void run()
   {
      String s;
      while (true)
      {
                  Calendar c = Calendar.getInstance();
                  StringBuilder builder = new StringBuilder();
                  Formatter formatter = new Formatter(builder);
                  formatter.format("%02d:%02d:%02d", c.get(Calendar.HOUR), c.get(Calendar.MINUTE), c.get(Calendar.SECOND));
                  formatter.flush();
                  s = builder.toString();
         try
         {
            t.sleep(1000);
         }
         catch (Exception e)
         {
         }
         time.setText(" " + s);
         listenerTF.setText(time.getText());
      }
   }

Here is the new run for the four labels

public void run() {
        while(true) {
            //currentTimeZone = TimeZone.getDefault() ;
            clockCalendar = new GregorianCalendar();
            if(hourFormat == 24)
                currentHour = clockCalendar.get(Calendar.HOUR_OF_DAY);
            else
            currentHour = clockCalendar.get(Calendar.HOUR);
            currentMinute = clockCalendar.get(Calendar.MINUTE);
            currentSecond = clockCalendar.get(Calendar.SECOND);
          currentAmPm = clockCalendar.get(Calendar.AM_PM);
             
              hoursDisplay.setText("" + currentHour);
            minutesDisplay.setText(""  + currentMinute );
            secondsDisplay.setText(""  + currentSecond);
              
              if(Calendar.AM_PM == Calendar.AM) {
                 amPmDisplay.setText("am");
            }
            else{
               amPmDisplay.setText("pm");
            }

            try {
                Thread.sleep(1000L);
            } catch(InterruptedException interruptedexception) { }
        }
    }
0
 
Drop_of_RainAuthor Commented:
Maybe at this point it would be easier to do this as an inner class in the GUI and then refactor it at a later time. The problem has to do with the displaying of the time in the label i think Here are both classes can you help me put these in one class and get the basics working?

import javax.swing.*;
import java.awt.Frame;
import java.awt.Container;
import java.util.Formatter;
import java.util.*;
import java.util.GregorianCalendar;

      public class DispTime extends JFrame implements Runnable
{
   
   JLabel hoursDisplay;
   JLabel minutesDisplay;
   JLabel secondsDisplay;
   JLabel amPmDisplay;
   Thread t;
   int i;
 
   DispTime()
   {

      t = new Thread(this);
      t.start();
   }


   public void run() {
        while(true) {
            //currentTimeZone = TimeZone.getDefault() ;
            clockCalendar = new GregorianCalendar();
            if(hourFormat == 24)
                currentHour = clockCalendar.get(Calendar.HOUR_OF_DAY);
            else
            currentHour = clockCalendar.get(Calendar.HOUR);
            currentMinute = clockCalendar.get(Calendar.MINUTE);
            currentSecond = clockCalendar.get(Calendar.SECOND);
          currentAmPm = clockCalendar.get(Calendar.AM_PM);
             
              hoursDisplay.setText("" + currentHour);
            minutesDisplay.setText(""  + currentMinute );
            secondsDisplay.setText(""  + currentSecond);
              
              if(Calendar.AM_PM == Calendar.AM) {
                 amPmDisplay.setText("am");
            }
            else{
               amPmDisplay.setText("pm");
            }

            try {
                t.sleep(1000);
            } catch(InterruptedException interruptedexception) { }
        }
    }


   public static void main(String[] args)
   {
     
      final DispTime PRN = new DispTime();
      final GuiPanel gp = new GuiPanel();
      PRN.setTimeDispListener(gp.getTimeDisplay());

      java.awt.EventQueue.invokeLater(new Runnable()
      {
         public void run()
         {
            gp.setVisible(true);
         }
      });

   }


   /**
    * @param timeDisplay
    */
    void setTimeDispListener(JLabel timeDisplay)
   {
     this.listenerTF = timeDisplay;
   }
   
private JLabel listenerTF;
private int currentHour;
private int currentMinute;
private int currentSecond;
private int currentAmPm;
private Thread runner;
private GregorianCalendar clockCalendar;
private int hourFormat;
   
}


And the GuiPanel code is above. Maybe have to keep this simple at this point
0
 
Drop_of_RainAuthor Commented:
I raised the point to 500 thanks for the help.
0
 
ksivananthCommented:
>>You don't need the TimeZone actually, since you only use the default anyway

actually the author wants to specify the timezone based on the user selection, see the below comment from the author!

I think, the author wants a setup screen where the user will change the seeting and that should reflect in the result screen( guipanel! )

>>I want to have a setup window that the user will click radioButtons to make choices like timeZone  and other info that will be user by that user.
>>
0
 
Drop_of_RainAuthor Commented:
I need to get the basics working first. Can you help me up these into 1 class so i can maybe get the basics working for now and then i can add other things as I go. Working with many labels to display time has become more complicated then I realized.
0
 
CEHJCommented:
>>I want to have a setup window that the user will click radioButtons to make choices like timeZone  and other info that will be user by that user.
>>

Not sure why you'd want someone else's time zone, but if there's a good reason, fair enough. If that's the case, change

>>
private GregorianCalendar clockCalendar;
private GregorianCalendar currentTimeZone;
private int hourFormat;
>>

to

private GregorianCalendar clockCalendar;
private TimeZone currentTimeZone;
private int hourFormat;
0
 
ksivananthCommented:
what are you trying to do with the DispTime class?
0
 
Drop_of_RainAuthor Commented:
All it was for was the main and setting the time into the displays
0
 
ksivananthCommented:
or, more specifically, how this DispTime affects the GuiPanel class?
0
 
ksivananthCommented:
>>the main and setting the time into the displays

yes, it has a main method but where it displays the time? you'll have to init the labels and then add them to a container and then make the fame visible. In that case why do you need GuiPanel?
0
 
Drop_of_RainAuthor Commented:
It has been changed as you can see. In the run it got the system time and  set the text with that info. It had the main in it for the GuiPanel as well. So it was the thread for the clock to run as well. Thats it
0
 
Drop_of_RainAuthor Commented:
The GuiPanel has its own main now.
0
 
ksivananthCommented:
do you want to display the guipanel and the DispTime in single screen?
0
 
Drop_of_RainAuthor Commented:
Go to this question and go to the sollution. The code for both classes are there and you can run the app and see for yourself.

http://www.experts-exchange.com/Programming/Languages/Java/J2SE/Q_22678353.html
0
 
ksivananthCommented:
so what are you trying different from that solution?
0
 
Drop_of_RainAuthor Commented:
That was when there was only 1 label that displayed the text, not there is 4 1for the hours, minutes, seconds, amPm.  and 3 for the date. Therefore the get and set has changed. as well. I couldn't even fine any examples for something like this.
0
 
ksivananthCommented:
the below code works for disptime atleast, I don't have JDK1.6 now so couldn't test guipanel!

import javax.swing.*;

import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Container;
import java.util.*;


      public class DispTime extends JFrame implements Runnable
{
   
   JLabel hours;
   JLabel minutes;
   JLabel seconds;
   JLabel amPm;
   Thread t;
   int i;
 
   DispTime()
   {
         hours = new JLabel();
         minutes = new JLabel();
         seconds = new JLabel();
         amPm = new JLabel();
         JPanel pnlTime = new JPanel() ;
         pnlTime.add( hours ) ;
         pnlTime.add( minutes ) ;
         pnlTime.add( seconds ) ;
         pnlTime.add( amPm ) ;
         this.getContentPane().add( pnlTime ) ;
         this.setSize( 100, 50 ) ;
            t = new Thread(this);
         try{
               EventQueue.invokeAndWait( new Runnable(){
                     public void run(){
                              t.start();
                     }
               }) ;
         }catch( Exception e ){
               
         }
   }


   public void run() {
        while(true) {
              currentTimeZone = TimeZone.getDefault() ;
            clockCalendar = new GregorianCalendar(currentTimeZone);
            if(hourFormat == 24)
                currentHour = clockCalendar.get(Calendar.HOUR_OF_DAY);
            else
            currentHour = clockCalendar.get(Calendar.HOUR);
            currentMinute = clockCalendar.get(Calendar.MINUTE);
            currentSecond = clockCalendar.get(Calendar.SECOND);
          currentAmPm = clockCalendar.get(Calendar.AM_PM);
             
              hours.setText("" + currentHour);
            minutes.setText(""  + currentMinute );
            seconds.setText(""  + currentSecond);
             
              if(Calendar.AM_PM == Calendar.AM) {
                 amPm.setText("am");
            }
            else{
               amPm.setText("pm");
            }

            try {
                Thread.sleep(1000L);
            } catch(InterruptedException interruptedexception) { }
        }
    }


   public static void main(String[] args)
   {
     
      final DispTime PRN = new DispTime();
      PRN.setVisible( true ) ;
/*      final GuiPanel gp = new GuiPanel();
      PRN.setTimeDispListener(gp.getTimeDisplay());

      java.awt.EventQueue.invokeLater(new Runnable()
      {
         public void run()
         {
            gp.setVisible(true);
         }
      });
*/
   }


   /**
    * @param timeDisplay
    */
    void setTimeDispListener(JLabel timeDisplay)
   {
      this.listenerTF = timeDisplay;
   }
   
   private JLabel listenerTF;
   
private TimeZone currentTimeZone;
private int currentHour;
private int currentMinute;
private int currentSecond;
private int currentAmPm;
private Thread runner;
private GregorianCalendar clockCalendar;
private int hourFormat;
   
   
}
0
 
Drop_of_RainAuthor Commented:
It says it is pm when it is am
0
 
ksivananthCommented:
>>if(Calendar.AM_PM == Calendar.AM)

modify it to

if(currentAmPm== Calendar.AM)
0
 
Drop_of_RainAuthor Commented:
Ok thats working. Now i want to ask if the formating can be set so that there are 2 digits from 0-9 in all fields like a digital clock?
0
 
ksivananthCommented:
this code does that!

import javax.swing.*;

import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Container;
import java.text.DecimalFormat;
import java.util.*;


      public class DispTime extends JFrame implements Runnable
{
   
   JLabel hours;
   JLabel minutes;
   JLabel seconds;
   JLabel amPm;
   Thread t;
   int i;
   private DecimalFormat formatter = new DecimalFormat( "00" ) ;
 
   DispTime()
   {
         hours = new JLabel();
         minutes = new JLabel();
         seconds = new JLabel();
         amPm = new JLabel();
         JPanel pnlTime = new JPanel() ;
         pnlTime.add( hours ) ;
         pnlTime.add( minutes ) ;
         pnlTime.add( seconds ) ;
         pnlTime.add( amPm ) ;
         this.getContentPane().add( pnlTime ) ;
         this.setSize( 100, 50 ) ;
            t = new Thread(this);
         try{
               EventQueue.invokeAndWait( new Runnable(){
                     public void run(){
                              t.start();
                     }
               }) ;
         }catch( Exception e ){
               
         }
   }


   public void run() {
        while(true) {
              currentTimeZone = TimeZone.getDefault() ;
            clockCalendar = new GregorianCalendar(currentTimeZone);
            if(hourFormat == 24)
                currentHour = clockCalendar.get(Calendar.HOUR_OF_DAY);
            else
            currentHour = clockCalendar.get(Calendar.HOUR);
            currentMinute = clockCalendar.get(Calendar.MINUTE);
            currentSecond = clockCalendar.get(Calendar.SECOND);
          currentAmPm = clockCalendar.get(Calendar.AM_PM);
             
              hours.setText("" + formatter.format( currentHour ) );
            minutes.setText(""  + formatter.format( currentMinute ) );
            seconds.setText(""  + formatter.format( currentSecond ) );
             
              if(currentAmPm == Calendar.AM) {
                 amPm.setText("am");
            }
            else{
               amPm.setText("pm");
            }

            try {
                Thread.sleep(1000L);
            } catch(InterruptedException interruptedexception) { }
        }
    }


   public static void main(String[] args)
   {
     
      final DispTime PRN = new DispTime();
      PRN.setVisible( true ) ;
/*      final GuiPanel gp = new GuiPanel();
      PRN.setTimeDispListener(gp.getTimeDisplay());

      java.awt.EventQueue.invokeLater(new Runnable()
      {
         public void run()
         {
            gp.setVisible(true);
         }
      });
*/
   }


   /**
    * @param timeDisplay
    */
    void setTimeDispListener(JLabel timeDisplay)
   {
      this.listenerTF = timeDisplay;
   }
   
   private JLabel listenerTF;
   
private TimeZone currentTimeZone;
private int currentHour;
private int currentMinute;
private int currentSecond;
private int currentAmPm;
private Thread runner;
private GregorianCalendar clockCalendar;
private int hourFormat;
   
   
}
0
 
Drop_of_RainAuthor Commented:
That is it. Thanks for the help. I will post another question to see if we can get this displayed into the GuiPanel. I think I will use the main in the GuiPanel and make everything else just classes. So the get and set methods for timeDisplay should be added to this class as well. That way all time is handled my this class, don't you think?
0
 
ksivananthCommented:
>>That is it. Thanks for the help.

:)

>>I will post another question to see if we can get this displayed into the GuiPanel. I think I will use the main in the GuiPanel and make everything else just classes. So the get and set methods for timeDisplay should be added to this class as well. That way all time is handled my this class, don't you think?
>>

I don't how this and guipanel are related and what you are trying to do!
0
 
Drop_of_RainAuthor Commented:
This will be the time class that will handle all the time stuff, geting, setting ,updating  and adjusting  all will convert the time to milliseconds for the timer. It will just pass the refference to the GUI and the gui will have the display in it.
0
 
CEHJCommented:
:-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 17
  • 14
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now