Java tutorial
/* * 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. */ package com.firmansyah.imam.sewa.kendaraan; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; import org.apache.commons.codec.digest.DigestUtils; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONValue; import org.json.simple.parser.ParseException; /** * * @author Imam Firmansyah */ public class FormUser extends javax.swing.JFrame { /** * Creates new form FormPelanggan */ public FormUser() { initComponents(); inputIdUser.setEditable(false); btnUbah.setEnabled(false); btnHapus.setEnabled(false); btnAktif.setEnabled(false); } /** * 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. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jScrollPane2 = new javax.swing.JScrollPane(); tblPelanggan = new javax.swing.JTable(); labelIdUser = new javax.swing.JLabel(); labelNama = new javax.swing.JLabel(); labelUsername = new javax.swing.JLabel(); inputIdUser = new javax.swing.JTextField(); inputNama = new javax.swing.JTextField(); btnTambah = new javax.swing.JButton(); btnUbah = new javax.swing.JButton(); btnHapus = new javax.swing.JButton(); jSeparator1 = new javax.swing.JSeparator(); btnFormSewa = new javax.swing.JButton(); btnKeluar = new javax.swing.JButton(); inputUsername = new javax.swing.JTextField(); labelPassword = new javax.swing.JLabel(); inputPassword = new javax.swing.JPasswordField(); btnAktif = new javax.swing.JButton(); btnRefresh = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Form Pelaggan"); addWindowListener(new java.awt.event.WindowAdapter() { public void windowActivated(java.awt.event.WindowEvent evt) { formWindowActivated(evt); } }); tblPelanggan.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "No", "Nama", "Username", "Status", "ID User" })); tblPelanggan.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { tblPelangganMouseClicked(evt); } }); jScrollPane2.setViewportView(tblPelanggan); labelIdUser.setText("ID User"); labelNama.setText("Nama"); labelUsername.setText("Username"); btnTambah.setText("Tambah"); btnTambah.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnTambahActionPerformed(evt); } }); btnUbah.setText("Ubah"); btnUbah.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnUbahActionPerformed(evt); } }); btnHapus.setText("Hapus"); btnHapus.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnHapusActionPerformed(evt); } }); btnFormSewa.setText("Form Sewa"); btnFormSewa.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnFormSewaActionPerformed(evt); } }); btnKeluar.setText("Keluar"); btnKeluar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnKeluarActionPerformed(evt); } }); labelPassword.setText("Password"); btnAktif.setText("Non Akifkan"); btnAktif.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAktifActionPerformed(evt); } }); btnRefresh.setText("Refresh"); btnRefresh.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnRefreshActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 650, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(labelIdUser).addComponent(labelNama) .addComponent(labelUsername).addComponent(labelPassword)) .addGap(40, 40, 40) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(inputPassword, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(inputUsername, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(inputIdUser, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(inputNama, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addComponent(btnTambah) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnUbah) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnHapus) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnRefresh) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnAktif))) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addComponent(btnFormSewa, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnKeluar))) .addContainerGap())); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { btnHapus, btnRefresh, btnTambah, btnUbah }); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { btnFormSewa, btnKeluar }); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(23, 23, 23) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(labelIdUser).addComponent(inputIdUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(labelNama).addComponent(inputNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(labelUsername).addComponent(inputUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(labelPassword).addComponent(inputPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnTambah).addComponent(btnUbah).addComponent(btnHapus) .addComponent(btnAktif).addComponent(btnRefresh)) .addGap(18, 18, 18) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnFormSewa).addComponent(btnKeluar)) .addContainerGap(20, Short.MAX_VALUE))); setSize(new java.awt.Dimension(686, 467)); setLocationRelativeTo(null); }// </editor-fold>//GEN-END:initComponents private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated // munculkan data kendaraan showDataUser(); }//GEN-LAST:event_formWindowActivated private void tblPelangganMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblPelangganMouseClicked int row = tblPelanggan.getSelectedRow(); String tblUserID = (tblPelanggan.getModel().getValueAt(row, 4).toString()); System.out.println("ID User : " + tblUserID); try { showDataUserById(Integer.parseInt(tblUserID)); } catch (ParseException ex) { Logger.getLogger(FormUser.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_tblPelangganMouseClicked private void btnFormSewaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFormSewaActionPerformed this.dispose(); System.out.println("Panggil Form Sewa"); // memanggil form Sewa FormSewa callForm = new FormSewa(); callForm.setLocationRelativeTo(null); callForm.setVisible(true); }//GEN-LAST:event_btnFormSewaActionPerformed private void btnKeluarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnKeluarActionPerformed int dialogButton = JOptionPane.YES_NO_OPTION; int dialogResult; dialogResult = JOptionPane.showConfirmDialog(this, "Anda Yakin Ingin Keluar? ", "Konfirmasi", dialogButton); if (dialogResult == 0) { System.out.println("Quit"); this.dispose(); } else { System.out.println("cancel"); } }//GEN-LAST:event_btnKeluarActionPerformed private void btnTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTambahActionPerformed String nama = inputNama.getText(); String nama_filter = nama.replaceAll(" ", "%20"); String username = inputUsername.getText(); String password = inputPassword.getText(); String password_hash = DigestUtils.md5Hex(password); System.out.println("Nama : " + nama); System.out.println("Username : " + username); System.out.println("Password : " + password); if (nama.isEmpty() || username.isEmpty() || password.isEmpty()) { JOptionPane.showMessageDialog(this, "Data Tidak Boleh Kosong", "Informasi", JOptionPane.ERROR_MESSAGE); } else if (username.contains(" ")) { JOptionPane.showMessageDialog(this, "Username Tidak Boleh Menggunakan Spasi", "Informasi", JOptionPane.ERROR_MESSAGE); } else { String url = Path.serverURL + "/user/create/" + username + "/" + password_hash + "/" + nama_filter; getDataURL dataurl = new getDataURL(); try { String data = dataurl.getData(url); System.out.println(data); if (data.equals("1")) { JOptionPane.showMessageDialog(this, "Registrasi Berhasil,\nSelamat Begabung " + nama, "Informasi", JOptionPane.INFORMATION_MESSAGE); cleanFormUser(); } else { JOptionPane.showMessageDialog(this, "Registrasi Gagal\nUsername Sudah digunakan", "Informasi", JOptionPane.ERROR_MESSAGE); } } catch (IOException ex) { Logger.getLogger(FormRegister.class.getName()).log(Level.SEVERE, null, ex); } } }//GEN-LAST:event_btnTambahActionPerformed private void btnUbahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUbahActionPerformed String id = inputIdUser.getText(); String nama = inputNama.getText(); String nama_filter = nama.replaceAll(" ", "%20"); String username = inputUsername.getText(); String password = inputPassword.getText(); String password_hash = DigestUtils.md5Hex(password); System.out.println("Nama : " + nama); System.out.println("Username : " + username); System.out.println("Password : " + password); if (nama.isEmpty() || username.isEmpty()) { JOptionPane.showMessageDialog(this, "Nama dan Username Tidak Boleh Kosong", "Informasi", JOptionPane.ERROR_MESSAGE); } else if (username.contains(" ")) { JOptionPane.showMessageDialog(this, "Username Tidak Boleh Menggunakan Spasi", "Informasi", JOptionPane.ERROR_MESSAGE); } else { try { String url; if (password.isEmpty()) { url = Path.serverURL + "/user/update/" + id + "/" + nama_filter + "/" + username; } else { url = Path.serverURL + "/user/update/" + id + "/" + nama_filter + "/" + username + "/" + password_hash; } getDataURL dataurl = new getDataURL(); String data = dataurl.getData(url); System.out.println(data); if (data.equals("1")) { JOptionPane.showMessageDialog(this, "Data Berhasil diubah", "Informasi", JOptionPane.INFORMATION_MESSAGE); // refresh form refreshForm(); } else { JOptionPane.showMessageDialog(this, "Data Tidak Berhasil diubah", "Informasi", JOptionPane.ERROR_MESSAGE); } } catch (IOException ex) { Logger.getLogger(FormUser.class.getName()).log(Level.SEVERE, null, ex); } } }//GEN-LAST:event_btnUbahActionPerformed private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHapusActionPerformed String id = inputIdUser.getText(); System.out.println("Memilih ID : " + id); int dialogButton = JOptionPane.YES_NO_OPTION; int dialogResult; dialogResult = JOptionPane.showConfirmDialog(this, "Anda yakin Menghapus Data ini? ", "Konfirmasi", dialogButton); if (dialogResult == 0) { String url = Path.serverURL + "/user/delete/" + id; getDataURL dataurl = new getDataURL(); try { String data = dataurl.getData(url); System.out.println(data); if (data.equals("1")) { JOptionPane.showMessageDialog(this, "Data Berhasil dihapus", "Informasi", JOptionPane.INFORMATION_MESSAGE); System.out.println("Menghapus Data ID : " + id); } else { JOptionPane.showMessageDialog(this, "Data Tidak Berhasil dihapus", "Informasi", JOptionPane.ERROR_MESSAGE); } } catch (IOException ex) { Logger.getLogger(FormUser.class.getName()).log(Level.SEVERE, null, ex); JOptionPane.showMessageDialog(this, "Data Tidak Bisa dihapus\nKarena Memiliki Relasi dengan Data Lainnya", "Informasi", JOptionPane.ERROR_MESSAGE); } } else { System.out.println("Tidak Menghapus : " + id); } // refresh form refreshForm(); }//GEN-LAST:event_btnHapusActionPerformed private void btnAktifActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAktifActionPerformed String status = btnAktif.getText(); String id = inputIdUser.getText(); System.out.println("Memilih ID : " + id); if (status.equals("Aktifkan")) { status = "1"; } else { status = "0"; } int dialogButton = JOptionPane.YES_NO_OPTION; int dialogResult; dialogResult = JOptionPane.showConfirmDialog(this, "Anda yakin Ingin Mengubah Status User ini? ", "Konfirmasi", dialogButton); if (dialogResult == 0) { String url = Path.serverURL + "/user/status/" + id + "/" + status; getDataURL dataurl = new getDataURL(); try { String data = dataurl.getData(url); System.out.println(data); if (data.equals("1")) { JOptionPane.showMessageDialog(this, "Status Berhasil diubah", "Informasi", JOptionPane.INFORMATION_MESSAGE); System.out.println("Mengubah Status Data ID : " + id); } else { JOptionPane.showMessageDialog(this, "Status Tidak Berhasil diubah", "Informasi", JOptionPane.ERROR_MESSAGE); } } catch (IOException ex) { Logger.getLogger(FormUser.class.getName()).log(Level.SEVERE, null, ex); } } else { System.out.println("Tidak Mengubah Status dari ID : " + id); } // refresh form refreshForm(); }//GEN-LAST:event_btnAktifActionPerformed private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed refreshForm(); }//GEN-LAST:event_btnRefreshActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(FormUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(FormUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(FormUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(FormUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new FormUser().setVisible(true); } }); } // function munculkan data kendaraan public void showDataUser() { try { getDataURL dataurl = new getDataURL(); DefaultTableModel model = (DefaultTableModel) tblPelanggan.getModel(); model.setRowCount(0); String url = Path.serverURL + "/user/show/"; String data = dataurl.getData(url); Object obj = JSONValue.parse(data); JSONArray dataArray = (JSONArray) obj; System.out.println("Banyak datanya : " + dataArray.size()); for (int i = 0; i < dataArray.size(); i++) { JSONObject getData = (JSONObject) dataArray.get(i); String cek_status = getData.get("status").toString(); if (cek_status.equals("0")) { cek_status = "Non Aktif"; } else { cek_status = "Aktif"; } Object[] row = { i + 1, getData.get("nama"), getData.get("username"), cek_status, getData.get("id"), }; model.addRow(row); } } catch (IOException ex) { Logger.getLogger(FormUser.class.getName()).log(Level.SEVERE, null, ex); } } // function munculkan data kendaraan by id public void showDataUserById(int id) throws ParseException { try { getDataURL dataurl = new getDataURL(); String url = Path.serverURL + "/user/show/" + id; String data = dataurl.getData(url); Object obj = JSONValue.parse(data); JSONArray dataArray = (JSONArray) obj; JSONObject getData = (JSONObject) dataArray.get(0); Object nama = getData.get("nama"); Object username = getData.get("username"); Object id_pelanggan = getData.get("id"); Object status = getData.get("status"); if (status.equals("0")) { btnAktif.setText("Aktifkan"); } else { btnAktif.setText("Non Aktifkan"); } inputNama.setText((String) nama); inputUsername.setText((String) username); inputIdUser.setText((String) id_pelanggan.toString()); btnTambah.setEnabled(false); btnUbah.setEnabled(true); btnHapus.setEnabled(true); btnAktif.setEnabled(true); } catch (IOException ex) { Logger.getLogger(FormUser.class.getName()).log(Level.SEVERE, null, ex); } } // function clean Form public void cleanFormUser() { inputIdUser.setText(""); inputNama.setText(""); inputPassword.setText(""); inputUsername.setText(""); } public void refreshForm() { showDataUser(); cleanFormUser(); btnTambah.setEnabled(true); btnUbah.setEnabled(false); btnHapus.setEnabled(false); btnAktif.setEnabled(false); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnAktif; private javax.swing.JButton btnFormSewa; private javax.swing.JButton btnHapus; private javax.swing.JButton btnKeluar; private javax.swing.JButton btnRefresh; private javax.swing.JButton btnTambah; private javax.swing.JButton btnUbah; private javax.swing.JTextField inputIdUser; private javax.swing.JTextField inputNama; private javax.swing.JPasswordField inputPassword; private javax.swing.JTextField inputUsername; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JSeparator jSeparator1; private javax.swing.JLabel labelIdUser; private javax.swing.JLabel labelNama; private javax.swing.JLabel labelPassword; private javax.swing.JLabel labelUsername; private javax.swing.JTable tblPelanggan; // End of variables declaration//GEN-END:variables }