button disabled

I need to randomly select a JButton that is not already disabled. I have 15 buttons all together. The user selects a button, it is then disabled, the computer is suppose to then randomly select a button that is not disabled. I am using setEnabled(false), to disable the button. I am not too good at Java and I am a bit lost on how I would do this! Sorry if this is an easy question!

Any help would be great!

[421 byte] By [javaiscoolbutharda] at [2007-11-27 3:09:03]
# 1
Do you know how to generate a random int, say using java.util.Random?
DrLaszloJamfa at 2007-7-12 3:57:40 > top of Java-index,Java Essentials,Java Programming...
# 2
Are you storing the buttons in an Array or Collection?
CaptainMorgan08a at 2007-7-12 3:57:40 > top of Java-index,Java Essentials,Java Programming...
# 3
if your newie your best option is assigning an int to each button and use a switch/case.
big_rota at 2007-7-12 3:57:40 > top of Java-index,Java Essentials,Java Programming...
# 4
ok so what would i put in a Switch? how would it know which buttons are disabled and which ones aren't.
javaiscoolbutharda at 2007-7-12 3:57:40 > top of Java-index,Java Essentials,Java Programming...
# 5

I don't see how a switch is useful, myself.

* Instead imagine an array of 15 JButtons, say buttons

* Randomly select an offset in the array, say j

* disable buttons[j]

What if buttons[j] is already disabled (you said *one* would be)?

Well you can do the random select repeatedly until you succeed --

14-out-of-15 is pretty good odds.

> how would it know which buttons are disabled and which ones aren't?

Doesn't JButton have a method named isEnabled?

DrLaszloJamfa at 2007-7-12 3:57:40 > top of Java-index,Java Essentials,Java Programming...
# 6
Sorry I know this stuff should be easy. How do I store the buttons in an array?JButton[] = {button1, button2, button3, .......that wont work.
javaiscoolbutharda at 2007-7-12 3:57:40 > top of Java-index,Java Essentials,Java Programming...
# 7

JButton[] buttonArray = new JButton[]

{

button1,

button2,

button3,

///etc

};

You can select a random button like this.

Random randGen = new Random();

JButton randomButton = buttonArray[randGen.nextInt(buttonArray.length)];

CaptainMorgan08a at 2007-7-12 3:57:40 > top of Java-index,Java Essentials,Java Programming...
# 8
Thanks Captain. Sorry for being a pain but JButton[] buttonArray = new JButton[]{button1,button2,};this gives an error:cannot find symbolsymbol: variable button1cannot find symbolsymbol: variable button2etc
javaiscoolbutharda at 2007-7-12 3:57:40 > top of Java-index,Java Essentials,Java Programming...
# 9
Have you declared button1, button2, etc?
CaptainMorgan08a at 2007-7-12 3:57:40 > top of Java-index,Java Essentials,Java Programming...
# 10
thanks, got that to work. now its saying than randomButton is null. when the random button is selected i then want to display text in the button ("selected").
javaiscoolbutharda at 2007-7-12 3:57:40 > top of Java-index,Java Essentials,Java Programming...
# 11
Have you initialized all the buttons in the array?
CaptainMorgan08a at 2007-7-12 3:57:40 > top of Java-index,Java Essentials,Java Programming...
# 12
Are the initalized by sayingJButton[] buttonArray = new JButton[]{b1,b2,b3,b4,b5,b6,b7,b8};?
javaiscoolbutharda at 2007-7-12 3:57:40 > top of Java-index,Java Essentials,Java Programming...
# 13
Not unless you've initialized those b1, b2, b3... earlier like.JButton b1 = new JButton();JButton b2 = new JButton();etc.
-Kayaman-a at 2007-7-12 3:57:40 > top of Java-index,Java Essentials,Java Programming...
# 14
yeah i have that done. and im still getting the same error, that randomButton is null
javaiscoolbutharda at 2007-7-12 3:57:40 > top of Java-index,Java Essentials,Java Programming...
# 15

ok, im not getting that error anymore. Now, randomButton is selecting 5 buttons instead of one and I am getting the following error:

random: javax.swing.JButton[,45,0,45x52,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1f78040,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=o.gif,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]

random: javax.swing.JButton[,0,52,45x52,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1f78040,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=o.gif,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]

random: javax.swing.JButton[,45,0,45x52,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1f78040,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=o.gif,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]

random: javax.swing.JButton[,90,52,45x52,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1f78040,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=o.gif,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]

random: javax.swing.JButton[,45,104,45x52,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1f78040,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=o.gif,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]

random: javax.swing.JButton[,45,104,45x52,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1f78040,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=o.gif,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]

random: javax.swing.JButton[,45,104,45x52,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1f78040,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=o.gif,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]

random: javax.swing.JButton[,45,52,45x52,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1f78040,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=o.gif,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=,defaultCapable=true]

javaiscoolbutharda at 2007-7-21 20:41:46 > top of Java-index,Java Essentials,Java Programming...
# 16
Are you sure that is an error? That looks like the output you get when you try to System.out.println() a JButton.
maple_shafta at 2007-7-21 20:41:46 > top of Java-index,Java Essentials,Java Programming...