The moose likes Beginning Java and the fly likes calling a method and changing it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calling a method and changing it" Watch "calling a method and changing it" New topic

calling a method and changing it

Nick de Waal
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
Hi all,

I have a method that clears all text fields of a particular GUI
here it is:

simple I know but I am a beginner and I thought I would write this to save me from writing it in different actionPerformed methods where I need the same result,
so I could just call this one.
Now for my question: Is it possible to call this method and somehow manipulate it to clear all text except for the first two?
Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

Depending on your specific needs, both now and what you might need in the future, some options are:

1) Pass a boolean indicating whether to clear those first 2 fields or not. The code to clear them goes in an if statement that checks that boolean.

2) Pass in an explicit list of names of fields to clear. (Or not to clear, if that's smaller/rarer.)

3) Break this method into two methods. One to clear all but the first 2, and the other that clears the first 2 fields and then calls the other method.

And various variations on the above.
Nick de Waal
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
Thank you Jeff,

I chose the 3rd option as I found it placed the least exertion on my brain
but I have copied the other 2 options to try out when I have a little more time.

And it works well for what I need.
I agree. Here's the link:
subject: calling a method and changing it
It's not a secret anymore!