Java tutorial
package com.cactus; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JTextField; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.*; import org.apache.http.util.EntityUtils; import org.json.JSONObject; import javax.swing.GroupLayout.Alignment; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.GroupLayout; import javax.swing.LayoutStyle.ComponentPlacement; import javax.swing.WindowConstants; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author imdak_000 */ public class ClientLoginGUI extends JFrame { // Variables declaration - do not modify//GEN-BEGIN:variables private JButton Login_Button; private JButton Register_Button; private JLabel Username_Label; private JLabel Password_label; private JTextField Username_TextField; private JPasswordField Password_TextField; // End of variables declaration//GEN-END:variables /** * Creates new form ClientGUI */ public ClientLoginGUI() { setResizable(false); initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { this.pack(); this.setLocationRelativeTo(null); Username_TextField = new JTextField(); Password_TextField = new JPasswordField(); Username_Label = new JLabel(); Password_label = new JLabel(); Login_Button = new JButton(); Register_Button = new JButton(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Username_TextField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { Username_TextFieldActionPerformed(evt); } }); Password_TextField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { Password_TextFieldActionPerformed(evt); } }); Username_Label.setText("Username:"); Password_label.setText("Password:"); Login_Button.setText("Login"); Login_Button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try { Login_ButtonActionPerformed(evt); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); Register_Button.setText("Register"); Register_Button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try { Register_ButtonActionPerformed(evt); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); javax.swing.GroupLayout layout = new GroupLayout(getContentPane()); layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(layout .createSequentialGroup().addGap(101) .addGroup(layout.createParallelGroup(Alignment.LEADING, false).addComponent(Password_label) .addComponent(Username_TextField) .addComponent(Password_TextField, GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addComponent(Register_Button) .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(Login_Button)) .addComponent(Username_Label, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)) .addContainerGap(128, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(71).addComponent(Username_Label) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(Username_TextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGap(27).addComponent(Password_label).addPreferredGap(ComponentPlacement.RELATED) .addComponent(Password_TextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGap(33).addGroup(layout.createParallelGroup(Alignment.BASELINE) .addComponent(Register_Button).addComponent(Login_Button)) .addContainerGap(48, Short.MAX_VALUE))); getContentPane().setLayout(layout); pack(); }// </editor-fold>//GEN-END:initComponents private void Username_TextFieldActionPerformed(ActionEvent evt) {//GEN-FIRST:event_Username_TextFieldActionPerformed }//GEN-LAST:event_Username_TextFieldActionPerformed private void Password_TextFieldActionPerformed(ActionEvent evt) {//GEN-FIRST:event_Password_TextFieldActionPerformed // TODO add your handling code here: }//GEN-LAST:event_Password_TextFieldActionPerformed //Action listener for "login" button private void Login_ButtonActionPerformed(ActionEvent evt) throws ClientProtocolException, IOException, Exception {//GEN-FIRST:event_Login_ButtonActionPerformed String userName = Username_TextField.getText(); String userPass = String.valueOf(Password_TextField.getPassword()); //error checks to see if these fields are empty. //turning the parameters into a JSON object that is sent to the server to process CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("https://teamcactus.us/Login.php"); String json = "{\"userName\":\"" + userName + "\",\"userPass\":\"" + userPass + "\"}"; StringEntity entity = new StringEntity(json); httpPost.setEntity(entity); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); HttpResponse response = client.execute(httpPost); // Getting the status code. //int statusCode = response.getStatusLine().getStatusCode(); // Getting the response body. String responseBody = EntityUtils.toString(response.getEntity()); JSONObject obj = new JSONObject(responseBody); String status = obj.getString("status"); //if successful stores jwt into the user object and goes to the homepage for the chat program if (status.equals("success")) { String jwt = obj.getString("jwt"); User user = new User(userName, userPass, jwt); this.dispose(); new ClientHomeGUI(user).setVisible(true); } //display various errors if there was a problem trying to log in. else { String msg = obj.getString("msg"); JOptionPane.showMessageDialog(null, msg, "Error", JOptionPane.ERROR_MESSAGE); } }//GEN-LAST:event_Login_ButtonActionPerformed //"Register" button sends user to the register screen to create a new user. private void Register_ButtonActionPerformed(ActionEvent evt) throws Exception {//GEN-FIRST:event_Register_ButtonActionPerformed this.dispose(); new ClientRegisterGUI().setVisible(true); }//GEN-LAST:event_Register_ButtonActionPerformed }