Morsfmo
Morsfmo

Reputation: 21

java swing checkboxes java swing

I have a question with my java application. I create a dice role game on java swing, created separate object for every dice that shows different dice numbers on my frame every time clicking throw button getting problem with jcheckboxes everytime. I want when checkbox is checked the dice would stay with that number and dont change.

I trying this this way:

if(ch1.isSelected() == true){  
    ch1.setSelected(true);
    Die[0].setValue(0);     
}

ch1 is my checkbox I want to keep always checked ,and keep value 0 but when button throw pressed, it still giving a random number for that dice.

maybe you know how to keep checkbox always checked ?

Upvotes: 1

Views: 305

Answers (3)

Robert
Robert

Reputation: 38

Create a dice object:

public class Dice {

private boolean canRoll;
private int value;

public Dice() {
    canRoll = false;
    value = 0;
}

public int roll()
{
    if(canRoll) {
        // Roll the dice!
        value = /* Roll algorithem */;
    }
    return value;
}

public void setCanRoll(boolean k)
{
this.canRoll = k;
}

}

Than create a array of dice objects. You can set the canRoll boolean with the setCanRoll(boolean k) method. Roll the dice and print the output to the screen.

If this is not what you mean than maybe: ch1.setEditable(false);

Upvotes: 0

Morsfmo
Morsfmo

Reputation: 21

this method inside action listener where i have my objects and cant access them from where

public void myDices() {

    Die[] Die = new Die[10];
    Die[0] = new Die();
    Die[1] = new Die();
    Die[2] = new Die();
    Die[3] = new Die();
    Die[4] = new Die();
    Die[5] = new Die();
    Die[6] = new Die();
    Die[7] = new Die();
    Die[8] = new Die();
    Die[9] = new Die();

    diceClass.roll(Die);
    diceClass.display(Die);

Upvotes: 0

liquidsystem
liquidsystem

Reputation: 634

Try this instead:

private void ch1MouseClicked(java.awt.event.MouseEvent evt) {                                        
    Die[0].setValue(0);
}  

Upvotes: 1

Related Questions