Pensavo ci fosse qualche motivo risaputo, ovviamente errori.
Comunque posto il codice di tutto:
Launcher:
package progetto.centroSportivo.business;
import progetto.centroSportivo.dao.*;
import progetto.centroSportivo.dbConnection.DbConnection;
import progetto.centroSportivo.model.*;
import progetto.centroSportivo.view.*;
import java.util.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Launcher {
public static void main(String[] args) {
HomeFrame home = new HomeFrame("Centro Sportivo");
home.setVisible(true);
}
}
Il Frame:
package progetto.centroSportivo.view;
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import progetto.centroSportivo.model.*;
import progetto.centroSportivo.actionListener.LoginListener;
import progetto.centroSportivo.dao.*;
public class HomeFrame extends JFrame{
JPanel northPanel = new JPanel();
JPanel welcomePanel = new JPanel();
private JTextField emailTxt;
private JPasswordField passwordTxt;
private JButton loginButton;
private JButton signupButton;
private JTable namesDisciplina;
private JScrollPane scrollPane;
public JButton getLoginButton()
{
return loginButton;
}
public JButton getSignupButton()
{
return signupButton;
}
public JPanel getNorthPanel()
{
return northPanel;
}
public JTextField getEmailTxt()
{
return emailTxt;
}
public JPasswordField getPasswordTxt()
{
return passwordTxt;
}
public JTable getNamesDisciplina()
{
return namesDisciplina;
}
public JScrollPane getScrollPane()
{
return scrollPane;
}
public JPanel getWelcomePanel()
{
return welcomePanel;
}
public HomeFrame(String title) throws HeadlessException
{
super(title);
setSize(800,500);
setLocation(400, 50);
setDefaultCloseOperation(EXIT_ON_CLOSE);
loginButton = new JButton("Accedi");
signupButton = new JButton("Registrati");
northPanel.add(new JLabel("E-mail: "));
emailTxt = new JTextField(10);
northPanel.add(emailTxt);
northPanel.add(new JLabel("Password: "));
passwordTxt = new JPasswordField(10);
northPanel.add(passwordTxt);
northPanel.add(loginButton);
northPanel.add(signupButton);
getContentPane().add(northPanel, BorderLayout.NORTH);
getContentPane().add(welcomePanel);
/* visualizzo le discipline presenti nel db in una JTable */
ArrayList<Disciplina> listNamesDiscipline = new ArrayList<Disciplina>();
listNamesDiscipline = DisciplinaDAO.showNamesDiscipline();
TableNamesDiscipline table = new TableNamesDiscipline(listNamesDiscipline);
namesDisciplina = new JTable(table);
namesDisciplina.setShowGrid(false);
scrollPane = new JScrollPane(namesDisciplina);
getContentPane().add(scrollPane, BorderLayout.SOUTH);
LoginListener login = new LoginListener(this);
loginButton.addActionListener(login);
}
}
E questo è l'ActionListener:
package progetto.centroSportivo.actionListener;
import java.awt.event.*;
import javax.swing.JLabel;
import progetto.centroSportivo.dao.*;
import progetto.centroSportivo.model.Tesserato;
import progetto.centroSportivo.view.HomeFrame;
public class LoginListener implements ActionListener{
private HomeFrame frame;
public LoginListener(HomeFrame frame)
{
this.frame = frame;
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == frame.getLoginButton())
{
String email = frame.getEmailTxt().getText();
String password = new String(frame.getPasswordTxt().getPassword());
TesseratoDAO t = new TesseratoDAO();
boolean i = t.isRegistered(email, password);
String name = t.findNameByEmailAndPass(email, password);
JLabel welcomeMsg;
if(i = true)
{
if(name != null)
{
welcomeMsg = new JLabel("Benvenuto "+name+"!");
frame.getWelcomePanel().add(welcomeMsg);
frame.getEmailTxt().setText("");
frame.getPasswordTxt().setText("");
}
else
{
welcomeMsg = new JLabel("L'email e/o la password non sono corretti!");
frame.getWelcomePanel().add(welcomeMsg);
}
}
}
}
}
Questi i metodi che uso nell'ActionPerformed:
public boolean isRegistered(String email, String password)
{
boolean isRegistered = true;
ArrayList<String[]> result = DbConnection.getInstance().eseguiQuery("SELECT * FROM tesserato WHERE email='"+email+"' AND pass= '"+password+"' ");
if(result.size() == 0) isRegistered = false;
return isRegistered;
}
public String findNameByEmailAndPass(String email, String password)
{
Tesserato t = new Tesserato();
ArrayList<String[]> result = DbConnection.getInstance().eseguiQuery("SELECT tesserato.nome FROM tesserato WHERE email='"+email+"' AND pass= '"+password+"' ");
if(result.size() == 0) return null;
String[] riga = result.get(0);
t.setNome(riga[0]);
return t.getNome();
}