• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to change action after the 1st JButton click

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm stacked!! I am relatively new to java and I have been days searching the web to solve my problem without success, its now time to ask for your help.

I cannot...(actually do not know) how to change the actionlistener (actionPerformed) after the second click in an array of JButtons. Please find the code below so that you can guide me.

This is how the program performs right now.

On the 1st click on the grid, the border of the button clicked will change to 'blue' and another button will change its boarder to 'red'. on the next click the same action is performed.

What I need is to change action when the 'red' bordered button is clicked, lets say change the color of the buttons from the 'red' to the 'blue' buttons.(x 3 buttons).

The logic about how to perform the final result I think I can do on my own but my problem is how to change the action when the red bordered button is clicked.

Your help is much appreciated!

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Can you not keep track of that event in some variable, and then use the variable to influence the behavior of the action listener?
 
Matthew Buhagiar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your welcome!

Would it be .getSource() method?.....keep in mind I a beginner, I am still scrolling not even standing on my feet yet!!

Can you set me an example?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getSource? No, not sure what you have in mind.

I would advise to keep track of the status (number of clicks, if I understand that correctly) of each cell in a dedicated data structure (like a 10x10 array).

Alternatively, you can also make that part of the ActionListener class. Something like this:


 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think this discussion would fit better into our GUIs forum.
There is something peculiar about wanting to change the action after something has been clicked. The button would change its type after the first click. Very strange design.
You can however remove a Listener from a button and add a new Listener.
Or you can have a field in the Listener object
You can also have a Listener which counts clicks and behaves differently for single‑clicks and double‑clicks. But I can't remember how to do it. There is a getClickCount method in the MouseEvent class, so you may only be able to do that for a MouseListener and use its clicked method. That probably is different from what you want.
 
reply
    Bookmark Topic Watch Topic
  • New Topic