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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:29:44+00:00 2026-05-23T11:29:44+00:00

Very simple as am very new to java Eclipse. Using Eclipse IDE for Java

  • 0

Very simple as am very new to java Eclipse. Using Eclipse IDE for Java Developers
Version: Helios Service Release 2. Created a button and Label. Using this button to increment a the value by 1 at time but nothing is happening. Not sure why its not. Please can someone have a look at. I don’t think I am too far off then again I maybr. Thanks in advance…

 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import javax.swing.*;

   public class First 
   {
       //int counter = 0; //  tried it here -> unsuccesfull
       // static int counter = 0; //  tried it here -> unsuccesfull

      public static void main (String [] args){
      JFrame frame = new JFrame("att");
      frame.setVisible(true);
  frame.setSize(500,500);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      JPanel panel = new JPanel();
  frame.add(panel);

      JButton button = new  JButton("+");
      frame.add(panel);
  panel.add(button);

      JLabel label = new JLabel("1");
  frame.add(panel);
  panel.add(label);

      // int counter = 0; // tried it here -> unsuccesfull

      // final int counter = 0; // tried it here -> unsuccesfull. Getting error
      // "The final local variable counter cannot be assigned, since it is 
      // defined in an enclosing type" *

      button1.addActionListener(new ActionListener() 
      {
        public void actionPerformed(ActionEvent arg0) 
    {
          label1.setVisible(true);
      int counter = 0;
      counter = counter + 1 ; // *
    }
 }
);

Modified the code and placed the int variable after class declartion and jut after button1.addActionListener(new ActionListener() method header and no success

  • 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-23T11:29:45+00:00Added an answer on May 23, 2026 at 11:29 am

    You are setting to 0 the counter everytime the button is been clicked.

    If what you want is to keep track of the amount of times the button has been clicked then you want to declare the variable out of the listener. Otherwise, everytime you click on the button counter will always be declared and set to 0. Hence, it will not increment in the way you expect.

    You should try something like this:

      static int counter = 0; 
    
      public static void main (String [] args){
      JFrame frame = new JFrame("att");
      frame.setVisible(true);
      frame.setSize(500,500);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
      JPanel panel = new JPanel();
      frame.add(panel);
    
      JButton button1 = new  JButton("+");
      frame.add(panel);
      panel.add(button1);
    
      final JLabel label1 = new JLabel("0");
      frame.add(panel);
      panel.add(label1);
    
      button1.addActionListener(new ActionListener() 
     {
      public void actionPerformed(ActionEvent arg0) 
     {
          label1.setVisible(true);         
          counter += 1 ; // *
          label1.setText(String.valueOf(counter));
     }
     }
     );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I am building a very simple eclipse plugin for creating new java projects.
I generated a very simple runnable jar file using Eclipse's Export-->Java-->Runnable Jar File function.
I have built a very simple blog application using Ruby on Rails. New to
I'm very new to flash. I'm trying to show a simple button for my
This is my first Java application I am creating (using Eclipse IDE) and the
I'm creating a very simple Java chat program, using the Java TCP sockets. I'm
Sorry, I'm new to Java and there's probably a very simple answer to this.
Relatively new to rails and trying to model a very simple family tree with
I am sure this is a very simple problem, but I am new to
my file form element is very simple: $this->archivo = new Zend_Form_Element_File('archivo'); $this->archivo->setLabel('Archivo:') ->setRequired(true) ->setDestination(UPLOAD_PATH)

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.