Originally Posted by
pbrockway2
If you want do(Other)StuffWithTextFields() to be methods that are separated out from MainWindow consider making a class that contains the text fields in question. That class could expose do(Other)StuffWithTextFields() as public behaviour. And MainWindow could listen for events and call those methods as appropriate.
Thanks for the tips!
Do you mean "Provide the textfields from another class into the GUI"?
Honestly, I still cannot think a way of not using getters and setters. I am not against them and I do not dislike using them.
The thing is that, I use WindowBuilder Pro(or something like that) to build my GUI, then I fix it if it is needed and add my own code. So, I generally would be stuck with a very big constructor (That is how WindowBuilder plugin works), which includes everything and I do not know but I think this is inefficient. Of course I write many methods outside of the constructor
And my question actually just about implementing a seperate listener.
I hope this is one of those issues which you are stuck at the moment but will figure out easily if you stop thinking about for a while. Because I am out of ideas. Here are some;
One idea I came up with is, passing everything I need to manipulate to the listener. I mean, the button, the text boxes and any other stuff I need when the particular button is clicked. But I think that would complicate everything, and this should make my life easier.
The other thing was to pass the button which is clicked. I'd have a Object variable, and if it will be equal to the button clicked. Then again, this will end up being the previous idea, because my text fields are private too.
I asked similar question in an Android tutorial video series in udemy. I could not understand what exactly the man meant, but he gave me another idea, if he did not mean this. The idea is, I create a type of collection variable (he said HashMap I guess), I add all of my buttons and variables to be manipulated through the listener, and pass that hashmap to the listener. Of course, the rest would be easy.
It could go like this:
HashMap<String,Object> hm = new HashMap<String,Object>();
hm.put("myKey", myButton);
//Put other stuff too
//Pass it to listener and get myButton using this:
hm.get("myKey");
//So I use if statements:
if(event.getSource() == hm.get("myKey"){
}
And of course, if I have tons of variables in the GUI class, I should just make anonymous or at least inner classes, but HashMap sounds nice.