Is there a better way to pass many arguments to a controller's constructor and avoid too many arguments or initialization

179
January 29, 2018, at 3:09 PM

I read in one of the answers here in SO that Java Swing isn't fully in MVC pattern. I'm also new with using the MVC pattern and I code in Java Swing. I'm trying to find a solution or a way I can minimize the number of "this" keyword during initialization or assignment of values to fields.

For instance, say I have the ff. JPanel with many JComponents

//View
public class MyPanel extends JPanel{
private final JTextField1 jtf1;
private final JTextField2 jtf2;
....
..
private final JTextField30 jtf30;
    private void initControllers(){
        jtf1.addActionListener(new MyActionListener(jtf1,jtf2......jtf30));
    }
}
//Controller
public class MyActionListener implmements ActionListener{
private final JTextField1 jtf1;
private final JTextField2 jtf2;
....
..
private final JTextField30 jtf30;
    public MyActionListener(JTextField1 jtf1,JTextField2 jtf2,..JTextField30 jtf30){
        this.jtf1 = jtf1;
        this.jtf2 = jtf2;
        ........
        this.jtf30 = jtf30; //Does this mean I have to have 30 of "this" assignment?
    }
}

Or I thought maybe if I pass the entire JPanel view MyPanel and generate getters(), would be correct or better?

Like this:

public class MyActionListener implmements ActionListener{
private final JTextField1 jtf1;
private final JTextField2 jtf2;
....
..
private final JTextField30 jtf30;
    public MyActionListener(JPanel myPanel){
        this.jtf1 = myPanel.getJtf1;
        this.jtf2 = myPanel.getJtf2;
        ........
        this.jtf30 = myPanel.getJtf3; 
    }
}

What is the common practice of resolving tens of view components that you need reference? Can you give advice on how to correct this? I doesn't look appropriate to have too many arguments to constructors. I want to clean the code.

From how I understand what I read about MVC is that, the controller class can accept both Model and View as arguments in its constructor as in

public class MyController implements xListener{
private final Model model;
private final View view; 
    public MyController(Model m, View v){
       this.model = m; //to access getters or setters
       this.view = v; // to access getters or setters
    }
}

Making MyController dependent to Model m and View v, if that's correct.

I'd appreciate any suggestions

Thanks.

Rent Charter Buses Company
READ ALSO
Editing main method to test a method

Editing main method to test a method

I am trying to test my method belowThe code was provided and my task is to edit the main method so it will count the number of occurrence of the word in the file

208
Using setFill with a variable?

Using setFill with a variable?

I'm trying to pass a variable in Java, I need help getting it to work, I've included all relevant code below, there's threejava file's i'm working with

220
How to set AlertDialog button font color in Kotlin

How to set AlertDialog button font color in Kotlin

I know to setup font color in Java of AlertDialog button we can do like

527