Corrigiendo bugs listeners

This commit is contained in:
Guillermo Roche 2019-11-11 22:31:10 +01:00
parent b660d8d551
commit 95805c3efa
3 changed files with 21 additions and 29 deletions

View File

@ -2,14 +2,12 @@ package VistaControlador;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import Logica.Gestion;
import Logica.Transaccion;
public class Controlador implements ActionListener,ChangeListener{
@ -18,7 +16,7 @@ public class Controlador implements ActionListener,ChangeListener{
this.vista=vista;
this.aniadirListeners();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(this.vista.boton.getActionCommand())) {
String nombre=JOptionPane.showInputDialog("Introduce un nuevo gasto o ingreso");
@ -33,11 +31,10 @@ public class Controlador implements ActionListener,ChangeListener{
}
try {
this.vista.aniadirElemento(nombre, Integer.parseInt(dinero));
this.vista.aniadirElemento(nombre, Integer.parseInt(dinero),this);
this.vista.menu.total.setText(String.valueOf(Gestion.getTotal()));
this.vista.menu.total.revalidate();
this.vista.menu.total.repaint();
this.aniadirListeners();
}catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Debe introducir un numero", "error", JOptionPane.WARNING_MESSAGE);
}
@ -45,33 +42,31 @@ public class Controlador implements ActionListener,ChangeListener{
JCheckBox pulsado=(JCheckBox)e.getSource();
for(int i=0;i<vista.gestiones.getElementos().size();i++) {
if(vista.gestiones.getElementos().get(i).toString().equals(pulsado.getText())) {
vista.gestiones.alterarVisibilidad(!vista.gestiones.getElementos().get(i).getVisivilidad(), i);
//vista.gestiones.alterarVisibilidad(!vista.gestiones.getElementos().get(i).getVisivilidad(), i);
vista.gestiones.alterarVisibilidad(pulsado.getFocusTraversalKeysEnabled(), i);
this.vista.menu.total.setText(String.valueOf(Gestion.getTotal()));
this.vista.menu.total.revalidate();
this.vista.menu.total.repaint();
}
}
}
}
private void aniadirListeners() {
vista.boton.addActionListener(this);
vista.boton.setActionCommand("Aniadir");
int contador=0;
for(JCheckBox check:this.vista.transacciones) {
check.addActionListener(this);
check.setActionCommand("pulsar "+contador);
contador++;
}
}
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
JCheckBox pulsado=(JCheckBox)e.getSource();
for(Transaccion transaaccio:vista.gestiones.getElementos()) {
if(transaaccio.toString().equals(pulsado.getText())) {
transaaccio.alterarVisivilidad(false);
}
}
}
private void aniadirListeners() {
vista.boton.addActionListener(this);
for(JCheckBox check:this.vista.transacciones) {
check.addActionListener(this);
}
}
}

View File

@ -19,10 +19,6 @@ public class Menu extends JFrame{
protected Vista gastos;
protected JTextArea total;
public Menu() {
init();
}
private void init() {
this.setLayout(new GridBagLayout());
GridBagConstraints constrain=new GridBagConstraints();
this.ingresos=new Vista(this);

View File

@ -13,6 +13,7 @@ import Logica.*;
public class Vista extends JPanel{
private int x,y;
private static final int altocheck=28;
protected Gestion gestiones;
protected JButton boton;
protected LinkedList<JCheckBox> transacciones;
@ -20,7 +21,6 @@ public class Vista extends JPanel{
JScrollPane panel;
Menu menu;
public Vista(Menu menu) {
//this.setLayout(new );
this.transacciones=new LinkedList<JCheckBox>();
this.menu=menu;
this.x=100;
@ -30,22 +30,23 @@ public class Vista extends JPanel{
this.cuadro=new JPanel();
this.panel=new JScrollPane(cuadro);
this.panel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
this.panel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.panel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
cuadro.setPreferredSize(new Dimension(x, y));
panel.setPreferredSize(new Dimension(100,200));
panel.setVisible(true);
this.add(panel);
this.aniadirElemento("asdfa", 0);
}
public void aniadirElemento(String nombre, int dinero) {
public void aniadirElemento(String nombre, int dinero, Controlador controlador) {
Transaccion transaccion=new Transaccion(nombre, dinero);
this.gestiones.aniadirGasto(transaccion);
JCheckBox check=new JCheckBox(transaccion.toString());
check.setSelected(true);
check.setSize(new Dimension(x,Vista.altocheck));
check.addActionListener(controlador);
this.transacciones.add(check);
this.cuadro.add(check);
this.y+=28;
//System.out.println(this.y);
this.y+=Vista.altocheck;
cuadro.setPreferredSize(new Dimension(x, y));
this.revalidate();
this.repaint();