Search code examples
javaswingtimerjlabel

Display time in GUI


I am working on a personal project very simple but this is part of a bigger project. Any help is appreciated. This is my code the and the output is wrong, when I run it, it displays one big window and one separate small window displaying the time. The right way should be displaying time in the big frame in a JLabel.

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

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class DateShowAPII extends javax.swing.JFrame {

/**
 * Creates new form DateShowAPI
 */
public DateShowAPII() {
    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() {

    JLabel = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel2.setText("Time");

    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(104, 104, 104)
            .addComponent(jLabel2)
            .addGap(28, 28, 28)
            .addComponent(JLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(179, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,    layout.createSequentialGroup()
            .addContainerGap(139, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(JLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel2))
            .addGap(132, 132, 132))
    );

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

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {

    JFrame frame = new JFrame();

    String date = new SimpleDateFormat("hh:mm:ss").format(new Date());
    JLabel l = new JLabel(date);

    frame.getContentPane().add(l);

    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

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

        public void run() {
            new DateShowAPII().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JLabel JLabel;
private javax.swing.JLabel jLabel2;
// End of variables declaration
}

Solution

  • DateShowAPII is a frame (that's the big one) and in main, you create another one (and pack it) -- that's the small one with the time.

    Instead, don't create another frame and add the JLabel to the new DateShowAPII you created.