java - How to avoid nested ActionListeners? -
in program, want user to:
- pick/open database (like access) on own
- pick table database
- select column(s) table
in code, have class this:
mntmopendatabase.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { //open database //display tables buttons tablebutton.addactionlistener(new actionlistener() { // select table public void actionperformed(actionevent e) { //display columns of table selected buttons colbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) {// add list of columns exported }
and results big block of code. there cleaner, simpler way this?
the solution refactor:
- create separate , separately testable class code open database.
- and separate , separately testable class display of data.
- in actionlistener, either create instances of these classes, or interact them, if exist.
- learn basic principles of m-v-c (model-view-control) design pattern, , use them. don't have slavish them, , lord-knows there many variants, overall guiding principles should @ least respected.
- strive make gui or view dumb possible. knows how display data, has facilities allow control update display, , knows how notify control when user interacts it, , that's it.
- side recommendation 1: sure database interaction done in background thread.
- side recommendation 2: sure swing interaction done in on swing edt (the event-dispatch thread).
please @ similar more complete question , answer: how can 1 best avoid writing bloated gui code?. answers gets, , wish up-vote them gazillion times.
for example, code above simple as:
mntmopendatabase.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { control.opendatabase(); } }
Comments
Post a Comment