Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Can one Object "Listen" to another Object's "State"

$
0
0
I'm not entirely sure the topic is that descriptive so here comes a particularly lengthy explanation.

Problem setup
A 4 x 4 GridLayout arrangement of JButtons within a JPanel. The buttons each have a getState, setState method, wherein the state represents only one of 8 states (stored in an instance field within the subclassed JButton). The states are manipulated by the user with a simple click of a particular Jbutton. States cycle between 0, 1, and 2. States 3 through 7 are special conditions of state 2. By special conditions I mean that in order for a button to possess a state of 3 ~ 7 it must analyze the states of the surrounding buttons, the user can only cycle from 0 ~ 2, program logic/rules determine the remainder states 3 ~ 7.

My less than stellar solution
I would create a 4 x 4 two-dimensional array and place within it the states, passing the entire array through to the actionPerformed method of a particular JButton in order to determine whether it should be given states 3 ~ 7 (according to a set of prescribed rules). However, the limitation here is that a condition can occur wherein changing the state of the button that was clicked can also change the state of one of the 8 (or less if it happens to be at a corner/border) outlying buttons, meaning I would have to make the array an array of these custom JButtons in order to be able to manipulate their values at will. It seems like a lot of memory overhead to me, perhaps I'm wrong.

My hypothetical solution (as I've no idea whether or not it's even possible)
To somehow code a Listener, most likely a PropertyChangeListener that can somehow implement a form of recursion to dynamically update the state of a JButton, even if it wasn't clicked, due to its neighbor being clicked. Essentially creating a "chaining" effect wherein the buttons would update themselves "intelligently."

Conclusion
I realize this is a pretty abstract way of describing the problem. The problem, at least from my viewpoint, is currently more abstract than practical. The logic by which my particular problem description constrains me is less important than the general idea of constraining two or more distinct objects to a set of prescribed rules and making them aware of one another in some regard.

I appreciate any imagination you can spare.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>