Estou tentando fazer um modelo MVC porém estou com problemas em como pegar o que é digitado pelo usuario e adicionar um evento ao meu bottão pelo meu controller:
minha view:
public class ViewLogin extends JFrame { private Container con; private static final int FRAME_WIDTH = 400; static final int FRAME_HEIGHT = 350; static final int FRAME_X_ORIGIN = 150; static final int FRAME_Y_ORIGIN = 150; static final int BUTTON_WIDTH = 90; static final int BUTTON_HEIGHT = 30; private JPanel Logo; private JPanel Items; private JPanel Footer; private JLabel label1,label2; private JTextField login; private JPasswordField password; private JButton entrar; private JButton registrar; /** * Create the frame. */ public ViewLogin() { con=getContentPane(); //Set the frame properties setSize (FRAME_WIDTH, FRAME_HEIGHT); // setResizable (false); setTitle ("market v1"); setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); setLogin(new JTextField("",10)); setPassword(new JPasswordField("",10)); Items = new JPanel(new GridLayout(3,1)); setEntrar(new JButton("Entrar")); getEntrar().setBounds(100, 285, BUTTON_WIDTH, BUTTON_HEIGHT); registrar = new JButton("Registrar"); registrar.setBounds(195, 285, BUTTON_WIDTH, BUTTON_HEIGHT); label1 = new JLabel(); label1.setText("Username:"); label2 = new JLabel(); label2.setText("Password:"); Items.add(label1); Items.add(getLogin()); Items.add(label2); Items.add(getPassword()); Items.add(getEntrar()); Items.add(registrar); add(Items,BorderLayout.CENTER); setVisible(true); } public JTextField getLogin() { return login; } public void setLogin(JTextField login) { this.login = login; } public JPasswordField getPassword() { return password; } public void setPassword(JPasswordField password) { this.password = password; } public JButton getEntrar() { return entrar; } public void setEntrar(JButton entrar) { this.entrar = entrar; } public void adicionarOuvinteLogin (ActionListener ouvinte) { this.entrar.addActionListener(ouvinte); this.registrar.addActionListener(ouvinte); } }
meu controller:
public class ControllerLogin { //view ViewLogin loginView; FuncionarioDAO funcionario; public ControllerLogin() { loginView = new ViewLogin(); } // getters and setters do controller public ViewLogin getLoginView() { return loginView; } public void setLoginView(ViewLogin loginView) { this.loginView = loginView; } }
eu queria chamar o meu metodo de validação pelo o meu controller pegando os dados da minha view:
meu metodo:
public boolean Autenticar(String usuario, String senha) { PreparedStatement ps = null; ResultSet rs = null; String stringAutenticar = "SELECT * from usuario u WHERE u.login = ?"; try { ps = con.prepareStatement(stringAutenticar); ps.setString(1, usuario); rs = ps.executeQuery(); if (rs.next()) { if (BCrypt.checkpw(senha, rs.getString("senha"))) { JOptionPane.showMessageDialog(null,"Hello, Welcome to Javatpoint."); return true; } } return false; } catch (SQLException ex) { ex.printStackTrace(); return false; } finally { try { rs.close(); ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }