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.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import javax.swing.JCheckBox; import javax.swing.JCheckBox;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener; import javax.swing.event.ChangeListener;
import Logica.Gestion; import Logica.Gestion;
import Logica.Transaccion;
public class Controlador implements ActionListener,ChangeListener{ public class Controlador implements ActionListener,ChangeListener{
@ -18,7 +16,7 @@ public class Controlador implements ActionListener,ChangeListener{
this.vista=vista; this.vista=vista;
this.aniadirListeners(); this.aniadirListeners();
} }
@Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(this.vista.boton.getActionCommand())) { if(e.getActionCommand().equals(this.vista.boton.getActionCommand())) {
String nombre=JOptionPane.showInputDialog("Introduce un nuevo gasto o ingreso"); String nombre=JOptionPane.showInputDialog("Introduce un nuevo gasto o ingreso");
@ -33,11 +31,10 @@ public class Controlador implements ActionListener,ChangeListener{
} }
try { 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.setText(String.valueOf(Gestion.getTotal()));
this.vista.menu.total.revalidate(); this.vista.menu.total.revalidate();
this.vista.menu.total.repaint(); this.vista.menu.total.repaint();
this.aniadirListeners();
}catch (NumberFormatException ex) { }catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Debe introducir un numero", "error", JOptionPane.WARNING_MESSAGE); 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(); JCheckBox pulsado=(JCheckBox)e.getSource();
for(int i=0;i<vista.gestiones.getElementos().size();i++) { for(int i=0;i<vista.gestiones.getElementos().size();i++) {
if(vista.gestiones.getElementos().get(i).toString().equals(pulsado.getText())) { 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.setText(String.valueOf(Gestion.getTotal()));
this.vista.menu.total.revalidate(); this.vista.menu.total.revalidate();
this.vista.menu.total.repaint(); 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 @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub // 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 Vista gastos;
protected JTextArea total; protected JTextArea total;
public Menu() { public Menu() {
init();
}
private void init() {
this.setLayout(new GridBagLayout()); this.setLayout(new GridBagLayout());
GridBagConstraints constrain=new GridBagConstraints(); GridBagConstraints constrain=new GridBagConstraints();
this.ingresos=new Vista(this); this.ingresos=new Vista(this);

View File

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