John Clark
John Clark

Reputation: 3

Limit Number of Times JButton can be clicked?

I have a dice program in Java that roll 5 dice by clicking a "Roll" button.

I am trying to make the button so that after it is clicked 3 times, it is disables and can not be clicked unless it is closed and re-opened.

Thank you!

topPanel.add(button1);
int i = 0;
button1.setToolTipText("Click this button to roll the dice.");
button1.setForeground(Color.red);
button1.setContentAreaFilled(false);
button1.setFocusPainted(false);
button1.setBorderPainted(false);
if (i >= 3) {
  button1.setEnabled(false);
} else {

  i++;
}
button1.setFont(new Font("Arial", Font.BOLD, 15));
button1.setPreferredSize(new Dimension(40, 25));
button1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
          die1.roll();
          die1.draw(dk);
          die2.roll();
          die2.draw(dk);
          die3.roll();
          die3.draw(dk);
          die4.roll();
          die4.draw(dk);
          die5.roll();
          die5.draw(dk);

Upvotes: 0

Views: 2840

Answers (5)

artist
artist

Reputation: 6371

Declare a variable in your class body,

int counter = 0;

//In your button click event

counter = counter + 1;


 if((counter > 0) && (counter < 3){

// your program logic comes here

}

if(counter >= 3){

 button.setEnabled = false;

 Toast.makeText(activityname,"Restart the game", TOAST.LENGTH_LONG).show();

}

Upvotes: 1

Madhawa Priyashantha
Madhawa Priyashantha

Reputation: 9872

use a variable and if condition declare variable outside

int i=0;

inside button action performed event

if(i>=3){
  button.setEnabled(false);
}else{
  // do anything
}
i++;

move code outside of event

    int i = 0;

    topPanel.add(button1);
    button1.setToolTipText("Click this button to roll the dice.");
    button1.setForeground(Color.red);
    button1.setContentAreaFilled(false);
    button1.setFocusPainted(false);
    button1.setBorderPainted(false);

    button1.setFont(new Font("Arial", Font.BOLD, 15));
    button1.setPreferredSize(new Dimension(40, 25));
    button1.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent event) {

if (i >= 3) {
      button1.setEnabled(false);
    } else {

              die1.roll();
              die1.draw(dk);
              die2.roll();
              die2.draw(dk);
              die3.roll();
              die3.draw(dk);
              die4.roll();
              die4.draw(dk);
              die5.roll();
              die5.draw(dk);
    }
i++;
}

Upvotes: 0

MaddHatter
MaddHatter

Reputation: 3

Utilize a counter initialized to zero and incremented inside the actionListener for the button. After every time the button is clicked check to see if the counter value is less than your predetermined limit. If it reaches the limit call setEnabled(false) on the button object.

Upvotes: 0

Iker Obregon Reigosa
Iker Obregon Reigosa

Reputation: 243

You can create a variable to count how many times that button has been pressed. You create you variable where has int.

Inside the button listener you increments that variable by 1 (or whatever you want)

Finally you put a condition to disable the button when the counter arrives to the number that you want.

Upvotes: 0

Patricia
Patricia

Reputation: 2865

Make a counter. A variable with the start value zero that increments every time one clicks the button.

Upvotes: 0

Related Questions