Sabtu, 28 April 2012

Custom Database Wizard Access MySQL Java NetBeans (2)


Custom Database Wizard (2)

23. Kemudian hentikan dahulu program, kembali ke IDE NEtBeans, pada FContacts dari palette Swing Controls, tambahkan 3 buah jTextField, 3 buah jLabel, dan 9 buah jButton pada FContacts.


24. Buka Source Code milik FContacts dengan cara klik Tombol Source pada FContacts.java. Tambahkan Kode program berikut :


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    jTable1.setRowSelectionInterval(0, 0);
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
    if (jTable1.getSelectedRow() > 0) {
        jTable1.setRowSelectionInterval(jTable1.getSelectedRow() - 1, jTable1.getSelectedRow() - 1);
    }
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
    if (jTable1.getSelectedRow() < (jTable1.getRowCount() - 1)) {
        jTable1.setRowSelectionInterval(jTable1.getSelectedRow() + 1, jTable1.getSelectedRow() + 1);
    }
}//GEN-LAST:event_jButton3ActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
// TODO add your handling code here:
    jTable1.setRowSelectionInterval(jTable1.getRowCount() - 1, jTable1.getRowCount() - 1);
}//GEN-LAST:event_jButton4ActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
// TODO add your handling code here:
    //entityManager1.getTransaction().rollback();
    entityManager1.getTransaction().rollback();
    entityManager1.getTransaction().begin();
    java.util.Collection data = query1.getResultList();
    for (Object entity : data) {
        entityManager1.refresh(entity);
    }
    list1.clear();
    list1.addAll(data);
    jTable1.setRowSelectionInterval(0, 0);
}//GEN-LAST:event_jButton5ActionPerformed

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
// TODO add your handling code here:
    my.dbworld.Contacts c = new my.dbworld.Contacts();
    entityManager1.persist(c);
    list1.add(c);
    int row = list1.size() - 1;
    jTable1.setRowSelectionInterval(row, row);
    jTable1.scrollRectToVisible(jTable1.getCellRect(row, 0, true));

}//GEN-LAST:event_jButton6ActionPerformed

private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed
// TODO add your handling code here:
    System.exit(0);
}//GEN-LAST:event_jButton12ActionPerformed

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
// TODO add your handling code here:
    int[] selected = jTable1.getSelectedRows();
    List<my.dbworld.Contacts> toRemove = new ArrayList<my.dbworld.Contacts>(selected.length);
    for (int idx = 0; idx < selected.length; idx++) {
        my.dbworld.Contacts c = (my.dbworld.Contacts) list1.get(jTable1.convertRowIndexToModel(selected[idx]));
        toRemove.add(c);
        entityManager1.remove(c);
    }
    list1.removeAll(toRemove);
    jTable1.setRowSelectionInterval(0, 0);
}//GEN-LAST:event_jButton10ActionPerformed

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
// TODO add your handling code here:
    try {
        entityManager1.getTransaction().commit();
        entityManager1.getTransaction().begin();
    } catch (RollbackException rex) {
        rex.printStackTrace();
        entityManager1.getTransaction().begin();
        List<my.dbworld.Contacts> merged = new ArrayList<my.dbworld.Contacts>(list1.size());
        for (my.dbworld.Contacts c : list1) {
            merged.add(entityManager1.merge(c));
        }
        list1.clear();
        list1.addAll(merged);
    }
}//GEN-LAST:event_jButton8ActionPerformed

25. Kemudian klik Run untuk menjalankan aplikasi. Cobalah semua fiturnya.


Berikut ini adalah source code program selengkapnya :
/* Contacts.java
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package my.dbworld;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 *
 * @author Amijaya
 */
@Entity
@Table(name = "CONTACTS")
@NamedQueries({@NamedQuery(name = "Contacts.findByName", query = "SELECT c FROM Contacts c WHERE c.name = :name"), @NamedQuery(name = "Contacts.findByAddress", query = "SELECT c FROM Contacts c WHERE c.address = :address"), @NamedQuery(name = "Contacts.findByPhone", query = "SELECT c FROM Contacts c WHERE c.phone = :phone")})
public class Contacts implements Serializable {
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "NAME", nullable = false)
    private String name;
    @Column(name = "ADDRESS")
    private String address;
    @Column(name = "PHONE")
    private String phone;

    public Contacts() {
    }

    public Contacts(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        String oldName = this.name;
        this.name = name;
        changeSupport.firePropertyChange("name", oldName, name);
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        String oldAddress = this.address;
        this.address = address;
        changeSupport.firePropertyChange("address", oldAddress, address);
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        String oldPhone = this.phone;
        this.phone = phone;
        changeSupport.firePropertyChange("phone", oldPhone, phone);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (name != null ? name.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Contacts)) {
            return false;
        }
        Contacts other = (Contacts) object;
        if ((this.name == null && other.name != null) || (this.name != null && !this.name.equals(other.name))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "my.dbworld.Contacts[name=" + name + "]";
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }

}


/* ================================================
 * FContacts.java
 *
 * Created on October 19, 2008, 10:29 PM
 */
package my.dbworld;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.RollbackException;

/**
 *
 * @author  Amijaya
 */
public class FContacts extends javax.swing.JFrame {

    /** Creates new form FContacts */
    public FContacts() {
        initComponents();
        entityManager1.getTransaction().begin();
        jTable1.setRowSelectionInterval(0, 0);
    }

    /** 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() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager1 = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("DBWorldPU").createEntityManager();
        query1 = java.beans.Beans.isDesignTime() ? null : entityManager1.createQuery("SELECT c FROM Contacts c");
        list1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query1.getResultList());
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list1, jTable1);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${name}"));
        columnBinding.setColumnName("Name");
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${address}"));
        columnBinding.setColumnName("Address");
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${phone}"));
        columnBinding.setColumnName("Phone");
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(jTable1);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jTable1, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.name}"), jTextField1, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jTable1, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.address}"), jTextField2, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jTable1, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.phone}"), jTextField3, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

...
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    jTable1.setRowSelectionInterval(0, 0);
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
    if (jTable1.getSelectedRow() > 0) {
        jTable1.setRowSelectionInterval(jTable1.getSelectedRow() - 1, jTable1.getSelectedRow() - 1);
    }
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
    if (jTable1.getSelectedRow() < (jTable1.getRowCount() - 1)) {
        jTable1.setRowSelectionInterval(jTable1.getSelectedRow() + 1, jTable1.getSelectedRow() + 1);
    }
}//GEN-LAST:event_jButton3ActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
// TODO add your handling code here:
    jTable1.setRowSelectionInterval(jTable1.getRowCount() - 1, jTable1.getRowCount() - 1);
}//GEN-LAST:event_jButton4ActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
// TODO add your handling code here:
    //entityManager1.getTransaction().rollback();
    entityManager1.getTransaction().rollback();
    entityManager1.getTransaction().begin();
    java.util.Collection data = query1.getResultList();
    for (Object entity : data) {
        entityManager1.refresh(entity);
    }
    list1.clear();
    list1.addAll(data);
    jTable1.setRowSelectionInterval(0, 0);
}//GEN-LAST:event_jButton5ActionPerformed

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
// TODO add your handling code here:
    my.dbworld.Contacts c = new my.dbworld.Contacts();
    entityManager1.persist(c);
    list1.add(c);
    int row = list1.size() - 1;
    jTable1.setRowSelectionInterval(row, row);
    jTable1.scrollRectToVisible(jTable1.getCellRect(row, 0, true));

}//GEN-LAST:event_jButton6ActionPerformed

private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed
// TODO add your handling code here:
    System.exit(0);
}//GEN-LAST:event_jButton12ActionPerformed

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
// TODO add your handling code here:
    int[] selected = jTable1.getSelectedRows();
    List<my.dbworld.Contacts> toRemove = new ArrayList<my.dbworld.Contacts>(selected.length);
    for (int idx = 0; idx < selected.length; idx++) {
        my.dbworld.Contacts c = (my.dbworld.Contacts) list1.get(jTable1.convertRowIndexToModel(selected[idx]));
        toRemove.add(c);
        entityManager1.remove(c);
    }
    list1.removeAll(toRemove);
    jTable1.setRowSelectionInterval(0, 0);
}//GEN-LAST:event_jButton10ActionPerformed

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
// TODO add your handling code here:
    try {
        entityManager1.getTransaction().commit();
        entityManager1.getTransaction().begin();
    } catch (RollbackException rex) {
        rex.printStackTrace();
        entityManager1.getTransaction().begin();
        List<my.dbworld.Contacts> merged = new ArrayList<my.dbworld.Contacts>(list1.size());
        for (my.dbworld.Contacts c : list1) {
            merged.add(entityManager1.merge(c));
        }
        list1.clear();
        list1.addAll(merged);
    }
}//GEN-LAST:event_jButton8ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FContacts().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager entityManager1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private java.util.List<my.dbworld.Contacts> list1;
    private javax.persistence.Query query1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}