0

After adding a listener to a second button, the first created button executes twice the same action:

public class ControladorTablaMaterial implements ActionListener {


 private VistaTablaMaterial vistaTablaMaterial;
    private JPanel jContentPane = null;
    private JScrollPane scrollPane = null;
    private JTable tablaMaterial;
    private JButton mostrarElementoButton;
    private JButton eliminarElementoButton;
    private ModeloTablaMaterial modeloTablaMaterial;


    public ControladorTablaMaterial(ArrayList<Material> coleccionMaterial, ActionListener listener) {
        String[] cabecera = {"Material", "Titulo"};
        this.vistaTablaMaterial = new VistaTablaMaterial(cabecera, coleccionMaterial);
        setupVistaTablaMAterial(listener);
    }

    private void setupVistaTablaMAterial(ActionListener listener) {
        this.scrollPane = vistaTablaMaterial.getScrollPane();
        this.tablaMaterial = vistaTablaMaterial.getTablaMaterial();
        this.modeloTablaMaterial = vistaTablaMaterial.getModeloTablaMaterial();
        this.mostrarElementoButton = vistaTablaMaterial.getMostrarElementoButton();
        this.eliminarElementoButton = vistaTablaMaterial.getMostrarElementoButton();
        this.initListeners(listener);
    }


    private void initListeners (ActionListener listener) {
        getMostrarElementoButton().addActionListener(listener);
        getEliminarElementoButton().addActionListener(listener);

    }

     @Override
    public void actionPerformed(ActionEvent e) {

    }

}

Everything works fine if I delete the line:

getEliminarElementoButton().addAtionListener(listener);

but of course I need that button to be listened to too.

Inside the listener class, in the actionPerformed(actionEvent e) method, I use the following code to differentiate both buttons:

if (e.getSource().equals(this.getControladorTablaMaterial().getMostrarElementoButton())) {

That seems to work fine except for this frame. Any guess?

Off topic: why isn't the code indentation working properly on Stackoverflow's editor?

DavidVV
  • 225
  • 1
  • 4
  • 12
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Jul 30 '16 at 14:53
  • This is a copy/paste error; voting to close as a problem caused by typo. – Sergey Kalinichenko Jul 30 '16 at 14:55
  • Take a look at the edit you've just undone to see why indentation is not working (long story short, it's because you are not doing it right). – Sergey Kalinichenko Jul 30 '16 at 14:56
  • @daskblinkenlight indentation fixed by selecting all lines but first and clicking on {}. Then selecting the last hanging "}" and clicking againg on {}. Now I know how it works and can fix it, but I'd call that a bug. – DavidVV Jul 30 '16 at 15:13

1 Answers1

1

The problem is in these lines:

this.mostrarElementoButton = vistaTablaMaterial.getMostrarElementoButton();
this.eliminarElementoButton = vistaTablaMaterial.getMostrarElementoButton();

That you are getting the same button for both.

Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68