mardi 16 avril 2019

Add Buttons to JTable adhering to Model View Controller design pattern

I'm trying to write a Java table gui, and I'm attempting to follow the Model View Controller design pattern and Dependency Inversion principle, as described here:

https://en.wikipedia.org/wiki/Dependency_inversion_principle

The trouble is, I want to imbed a clickable button within the table. I've found a few examples to do this, like in the link below:

http://esus.com/jbutton-cell-jtable/

The basic code looks like this:

class StrategyTableModel extends AbstractTableModel {
  this.data = new Object[rows][COLUMN_NAMES.length];

  @Override
  public Object getValueAt(int rowIndex, int columnIndex) {
    switch (columnIndex) {
    case 1: final JButton button = new Jbutton();
            return button;
    default: return data[rowIndex][columnIndex];
    }
  }

Now, in my interpretation of the Model View Controller design pattern, I've now embedded a View object (the button) into the Model object, and I'll need to attach an action listener connecting the controller to the button. So Model View and Controller all become interdependent if I follow this approach. But, I can't think of a simple way to get around it. Is there a better approach?

Aucun commentaire:

Enregistrer un commentaire