aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JToggleButton Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JToggleButton Question" Watch "JToggleButton Question" New topic
Author

JToggleButton Question

Paul Murray
Greenhorn

Joined: Oct 11, 2010
Posts: 8
Hi I'm writing a Guessing game app for an assignment (the type of thing where you have to match pictures by revealing pairs)
I'm using Toggle Buttons which display a playing card in un-pressed state and an image of an animal in depressed state. I want the user to be able to have only 2 buttons depressed at any one time unless they get a pair in which case the buttons will remain depressed until the game ends. any help with this would be greatly appreciated

thanks
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Block G Raptor wrote:Hi I'm writing a Guessing game app for an assignment (the type of thing where you have to match pictures by revealing pairs)
I'm using Toggle Buttons which display a playing card in un-pressed state and an image of an animal in depressed state. I want the user to be able to have only 2 buttons depressed at any one time unless they get a pair in which case the buttons will remain depressed until the game ends. any help with this would be greatly appreciated


Sounds like a great project. So what have you tried and what are you having problems with? In other words, what specifically is your question?

Oh, by the way, welcome to the Ranch!
Paul Murray
Greenhorn

Joined: Oct 11, 2010
Posts: 8
Ok I've finished the GUI. the actionlisteners work great (as in when I click a ToggleButton the icon changes to a pic of an animal) what I don't know how to do is to have the first 2 toggle buttons clicked Automatically return to off state when a third buton is clicked (unless of course the first 2 clicked make a pair ie reveal the same image) I haven't really tried anything yet as I'm totally confused as to where to begin. I think I can do it with if statements but would need a separate if for every possible combination of clicks( there are 12 buttons so a whole lot of combinations)
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Just to throw a few ideas your way...

-- instance field: firstButtonClicked (boolean) -- toggle true/false each time any button is clicked
-- instance field: firstButton (JToggleButton -- or even AbstractButton) -- assign the current button clicked when it's the "first button" -- as determined by whether firstButtonClicked is true or false

-- When the button clicked is NOT the first button, compare "values" (icons?). If different, "reset" the currently clicked button AND the firstButton.

Come to think of it, you can probably do without the boolean, if you setfirstButton alternately to the clicked button and to null. Something like


luck, db
There are no new questions, but there may be new answers.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Block G Raptor wrote:Ok I've finished the GUI. the actionlisteners work great (as in when I click a ToggleButton the icon changes to a pic of an animal)

You shouldn't use an ActionListener (not actionlistener)to change the icon. Go through the set... methods of JToggleButton or rather the methods inherited from AbstractButton and list out the methods that take an Icon as a parameter (or if you're lazy like me, find the get... methods that return an Icon and take it from there )
Paul Murray
Greenhorn

Joined: Oct 11, 2010
Posts: 8
Thanks Guys thats really helpfull.
Daryl unfortuneately I have to use action listener as thats the brief for the Assignment. Its a college assignment specifically on action listeners
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

I didn't say you shouldn't use an ActionListener. I said you shouldn't use an ActionListener to change the Icon.
Paul Murray
Greenhorn

Joined: Oct 11, 2010
Posts: 8


OK Guy's above is my code. I'm trying to count the number of times a user clicks any of the JTogglebuttons so i can write an if statement that will reset the first 2 buttons to false(unless they match) when the counter reaches 3. however the counter only ever reaches 1 I've googled how to count button clicks and keep getting the same answer (count++) in the ActionPerformed method as in my code above.
why is it only counting the first click???Do I Need a loop somewhere here
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
To see your problem, walk through your code and analyze its flow and logic. For instance in your actionPerformed method you initialize your count variable and set it to 0, and then increment it. So when a button is pressed and the method is called, a new count variable is created in the method (and is visible only inside of the method) set to zero and incremented. When the method ends, all local variables, including the count variable, are destroyed.

If you want a variable to persist after the actionPerformed method has completed, it must be declared and initialized in the class, not in the actionPerformed method. Make sense?
Paul Murray
Greenhorn

Joined: Oct 11, 2010
Posts: 8
Thanks
I'm a GUI NOOB. so didn't realise that the method is invoked and runs through on each click
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Block G Raptor wrote:Thanks
I'm a GUI NOOB. so didn't realise that the method is invoked and runs through on each click


No problem. Also "Block Raptor" please see your private messages for an important administrative matter.
Paul Murray
Greenhorn

Joined: Oct 11, 2010
Posts: 8
pete stein wrote:
Block G Raptor wrote:Thanks
I'm a GUI NOOB. so didn't realise that the method is invoked and runs through on each click


No problem. Also "Block Raptor" please see your private messages for an important administrative matter.


Done.. Sorry One last question (I hope ) is there a way to pass a button click to an array so i can compare them ie. a 2 element array which takes in the icons from a pair of clicked buttons and then compares to see if they are the same icon ??
I can work out the logic but not the syntax. basically i want say
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Paul Murray wrote:One last question (I hope ) is there a way to pass a button click to an array so i can compare them ie. a 2 element array which takes in the icons from a pair of clicked buttons and then compares to see if they are the same icon ??
I can work out the logic but not the syntax. basically i want say

You may do better to know if one toggle button holds the same ImageIcon as another, and so you can always get the ImageIcons from the toggle buttons and compare them, say have a variable called icon1 and one called icon2, place the icon from the first button into icon1 and the second into icon2 and check if equal. This way you don't have to worry about checking array indices or anything of that sort, and don't have to hard-code which button holds which icon.

Also, when I've done something like this before (fairly recently as I was trying to learn Swing), I used it as an exercise in creating a model / view / controller (MVC) application. It may seem like overkill, but it taught me a lot.
Paul Murray
Greenhorn

Joined: Oct 11, 2010
Posts: 8
pete stein wrote:
You may do better to know if one toggle button holds the same ImageIcon as another, and so you can always get the ImageIcons from the toggle buttons and compare them, say have a variable called icon1 and one called icon2, place the icon from the first button into icon1 and the second into icon2 and check if equal. This way you don't have to worry about checking array indices or anything of that sort, and don't have to hard-code which button holds which icon.

Also, when I've done something like this before (fairly recently as I was trying to learn Swing), I used it as an exercise in creating a model / view / controller (MVC) application. It may seem like overkill, but it taught me a lot.

Thanks pete I get the Idea what I'm stuck on is how does java differentiate one button click from another. ie how do I tell it get the icon from what ever button is clicked first THEN get the Icon from whatever button is clicked next and so on. I've looked at daryll's code earlier in this thread which seems to be pseudo-code as it doesn't run when added to my code
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Paul Murray wrote:Thanks pete I get the Idea what I'm stuck on is how does java differentiate one button click from another. ie how do I tell it get the icon from what ever button is clicked first THEN get the Icon from whatever button is clicked next and so on. I've looked at daryll's code earlier in this thread which seems to be pseudo-code as it doesn't run when added to my code


Darryl's code is not pseudo-code but rather real workable code, but I don't think that it was meant for you to cut and paste. Rather you extract the ideas from the code and use them. You get the current button by calling getSource() on the ActionEvent that is passed into your actionPerformed method. Cast that into a JToggleButton and then extract the icon.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JToggleButton Question
 
Similar Threads
setting an attribute on the httprequest within a jsp
Adding multiple Clickable areas in trailing mode to a frame
Jbutton
Break button out of while loop
Facing problem with KeyListener for a frame having multiple panels