aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Buttons... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Buttons..." Watch "Buttons..." New topic
Author

Buttons...

James Mousseau
Greenhorn

Joined: May 28, 2008
Posts: 22
How can you say "If this button is clicked, and this button is clicked, and this button is clicked, Do this"

The thing is They have to be clicked within a certain order.

if (("X".equals (box1))&& ("X".equals (box2)) && ("X".equals (box3)))

winner is a Jlabel; box1 - box9 are buttons; p1s = int; PO = TextField; score1 is a string.
and Im unsure on how to get the int p1s to convert it to a string so it can be outputted in the PO textbox.

I would really appreciate the help


When in doubt, get the hammer!
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

So clicking things in Swing just causes events to happen. There is no special rhyme or reason, they just register events. So if you need to keep track of what was clicked in what order that is up to you to keep track of. Think data structure, not GUI.

As far as int to String look at the String API.

String.valueOf(int)


GenRocket - Experts at Building Test Data
James Mousseau
Greenhorn

Joined: May 28, 2008
Posts: 22
I have the code entered when something is clicked, do this, but I cant seem to find out how to make it if 3 of them are clicked and marked as something, then do this.

I can upload all of the code, but it doesn't seem necessary. I cant seem to find the logic in saying if 3 of the 9 buttons are the same output ("X"), then do something else.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Saying that 3 of the buttons are the same output makes no sense. Sounds to me like you need to keep a multidimensional array that corresponds to your button matrix. I'm just guessing because you really haven't given any context to your problem. I'm assuming this is some sort of game from your Winner label.

You could also query your button container for all of its components, loop through them, and get the text or name of the button (whichever you are changing) but I'd go for the data structure approach and just update it then query it on each button click event.
James Mousseau
Greenhorn

Joined: May 28, 2008
Posts: 22
Yes sir, it is a game.


I programmed this code looking at stuff on the internet, kinda new, but I have some knowledge of programming, just not much. its the mathematics left in the program that I must finish, and it doesnt seem to want to work out for me.

And I really appreciate you taking time to review this for me.
[ June 11, 2008: Message edited by: Ulf Dittmer ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Ok, so here is what I would do. At least something like this.

Create a matrix to hold your x's and o's

char matrix[][] = new char[3][3];

When a button is clicked determine which position in the matrix to update:

matrix[2][1] = "X";

After which, you check the rows, cols, and diags:

James Mousseau
Greenhorn

Joined: May 28, 2008
Posts: 22
Originally posted by Gregg Bolinger:
Ok, so here is what I would do. At least something like this.

Create a matrix to hold your x's and o's

char matrix[][] = new char[3][3];

When a button is clicked determine which position in the matrix to update:

matrix[2][1] = "X";

After which, you check the rows, cols, and diags:



This is extremely helpful, but the thing I dont get is "char matrix[][] = new char[3][3];"

When you have the [3][3], what does that mean, im a little unsure of this, is that basically saying 3X3? And for the rows, columns, and diagonals, how does that register 3 across, up and down, and top side to bottom opposite?

And before you go any further, I would really like to thank you and the site for being so helpful, I really appreciate all the help you and the JavaRanch community has done for me.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

This is extremely helpful, but the thing I dont get is "char matrix[][] = new char[3][3];"

This is a multidimensional array. Some might call it a matrix. Visually it would look something like this assuming a complete game where X's won.



Your winning combination is this

matrix[0][0]
matrix[0][1]
matrix[0][2]

If you follow the cols for loop through it would find this for you. Here is another.



matrix[0][1]
matrix[1][1]
matrix[2][1]

Note that arrays start at 0 so an array of length 3 goes from 0,1,2. Does that help?
James Mousseau
Greenhorn

Joined: May 28, 2008
Posts: 22
Makes perfect sense, thanks again.
James Mousseau
Greenhorn

Joined: May 28, 2008
Posts: 22
Tricky situation, it does not seem to want to work for me...

Error sais that "The type of the right sub-expression "java.lang.String", is not assignable to the variable, of type "char""

So I was wondering if I wrote it wrong, because I cant seem to figure out on where to assign the char to the clicks of X's and O's otherwise.



Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

I Think I might a syntax mistake. Try doing it like this:

matrix[4][3] = 'X';

Notice the single quotes rather than double quotes. char vs String.
James Mousseau
Greenhorn

Joined: May 28, 2008
Posts: 22
Well, it seems to work now, but its the same problem. It only switches up in box9

EDIT:
Could it be the placement on where the cols/rows/diags are placed, what if they are placed in lets say the code where box1 code is for X or O?

Or if it should be placed inside a bracket differently...

Im about ready to get the hammer!
[ June 10, 2008: Message edited by: James Mousseau ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Well, I assumed there were some holes in my implementation and I thought I'd give you a chance to find them. But you've been struggling along pretty good now so I whipped up very simple version of your app. This version is basically what I'd come up with if I had 15 minutes to get it done. So take it as exactly that and probably not something you'd want to wind up with as final quality. But it does work.

James Mousseau
Greenhorn

Joined: May 28, 2008
Posts: 22
Im not too sure where you program, but It didnt seem to want to run in "Ready To Program", which is what I use.

Couldnt see it run, so im still a little bit confused on the mathematics...

I tried piecing a bit of your code in for the mathematics in the private void, and it still wouldnt work properly, it would output the winner, even though the winner didnt win.

I do appreciate the help, I was just wondering with my code if you see a slight difference from yours, because mixing two different program styles usually doesnt work. Yours involves more complexity, compared to my basic coding. (eg, I have it rigged up so if you expand the program it wont look anything like X and O game, you have to keep it as it lies)

Im looking at your code, and I seen some things that I havent "understood" yet.

Im still continuing to fool around with it, but it seems to always get worse... and Im not too sure on why...



The thing I dont understand is could the problem be because of the placement of brackets? or the fact that I need more of something (missing something)?

Thats all the buttons, all the commands on the math, and functioning of the program from where I am now (not many changes, but some).

Hopefully a thank you in advance


EDIT:
And I was wondering if this could be an alternate ending to this massacre:

But the thing is I get an error saying its not boolean or something for the line "if (box1.setText = "X" && box2.setText ("X") && box3.setText("X"))"
Is there a way to set it up that way, or is it undoable?
[ June 11, 2008: Message edited by: James Mousseau ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Im not too sure where you program, but It didnt seem to want to run in "Ready To Program", which is what I use.

I'm not sure what that means.

or the fact that I need more of something (missing something)?

Did you initialize your matrix with ' ' values?



If you didn't then your if() block will never evaluate to true since you are checking for matrix[][] == ' '. Last night I realized that without a 3rd value (other than X and O) a row could appear to be a winner even though nothing was selected because as the loop runs through and checks each row/col/diag it will see that unselected box's are the same value. So you have to check for the same X or O as well as not being your default 3rd value. In my case I chose a space character.
James Mousseau
Greenhorn

Joined: May 28, 2008
Posts: 22
I just added that in, same problem, but didnt add the void part

I was wondering if this would make the difference


and I see you set play as char, so would that make a difference? or no?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

But the thing is I get an error saying its not boolean or something for the line "if (box1.setText = "X" && box2.setText ("X") && box3.setText("X"))"
Is there a way to set it up that way, or is it undoable?


I'm really not even sure what you are trying to do there. conditional blocks like if() check for true|false. The way to test for equality in Java is usually ==, .equals(), or .comparesTo(), depending on the type of data you are comparing.

== is used for primitives primarily so

if (2 == 2) // int
if ('a' == 'a') // char

.equals is used to check Objects so

if (Player1.equals(Player2))
if ("X".equals("X"))

You can also use .compareTo() to compare Strings

if ("X".compareTo("X") == 0) // returns non zero if false.
James Mousseau
Greenhorn

Joined: May 28, 2008
Posts: 22


What im trying to say is:

If box1's text is "X", and box2's text is "X", and box3's text is "X"

then output X Wins in a label,

else
leave it at Get 3 in a row.

Because my code sends the text to the buttons on X or O, so if there is 3 in a row that way, then output a winner.

If that makes sense, and I noticed you typed different methods on doing that possibly, which one would I like to use in this sort of format
[ June 11, 2008: Message edited by: Ulf Dittmer ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Ted Smyth
Ranch Hand

Joined: May 28, 2008
Posts: 73
Originally posted by James Mousseau:

And I was wondering if this could be an alternate ending to this massacre:

But the thing is I get an error saying its not boolean or something for the line "if (box1.setText = "X" && box2.setText ("X") && box3.setText("X"))"
Is there a way to set it up that way, or is it undoable?

[ June 11, 2008: Message edited by: James Mousseau ]




Not sure if you fixed this yet, but setText is a method, so you need to add brackets: box1.setText("Some Text"); which sets the text in the box. The method you're looking for is getText() which returns the value of the box's text: box1.getText().equals("X");

The expression

does not return a boolean value. That's why the compiler complains about it. IF statements must always evaluate to true or false (boolean values). A logical compare: if (someVariable.equals(someotherVariable)) will return true or false, satisfying the requirements of an IF statement.

Also: you can't compare Strings using the == operator, unless they are two references to the same String variable. Use the equals("") method to compare Strings properly (it compares the content of the Strings, where == compares the memory addresses of the two String variables).

So what you want is:



This way, you are comparing the text content of the boxes with the logical string "X", instead of comparing void (which is what setText("") returns) with the address of a variable you created on the fly, which has the value "X".

... a bit long-winded, but I hope it helps clear up a few conceptual problems with your code.


Edward Smith
James Mousseau
Greenhorn

Joined: May 28, 2008
Posts: 22
I Honestly think that solved my whole problem.

It works for box1,2, and 3!

I honestly dont know how to thank you gregg.

Ill try to finish up the program, dont think with that information there, its smooth sailing

Thanks again!


EDIT:

And yes, it did clear it up Ed, Much appreciated for all the help I recieved since I joined.

Ill try to give back to the community in a few years when I start learning more and more about Computer Programming.

[ June 11, 2008: Message edited by: James Mousseau ]
Ted Smyth
Ranch Hand

Joined: May 28, 2008
Posts: 73
Good luck and keep having fun!
James Mousseau
Greenhorn

Joined: May 28, 2008
Posts: 22


Its supposed to set the Jlabel to 1, then when the game starts again, how do I keep it counting from 1 on?
it seems to stay at 1.



Is declared in the public class XOXO, and this is the restart to the game under public static void:


Last time I had a problem like this it was where the variables were defined, but Im pretty sure they are where they are supposed to do...

last think I promise
[ June 11, 2008: Message edited by: James Mousseau ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Buttons...