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 7676903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:18:15+00:00 2026-05-31T17:18:15+00:00

I am trying to create a simple GUI form which has only 2 elements

  • 0

I am trying to create a simple GUI form which has only 2 elements – a simple label and a button. The text displayed on button is ‘Start’. The label is displaying 0 by default.

When I click Start button following actions shall take place:

  1. Counter shall start incrementing by 1 from 0 at every 1 second.
  2. Text displayed on the Start button shall change to Stop.
  3. When again I click on the same button (now showing caption as Stop), increment shall stop.
  4. Text on the button shall change to Start. And so on…

I am developing my application in Netbeans.

As shown in the above diagram, there are 2 .java files

Contents of AGC.java are:

public class AGC extends javax.swing.JFrame 
{
    public AGC()
    {    
        initComponents();
    }

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

    private javax.swing.JButton btnStartStop;  // name of start stop button
    private javax.swing.JLabel lblCounter;   // name of the label

}

Contents of Main.java are:

public class Main 
{
    public static int count = 0;
    public static boolean started = false;
}

I want to implement following logic:

private void btnStartStopMouseClicked(java.awt.event.MouseEvent evt) 
{
    if (Main.stared == true)
    {
        // logic to start counting
    }
    else
    {
        // logic to stop counting
    }
}

My problem is this:

  1. How to update lblCounter at every 1 second?
  2. What logic shall I implement to start the timer of 1 second and how to access lblCounter in that method ?

Kindly help. A working code would be very highly appreciated. Thanks in advance.

Jay

  • 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-05-31T17:18:16+00:00Added an answer on May 31, 2026 at 5:18 pm

    Simply use a javax.swing.Timer, and make one ActionListener, to do this thing for you . Give me ten mins for a working code example 🙂

    Here is a sample program for further help :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class UpdateWithTimer extends JFrame
    {
        private Timer timer;
        private JButton startStopButton;
        private JLabel changingLabel;
        private int counter = 0;
        private boolean flag = false;
        private ActionListener timerAction = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                counter++;
                changingLabel.setText("" + counter);
            }
        };
    
        private ActionListener buttonAction = new ActionListener()  
        {
            public void actionPerformed(ActionEvent ae)
            {
                if (!flag)
                {
                    startStopButton.setText("STOP TIMER");
                    timer.start();
                    flag = true;
                }
                else if (flag)
                {
                    startStopButton.setText("START TIMER");
                    timer.stop();
                    flag = false;
                }
            }
        };
    
        private void createAndDisplayGUI()
        {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationByPlatform(true);
    
            JPanel contentPane = new JPanel();
            changingLabel = new JLabel("" + counter);
            contentPane.add(changingLabel);
    
            startStopButton = new JButton("START TIMER");
            startStopButton.addActionListener(buttonAction);
    
            add(contentPane, BorderLayout.CENTER);
            add(startStopButton, BorderLayout.PAGE_END);
    
            timer = new Timer(1000, timerAction);
    
            setSize(300, 300);
            setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new UpdateWithTimer().createAndDisplayGUI();
                }
            });
        }
    }
    

    If you want the counter to again revert back to 0, on Stopping the Timer, simply add

    else if (flag)
    {
        startStopButton.setText("START TIMER");
        timer.stop();
        flag = false;
        counter = 0;
        changingLabel.setText("" + counter);
    }
    

    this part to the buttonAction‘s actionPerformed(...) method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a simple GUI with table containing x and y
I'm trying to create a simple GUI application (so far) in Qt with C++
I am trying to create this simple GUI where the number of clicks is
I am new to Qt, and I am trying to create a simple GUI
experts. I'm trying to create a simple file downloader (with GUI). I'm pretty new
Im trying to create a simple callback using blocks. I have a MainViewController which
I am trying to create a simple GUI. I have a menu bar that
I'm new to Python and I'm trying to create a simple GUI using Tkinter.
I am trying to create a really simple GUI using GtkBuilder and glade. To
I'm trying to create a (very) simple Win32 GUI program, but for some reason

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.