944,097 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1452
  • Java RSS
Dec 4th, 2007
0

Turning my board from console to Message box

Expand Post »
My tic tac toe board is done, but I need it to show up in the Dialog box, not the console, how would I do that?
Here is my code:

javax.swing.JOptionPane.showMessageDialog(null, "\n-------");

for (int i = 0; i < 3; i++) {
System.out.print("| ");
for (int j = 0; j < 3; j++)
System.out.print(spot[i][j] + " | ");
javax.swing.JOptionPane.showMessageDialog(null, "\n--------");
Reputation Points: 10
Solved Threads: 0
Light Poster
Dio1080 is offline Offline
47 posts
since Aug 2007
Dec 4th, 2007
0

Re: Turning my board from console to Message box

Ok, I'm taking a look at it, but first you should know that when you post code, you should use the code tags like so:
Java Syntax (Toggle Plain Text)
  1. [ code ]leave out the spaces in the tags[ /code ]
and when you're using a specific language like you did (i.e. Java) you use it like so:
Java Syntax (Toggle Plain Text)
  1. [ code=java ]leave out the spaces in the tags[ /code ]

Now, although I'm able to figure it out myself, you have one '{' and no '}' in your code. It's not because you're talking to humans instead of the compiler that you don't have to be correct anymore. If your code and idea behind it wouldn't be that simple, it would be hard for us to know what you are really trying to do.

Others will appreciate it more when you follow rules like this and therefor will be more willing to help you out.

Black Box
Last edited by Black Box; Dec 4th, 2007 at 3:07 am.
Reputation Points: 60
Solved Threads: 7
Junior Poster in Training
Black Box is offline Offline
62 posts
since Nov 2007
Dec 4th, 2007
0

Re: Turning my board from console to Message box

java Syntax (Toggle Plain Text)
  1.  
  2. javax.swing.JOptionPane.showMessageDialog(null, "\n-------");
  3.  
  4. for (int i = 0; i < 3; i++) {
  5. System.out.print("| ");
  6. for (int j = 0; j < 3; j++)
  7. System.out.print(spot[i][j] + " | ");
  8. javax.swing.JOptionPane.showMessageDialog(null, "\n--------");
  9. }

I have the board, it works, but when I try making a messagebox instead of a console, I'm confused...
Last edited by Dio1080; Dec 4th, 2007 at 3:10 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
Dio1080 is offline Offline
47 posts
since Aug 2007
Dec 4th, 2007
0

Re: Turning my board from console to Message box

Ok, I'm assuming you want everything to appear in the message box. First of all, everything you give System.out.print, will be printed to your console, no matter what. The easiest way to get this done in your case is to do the following. Instead of printing what you want bit by bit to your messagebox, you should place it al in one string. So, don't use System.out.print, but use something like myString = myString + " | "; or something.

Then after your loops, you can create your messagebox and give your string to it as parameter.

That should pretty much do it. However, it seems that you're trying to work your way into Swing and I suggest you learn swing like it's supposed to be learned: from the beginning.

Black Box
Reputation Points: 60
Solved Threads: 7
Junior Poster in Training
Black Box is offline Offline
62 posts
since Nov 2007
Dec 4th, 2007
0

Re: Turning my board from console to Message box

waoh woah woah, thanks for the help, but I like it simple, I only got to arrays in my book so, everything I did so far has been simple, nothing to advance, thats why I have about 3 to 4 pages of code.
Reputation Points: 10
Solved Threads: 0
Light Poster
Dio1080 is offline Offline
47 posts
since Aug 2007
Dec 4th, 2007
0

Re: Turning my board from console to Message box

I can understand that you want to keep it simple, really, I think everyone wants everything to be simple. The thing is, diving head first in Swing and trying everything wrong, right and different until it works is:
1. a lot of work.
2. not going to teach you something because you won't know the exact reason why it works this time, but not any other time.
3. because of 1 & 2 a lot harder instead of simple...

So, either wait a bit with Swing, or try to put more time in it so you can learn it "the easy way".

Black Box
Reputation Points: 60
Solved Threads: 7
Junior Poster in Training
Black Box is offline Offline
62 posts
since Nov 2007
Dec 4th, 2007
0

Re: Turning my board from console to Message box

Yes, moving your console app over to a Swing component isn't just a matter of stuffing the output into a message box. Message boxes don't work like the console at all. You will need to work with frames/panels and either arrays of components or custom 2D painting with mouse listeners.

It gets deep pretty quickly once you decide to move into GUI programming and as Black Box noted, you need to start at the beginning.

If you do go that route and get a bit of understanding of Swing under your belt, feel free to post back with questions on what components and techniques can be used to build your interface.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Gridbag Layout Probelm [Code Included]
Next Thread in Java Forum Timeline: compliler





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC