Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Mouse clicking  RSS feed

 
James Mousseau
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to have mouse clicking in affect to a swing program?

eg

First click outputs X
second click outputs O
third click outputs X...etc

I dont know alot of the different typs of coding, just trying to learn...
I recently found import java.awt.event.MouseAdapter;, but not to sure on how to use it...
 
Campbell Ritchie
Sheriff
Posts: 53742
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your XO app, you don't want to use a MouseAnything; you are putting buttons on, which support ActionListeners, so you want ActionListeners.

I don't like your design; you ought to be adding your buttons with a for-loop, because they are so similar to one another. You also ought to add ActionListeners to them individually, rather than using addActionListener(this). See these recent threads: 1. 2. An old thread which has reappeared.

In fact you want a class called XONumberListener implements ActionListener, with the number as a field, then you can pass the number for the button to its constructor.

I would consider putting all the buttons into an array; you might do better to have a JButton[3][3] array, then each button (and each Listener object) would have a row number (0, 1, 2) and a column number (0, 1, 2) which might be easier to handle than 1 2 3 4 5 6 7 8 9.

You can use turn++; it will work just as well as turn + 2.

Sorry this will appear so much like hard work, but you will get a better design, and much less code, in the end.
 
James Mousseau
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for those links to other posts, and the advice on shortening my code, ill try it out, thanks
 
Campbell Ritchie
Sheriff
Posts: 53742
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why Campbell does not like your design . But you can use MouseListener. I have given a sample program below. Based on number of clicks country names will be appended and displayed in the text area.

 
James Mousseau
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really appreciate all of the help guys, its really helping me get my brain ready for college.

Ill try both methods out, so I know both

Much appreciated!
 
Campbell Ritchie
Sheriff
Posts: 53742
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason I chose to add an ActionListener to a JButton is that one should always use the highest-level Listener available.

You are mistaken that you are using number of clicks in that example; you are using the button and calling it number of clicks misleadingly.

The problem with using addActionListener(this) is that you end up with an unwieldy actionPerformed method, not consistent with the customs of object-oriented programming. Look how many ifs there are, which actually do the same thing. It would be a nightmare to maintain, and ought to be embodied in a class of its own.

Actually, there are simpler ways of setting X and O; you can set up a String[] with two members, and a single turn field. On clicking a button, you can sayYou can embody that in a class and call it XOActionListener implements ActionListener, then when you add to a button b you can say b.addActionListener(new XOActionListener(b)); It will save about 60 lines of code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!