Java buttons not working

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 421 Views - Last Post: 06 December 2012 - 01:38 PM Rate Topic: -----

#1 fvnbab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-November 12

Java buttons not working

Posted 06 December 2012 - 02:42 AM

hi,
I am trying to create hold button just like the ones in fruit slot machines. I have timers for each reel. I can only figure out parts of the code. like for example
 if (num1==num2){  
//is pressed
timer1.stop
      timer2.stop

but i cant seem to relate this to my hold buttons
any help?
Is This A Good Question/Topic? 0
  • +

Replies To: Java buttons not working

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,208
  • Joined: 20-September 08

Re: Java buttons not working

Posted 06 December 2012 - 03:20 AM

Don't really see the problem. Each button stops its Timer in actionPerformed
Was This Post Helpful? 0
  • +
  • -

#3 fvnbab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-November 12

Re: Java buttons not working

Posted 06 December 2012 - 03:22 AM

but how to i relate this to my hold buttons?
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,208
  • Joined: 20-September 08

Re: Java buttons not working

Posted 06 December 2012 - 03:30 AM

I don't know what you mean by 'relate'. You just add an ActionListener to your button
Was This Post Helpful? 1
  • +
  • -

#5 fvnbab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-November 12

Re: Java buttons not working

Posted 06 December 2012 - 03:33 AM

sorry if i confused but i meant what do you put after this code
if (e.getSource()== btnhold ){

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java buttons not working

Posted 06 December 2012 - 09:06 AM

   if(e.getSource() == btnHold) {
      timer.stop();
   }


Was This Post Helpful? 1
  • +
  • -

#7 fvnbab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-November 12

Re: Java buttons not working

Posted 06 December 2012 - 10:22 AM

Thank you for you help
I have added in the code But it doesn't work because after clicking on hold then clicking on
spin button timer restarts again.
Unless i have some sort of re-spin Button ? Is that right? or can i emend the code for my spin button?
Thanks again for help
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java buttons not working

Posted 06 December 2012 - 10:30 AM

There are multiple ways you can have

   boolean holdWasClicked = false;

   public void actionPerformed(ActionEvent e) {

       if(e.getSource() == btnHold) {
          timer.stop();
          holdWasClicked;
       }

       if(e.getSource() == btnSpin) {
          if(holdWasClicked)
             return;
          ...
       }


or the one that I would use to show to the user (the color will change) that he cannot spin again

   public void actionPerformed(ActionEvent e) {

       if(e.getSource() == btnHold) {
          timer.stop();
          btnSpin.setEnable(false);
       }

       if(e.getSource() == btnSpin) {
          ...
       }


Was This Post Helpful? 0
  • +
  • -

#9 fvnbab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-November 12

Re: Java buttons not working

Posted 06 December 2012 - 10:55 AM

Hi i am trying the first code
if(e.getSource() == btnHold) {
       timer.stop();
       holdWasClicked;//here it gives me an error insert "AssignmentOperator Expression"


and can you explain
boolean holdWasClicked = false;//what does adding false do?


Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java buttons not working

Posted 06 December 2012 - 11:02 AM

sorry my bad
boolean holdWasClicked = false;    // has to be in the instance variables

public void actionPerformed(ActionEvent e) {

    if(e.getSource() == btnHold) {
       timer.stop();
       holdWasClicked = true;    // <---n set to true the button was clicked
    }

    if(e.getSource() == btnSpin) {
       if(holdWasClicked)        // <--- the hold button was clicked
          return;                // <--- so I'll do niothing
       ...
    }


Was This Post Helpful? 1
  • +
  • -

#11 fvnbab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-November 12

Re: Java buttons not working

Posted 06 December 2012 - 11:12 AM

it stops the timer but when the user click on spin it should enable the other two reels to spin.
Was This Post Helpful? 0
  • +
  • -

#12 fvnbab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-November 12

Re: Java buttons not working

Posted 06 December 2012 - 11:30 AM

actually i figured out myself Thanks for help!
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java buttons not working

Posted 06 December 2012 - 11:32 AM

The easiest way would be to have, as real machine have:

3 timers
3 hold buttons
Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,208
  • Joined: 20-September 08

Re: Java buttons not working

Posted 06 December 2012 - 11:36 AM

Your problem is caused by an inadequate analysis of the requirements and hence getting the wrong design. I'm not going to do it for you, but in the real world, the wheels are largely independent, yet you're treating them (at least in the area of handling events) as if they're not. Each wheel should have its own spin and hold Action. Maybe nudge etc.
Was This Post Helpful? 0
  • +
  • -

#15 fvnbab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-November 12

Re: Java buttons not working

Posted 06 December 2012 - 12:06 PM

I do have three hold buttons and three timers
but the only problem was when I click spin After i click hold* the all three timers start again
because of only one actionlistener for spin button

This post has been edited by fvnbab: 06 December 2012 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2