Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8648951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:26:18+00:00 2026-06-12T13:26:18+00:00

I want to create a system that has a time in/time out feature. I

  • 0

I want to create a system that has a time in/time out feature.

I tried this code as a trial before including the the feature to my system:

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Time extends JFrame implements ActionListener {

     Date now = new Date();
     private JLabel time;
     private JButton getTime;
     private SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss");

public Time()
{

  setLayout(null);
  setSize(500,300);


  JLabel time = new JLabel("00:00:00");
  time.setSize(100,100);
  time.setLocation(40,40);

  JButton getTime = new JButton("GET TIME");
  getTime.addActionListener(this);
  getTime.setSize(90,30);
  getTime.setLocation(90,70);

  Container pane = getContentPane();

  pane.add(time);
  pane.add(getTime);

  setVisible(true);

}

public void actionPerformed(ActionEvent e)
{
    if (e.getActionCommand() == "GET TIME")
    {
        JOptionPane.showMessageDialog(null, "Time "+dateFormatter.format(now),       
"Time.",JOptionPane.INFORMATION_MESSAGE);
}
}

public static void main(String[] args) {
  new Time();
}
}

It gets the current time, but it still gives the same time when I click again the button. It only changes when I close the UI.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:26:19+00:00Added an answer on June 12, 2026 at 1:26 pm

    Canned answer:

    Don’t compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you’re interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that’s what matters here. So instead of

    if (fu == "bar") {
      // do something
    }
    

    do,

    if ("bar".equals(fu)) {
      // do something
    }
    

    or,

    if ("bar".equalsIgnoreCase(fu)) {
      // do something
    }
    

    Uncanned answer:

    So in other words, change this:

    if (e.getActionCommand() == "GET TIME")
    

    to this:

    if ("GET TIME".equals(e.getActionCommand())
    

    or better still, use String constants.

    Also, you’ll want to avoid using null layout in your Swing GUI’s as you’ll find out that that is the hard way to lay things out. Much easier and more robust is to use layout managers.

    Edit
    You need to get the time from within your actionPerformed method. In other words, now should be created in the actionPerformed method before calling the JOptionPane.

    public void actionPerformed(ActionEvent e)
    {
        if (e.getActionCommand() == "GET TIME")
        {
            now = new Date();
            JOptionPane.showMessageDialog(null, "Time "+dateFormatter.format(now),       
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a logger which logs all my messages sent to System.out
i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
I want to create a PNG file from a System.Drawing.Bitmap with the colours defined
Can I create a junit environment without file system and network access? I want
I am trying to create a conversations based messaging system. I want to group
I want create wordpress website into which I want create user management... That means
i want create a custom json data from the mssql 2008 results so that
Background This is only my second PyQt4 project. Developing a Windows app that has
I want to create an annotation that restricts a developer from specifying null as
I have legacy application code written for a real-time operating system (RTOS). Most of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.