1

I have multiple places where I convert between one coordinate system and another. In each case there is a cosine/sine calculation between, let's call them x, y and x', y'. These are all JFormattedTextFields.

If the user enters a value in any of the 4, an ActionListener is called. Lets call the fields fieldx, fieldy, fieldx1, and fieldy1. If the user enters anything in fieldx or fieldy, I HAD keyboard and focus listeners (the same one for all four fields) that would update fieldx1 and fieldy1 from the current values in fieldx and fieldy. If the call to the listener was from fieldx1 or fieldy1 it would calculate fieldx and fieldy.

Then I decided to save fields chosen (including a bunch of check-boxes on/off and some spinners' values) in a config file (new requirement after development). I thought that by setting values and states all would be fine BUT certain things were not happening (behind the scenes). I decided that part of this is that the triggered methods by various checking and entering and etc were not happening because the fields did not trigger keyboard and focus listeners when they were set by a piece of code.

After reading online I changed all the KeyboardAdapter to ActionListener and after I set the value I call postActionEvent() on the fields I mentioned above and now all the background stuff happens (though order is an issue and I'm going to save extra information about state to update this properly). I made this change because I thought it would be more difficult to fire off so many fake keyboard events?

There are probably more clever/smart ways to do this but for now I'm trying not to touch too much code.

  1. Does anyone have a suggestion on a way to save the state of a panel and refresh this (the current object, the panel)?

  2. If I continue to do it in this way, can someone suggest a way to MINIMIZE the times the ActionListener fires? It seems to fire so often it is ridiculous!

Should I instead do things as suggested here?

Should your class implement ActionListener or use an object of an anonymous ActionListener class

That seems like a lot more coding involved but a lot more precise in what triggers when...

If this kind of question/discussion is out of place here, just let me know :). Just typing up this page has made me think of more things to read up on and to try.

Community
  • 1
  • 1
evernoob
  • 53
  • 7

1 Answers1

14

I'm trying not to touch too much code.

This is a foundational mistake; the trash can is an important design tool, and one or more minimal examples will be invaluable in learning to compose more complex applications.

Does anyone have a suggestion on a way to save the state of a panel and refresh this (the current object, the panel)?

Using the Model–View–Controller pattern, the program's data should be stored in a suitable model, and transformations should be done when model elements are rendered in the view; the Converter example shows how to manage custom events, and this example expands on the topic

If I continue to do it in this way, can someone suggest a way to MINIMIZE the times the ActionListener fires?

As suggested in the original article, use Action to encapsulate behavior. See Java SE Application Design With MVC: Issues With Application Design for a deeper examination of the problem.

Community
  • 1
  • 1
trashgod
  • 196,350
  • 25
  • 213
  • 918
  • I see where the name comes from, trashgod. . . I might have time to do that now. When I asked this question I was getting very strange behavior at a critical time (no time to trash and re-factor code). I hope to do some of that now, over the next 6 months. Thanks. – evernoob May 05 '14 at 19:52
  • 2
    Excellent; I too strive to be _evernoob_, in the sense of always learning. Going forward, you can accept this answer by clicking on the [empty check mark](http://meta.stackoverflow.com/a/5235/163188) at the left. Leave a comment if you pose a related question. – trashgod May 05 '14 at 20:11
  • See also [*Java SE Application Design With MVC (with images)*](https://lkubaski.wordpress.com/2012/12/20/java-se-application-design-with-mvc-with-images/) – trashgod Feb 25 '16 at 11:56