Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

import javax.swing.

JOptionPane;
import javax.swing.table.DefaultTableModel;

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to
change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this
template
*/

/**
*
* @author Asus
*/
public class Aplikasi extends javax.swing.JFrame {

/**
* Creates new form Aplikasi
*/
public Aplikasi() {
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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

jTextFieldName = new javax.swing.JTextField();


jTextFieldEmail = new javax.swing.JTextField();
jComboBoxKategori = new javax.swing.JComboBox<>();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jTextFieldPhone = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabelPhone = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jButtonSave = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTextFieldName.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextFieldNameActionPerformed(evt);
}
});

jTextFieldEmail.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextFieldEmailActionPerformed(evt);
}
});
jComboBoxKategori.setModel(new javax.swing.DefaultComboBoxModel<>(new
String[] { "friend", "Family", "Work" }));
jComboBoxKategori.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBoxKategoriActionPerformed(evt);
}
});

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Name", "Phone", "Email", "Kategori"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.Integer.class,
java.lang.String.class, java.lang.String.class
};

public Class getColumnClass(int columnIndex) {


return types [columnIndex];
}
});
jScrollPane1.setViewportView(jTable1);
if (jTable1.getColumnModel().getColumnCount() > 0) {
jTable1.getColumnModel().getColumn(0).setHeaderValue("Name");
jTable1.getColumnModel().getColumn(1).setHeaderValue("Phone");
jTable1.getColumnModel().getColumn(2).setHeaderValue("Email");
jTable1.getColumnModel().getColumn(3).setHeaderValue("Kategori");
}

jTextFieldPhone.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextFieldPhoneActionPerformed(evt);
}
});

jLabel1.setText("Name");

jLabelPhone.setText("Phone");

jLabel3.setText("Email");

jButtonSave.setText("Save");
jButtonSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonSaveActionPerformed(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.Alignm
ent.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(25, 25, 25)
.addComponent(jTextFieldName,
javax.swing.GroupLayout.PREFERRED_SIZE, 202,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayou
t.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabelPhone))
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayou
t.Alignment.LEADING)
.addComponent(jComboBoxKategori,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupL
ayout.Alignment.LEADING, false)
.addComponent(jTextFieldPhone,
javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
.addComponent(jTextFieldEmail))
.addComponent(jButtonSave))))
.addContainerGap(135, Short.MAX_VALUE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(16, 16, 16)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextFieldName,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(9, 9, 9)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.BASELINE)
.addComponent(jLabelPhone)
.addComponent(jTextFieldPhone,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED
, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.BASELINE)
.addComponent(jLabel3)
.addComponent(jTextFieldEmail,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED
)
.addComponent(jComboBoxKategori,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED
)
.addComponent(jButtonSave)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED
)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
59, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(196, 196, 196))
);

pack();
}// </editor-fold>

private void jTextFieldEmailActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:


}

private void jTextFieldNameActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:


}

private void jTextFieldPhoneActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:


}

private void jComboBoxKategoriActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:


}

private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:


String name = jTextFieldName.getText();
String phone = jTextFieldPhone.getText();
String email = jTextFieldEmail.getText();
String category = jComboBoxKategori.getSelectedItem().toString();

if (name.isEmpty() || phone.isEmpty() || email.isEmpty() ||


category.isEmpty()) {
JOptionPane.showMessageDialog(this, "Lengkapi semua kolom!",
"Peringatan", JOptionPane.WARNING_MESSAGE);
return;
}
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.addRow(new Object[]{name, phone, email, category});

jTextFieldName.setText("");
jTextFieldPhone.setText("");
jTextFieldEmail.setText("");
jComboBoxKategori.setSelectedIndex(0);
}
/**
* @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(Aplikasi.class.getName()).log(java.util.logging.
Level.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(Aplikasi.class.getName()).log(java.util.logging.
Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(Aplikasi.class.getName()).log(java.util.logging.
Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(Aplikasi.class.getName()).log(java.util.logging.
Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Aplikasi().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton jButtonSave;
private javax.swing.JComboBox<String> jComboBoxKategori;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabelPhone;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextFieldEmail;
private javax.swing.JTextField jTextFieldName;
private javax.swing.JTextField jTextFieldPhone;
// End of variables declaration
}

You might also like