13 Replies - 274 Views - Last Post: 06 November 2011 - 05:31 PM Rate Topic: -----

#1 OttotheRobot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-November 11

Problem with GUI counter clicks

Posted 06 November 2011 - 03:41 PM

Having a bit of trouble updating the counter clicks every time a button is clicked

Example of the code I have so far:

int counter = 0;
JButton click = new JButton("Click");
JLabel count = new JLabel("Score: " + counter);

count.add(click);

click.addActionListener(this);

if(e.getSource() instanceof JButton)
{
     if(e.getActionCommand().equals("Click"))
     {
          counter++;
          count.setText("Score: " + counter);
     }
}





Any help would be appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with GUI counter clicks

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,523
  • Joined: 27-December 08

Re: Problem with GUI counter clicks

Posted 06 November 2011 - 03:45 PM

We'll need a little more information on what exactly is wrong? And did you setActionCommand() on the JButton?
Was This Post Helpful? 0
  • +
  • -

#3 OttotheRobot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-November 11

Re: Problem with GUI counter clicks

Posted 06 November 2011 - 03:51 PM

I basically need the counter to increment by one every time the user clicks on the "click" button. No, I haven't set a setActionCommand(). I've seen this repaint() code around but not sure how to implement it to my GUI program. Something along the lines of this:

http://i.imgur.com/2tNOP.jpg
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9037
  • View blog
  • Posts: 33,523
  • Joined: 27-December 08

Re: Problem with GUI counter clicks

Posted 06 November 2011 - 03:51 PM

You shouldn't need to invoke repaint() for this yourself. Also, can I assume that this is all of your code or just a sample of it?
Was This Post Helpful? 0
  • +
  • -

#5 OttotheRobot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-November 11

Re: Problem with GUI counter clicks

Posted 06 November 2011 - 03:54 PM

Oh okay. It's just a small snippet of a larger program. There are many buttons in my program but if I can implement a counter for just one then I think I'll be able to finish the rest of it.
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Problem with GUI counter clicks

Posted 06 November 2011 - 04:11 PM


if(e.getSource() instanceof JButton)
{
     if(e.getActionCommand().equals("Click"))
     {
          counter++;
          count.setText("Score: " + counter);
     }
}


This needs to be in an actionperformed method in the class. Cannot just have it in the constructor.
Was This Post Helpful? 0
  • +
  • -

#7 OttotheRobot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-November 11

Re: Problem with GUI counter clicks

Posted 06 November 2011 - 04:14 PM

View PostFuzzyness, on 06 November 2011 - 04:11 PM, said:


if(e.getSource() instanceof JButton)
{
     if(e.getActionCommand().equals("Click"))
     {
          counter++;
          count.setText("Score: " + counter);
     }
}


This needs to be in an actionperformed method in the class. Cannot just have it in the constructor.


It already is. That code is just the basics to get my problem across. Thanks for the input.

public void actionPerformed(ActionEvent e)
{
     if(e.getSource() instanceof JButton)
     {
          if(e.getActionCommand().equals("Click"))
          {
               counter++;
               count.setText("Score: " + counter);
          }
     }
}

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,148
  • Joined: 06-March 08

Re: Problem with GUI counter clicks

Posted 06 November 2011 - 04:41 PM

should work
counter will be incremented everytime the "Click" button is clicked
Was This Post Helpful? 0
  • +
  • -

#9 OttotheRobot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-November 11

Re: Problem with GUI counter clicks

Posted 06 November 2011 - 04:45 PM

Okay I'm starting to see my problem now. I'm trying to get the click counter to increment by one when I select two buttons that are the same. Going to have to work a little harder on the code. Thanks!
Was This Post Helpful? 0
  • +
  • -

#10 OttotheRobot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-November 11

Re: Problem with GUI counter clicks

Posted 06 November 2011 - 04:58 PM

While this topic is still alive, this code doesn't work and I'm not sure why:

if(e.getActionCommand().equals("Click") && e.getActionCommand().equals("Click"))
counter();


Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,148
  • Joined: 06-March 08

Re: Problem with GUI counter clicks

Posted 06 November 2011 - 05:00 PM

if(e.getActionCommand().equals("Click") && e.getActionCommand().equals("Click"))

why do you check twice if e.getActionCommand().equals("Click")
if it is true once it will be true forever
Was This Post Helpful? 0
  • +
  • -

#12 OttotheRobot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-November 11

Re: Problem with GUI counter clicks

Posted 06 November 2011 - 05:03 PM

There's two buttons, one is "Click 1" and the other is "Click 2". I want the counter to increment by one when both of those buttons are clicked. So I was thinking the code for this would be:

if(e.getActionCommand().equals("Click 1") && e.getActionCommand().equals("Click 2"))


Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,148
  • Joined: 06-March 08

Re: Problem with GUI counter clicks

Posted 06 November 2011 - 05:07 PM

now you just added "Click 1" and "Click 2" in your question :)
How can an ActionEvent comes from Click1 AND Click2 ? It is impossible
Was This Post Helpful? 1
  • +
  • -

#14 OttotheRobot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-November 11

Re: Problem with GUI counter clicks

Posted 06 November 2011 - 05:31 PM

Oh okay this is making more sense now. I have it figured out. Thanks for the help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1