BAB 8(Form & Event Heandler)

Event Handling

Event Handling adalah suatu metode untuk menangani sebuah event/aksi yang diberikan pengguna kepada suatu komponen GUI. Event adalah suatu peristiwa yang dipicu oleh pengguna pada suatu komponen, misalnya tombol ditekan. Dua paket yang biasa digunakan menangani kejadian adalah java.awt.event dan java.swing.event. Event handling dalam menangani event terbagi menjadi 2 macam yaitu Event Source, Event Listener atau Event Handler.

Pada program – program GUI, setiap user melakukan pengisian karakter melalui keyboard maupun klik terhadap tombol mouse, hal ini dikatakan bahwa suatu event telah terjadi. Event – event semacam ini dapat direspon atau ditangani oleh program untuk kemudian diproses sesuai dengan kebutuhan yang diinginkan. Sebagai contoh, pada saat user melakukan tombol klik pada suatu tombol yang ada di dalam sebuah program, program akan menampilkan sebuah dialog atau form lain. Teknik atau cara untuk melakukan suatu pekerjaan tertentu pada saat suatu event terjadi sering dinamakan event handling.

Mekanisme event handling yang terdapat didalam java didasarkan pada delegation event model (model pendelegasian event), yang mendefinisikan cara standart dan konsisten untuk membangkitkan dan memproses suatu event. Konsepnya sederhana saja : event source (sumber atau pembangkit event) akan membangkitkan atau menghasilkan suatu event tertentu dan mengirimkannya ke satu atau lebih event listener (pendengar event). Contoh event source adalah objek – objek atau komponen yang terdapat di dalam user-interface seperti : button, listbox, combobox, dan sebagainya. Sedangkan contoh event listener adalah interface keylistener, ActionListener, MouseMotionListener, ItemListener, dan sebagainnya. Disini, event listener akan menunggu sampai suatu event diterima. Begitu diterima event listener akan memproses event tersebut dengan melakukan pekerjaan – pekerjaan tertentu. Salah satu keuntungan dari mekanisme seperti ini adalah terdapatnya pemisahan – pemisahan yang jelas antara kode logis yang digunakan untuk memproses event dengan kode logis untuk keperluan user interface (yang digunakan membengkitkan event). Ini artinya, terdapat suatu pendelegasian event dari event source ke event listener karena pada saat pembuatan event source kita tidak perlu mendefinisikan kode untuk keperluan penanganan event.

Capture
Program C#
</div>
<div>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OOP2_Muslimin_eventHandling
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}
Hasilnya Adalah :
CaptureA1
Berikut Contoh program C++
</div>
<div>#pragma once

namespace OOP2_Muslimin_C_eventHandling {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
///          'Resource File Name' property for the managed resource compiler tool
///          associated with all .resx files this class depends on.  Otherwise,
///          the designers will not be able to interact properly with localized
///          resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^  label1;
private: System::Windows::Forms::TextBox^  textBox6;
private: System::Windows::Forms::Label^  label9;
private: System::Windows::Forms::Button^  button2;
private: System::Windows::Forms::Button^  button1;
private: System::Windows::Forms::TextBox^  textBox5;
private: System::Windows::Forms::TextBox^  textBox4;
private: System::Windows::Forms::TextBox^  textBox3;
private: System::Windows::Forms::TextBox^  textBox2;
private: System::Windows::Forms::TextBox^  textBox1;
private: System::Windows::Forms::Label^  label8;
private: System::Windows::Forms::Label^  label7;
private: System::Windows::Forms::Label^  label6;
private: System::Windows::Forms::Label^  label5;
private: System::Windows::Forms::Label^  label4;
private: System::Windows::Forms::Label^  label3;
protected:

private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->label1 = (gcnew System::Windows::Forms::Label());
this->textBox6 = (gcnew System::Windows::Forms::TextBox());
this->label9 = (gcnew System::Windows::Forms::Label());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button1 = (gcnew System::Windows::Forms::Button());
this->textBox5 = (gcnew System::Windows::Forms::TextBox());
this->textBox4 = (gcnew System::Windows::Forms::TextBox());
this->textBox3 = (gcnew System::Windows::Forms::TextBox());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->label8 = (gcnew System::Windows::Forms::Label());
this->label7 = (gcnew System::Windows::Forms::Label());
this->label6 = (gcnew System::Windows::Forms::Label());
this->label5 = (gcnew System::Windows::Forms::Label());
this->label4 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Algerian", 14.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label1->Location = System::Drawing::Point(91, 19);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(167, 21);
this->label1->TabIndex = 0;
this->label1->Text = L"BIODATA PRIBADI";
this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click);
//
// textBox6
//
this->textBox6->Location = System::Drawing::Point(123, 221);
this->textBox6->Name = L"textBox6";
this->textBox6->Size = System::Drawing::Size(178, 20);
this->textBox6->TabIndex = 31;
//
// label9
//
this->label9->AutoSize = true;
this->label9->Location = System::Drawing::Point(41, 224);
this->label9->Name = L"label9";
this->label9->Size = System::Drawing::Size(52, 13);
this->label9->TabIndex = 30;
this->label9->Text = L"KAMPUS";
//
// button2
//
this->button2->Location = System::Drawing::Point(210, 267);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(48, 30);
this->button2->TabIndex = 29;
this->button2->Text = L"NO";
this->button2->UseVisualStyleBackColor = true;
//
// button1
//
this->button1->Location = System::Drawing::Point(95, 267);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(48, 30);
this->button1->TabIndex = 28;
this->button1->Text = L"YES";
this->button1->UseVisualStyleBackColor = true;
//
// textBox5
//
this->textBox5->Location = System::Drawing::Point(123, 161);
this->textBox5->Name = L"textBox5";
this->textBox5->Size = System::Drawing::Size(178, 20);
this->textBox5->TabIndex = 27;
//
// textBox4
//
this->textBox4->Location = System::Drawing::Point(123, 191);
this->textBox4->Name = L"textBox4";
this->textBox4->Size = System::Drawing::Size(178, 20);
this->textBox4->TabIndex = 26;
//
// textBox3
//
this->textBox3->Location = System::Drawing::Point(123, 107);
this->textBox3->Name = L"textBox3";
this->textBox3->Size = System::Drawing::Size(178, 20);
this->textBox3->TabIndex = 25;
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(123, 135);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(178, 20);
this->textBox2->TabIndex = 24;
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(123, 78);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(178, 20);
this->textBox1->TabIndex = 23;
//
// label8
//
this->label8->AutoSize = true;
this->label8->Location = System::Drawing::Point(41, 163);
this->label8->Name = L"label8";
this->label8->Size = System::Drawing::Size(41, 13);
this->label8->TabIndex = 22;
this->label8->Text = L"KELAS";
//
// label7
//
this->label7->AutoSize = true;
this->label7->Location = System::Drawing::Point(67, 228);
this->label7->Name = L"label7";
this->label7->Size = System::Drawing::Size(0, 13);
this->label7->TabIndex = 21;
//
// label6
//
this->label6->AutoSize = true;
this->label6->Location = System::Drawing::Point(41, 194);
this->label6->Name = L"label6";
this->label6->Size = System::Drawing::Size(66, 13);
this->label6->TabIndex = 20;
this->label6->Text = L"SEMESTER";
//
// label5
//
this->label5->AutoSize = true;
this->label5->Location = System::Drawing::Point(41, 106);
this->label5->Name = L"label5";
this->label5->Size = System::Drawing::Size(27, 13);
this->label5->TabIndex = 19;
this->label5->Text = L"NIM";
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(41, 135);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(58, 13);
this->label4->TabIndex = 18;
this->label4->Text = L"JURUSAN";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(41, 76);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(38, 13);
this->label3->TabIndex = 17;
this->label3->Text = L"NAMA";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::Color::Silver;
this->ClientSize = System::Drawing::Size(343, 304);
this->Controls->Add(this->textBox6);
this->Controls->Add(this->label9);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->textBox5);
this->Controls->Add(this->textBox4);
this->Controls->Add(this->textBox3);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->label8);
this->Controls->Add(this->label7);
this->Controls->Add(this->label6);
this->Controls->Add(this->label5);
this->Controls->Add(this->label4);
this->Controls->Add(this->label3);
this->Controls->Add(this->label1);
this->Name = L"Form1";
this->Text = L"OOP2_Muslimin_C++_eventHandling";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
}
private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
}
};
}
Maka Hasilnya Sebagai berikut :
CaptureB1
Berikut Contoh Program Java :
/**
*
* @author indrawsp
*/
public class OOP2_Muslimin_Java_eventHandling extends javax.swing.JFrame {

/** Creates new form OOP2_Muslimin_Java_eventHandling */
public OOP2_Muslimin_Java_eventHandling() {
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() {

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextPane1 = new javax.swing.JTextPane();
jScrollPane2 = new javax.swing.JScrollPane();
jTextPane2 = new javax.swing.JTextPane();
jScrollPane3 = new javax.swing.JScrollPane();
jTextPane3 = new javax.swing.JTextPane();
jScrollPane4 = new javax.swing.JScrollPane();
jTextPane4 = new javax.swing.JTextPane();
jScrollPane5 = new javax.swing.JScrollPane();
jTextPane5 = new javax.swing.JTextPane();
jScrollPane6 = new javax.swing.JScrollPane();
jTextPane6 = new javax.swing.JTextPane();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setFont(new java.awt.Font("Algerian", 0, 14)); // NOI18N

jLabel1.setFont(new java.awt.Font("Algerian", 0, 14)); // NOI18N
jLabel1.setText("BIODATA PRIBADI");

jLabel2.setText("NAMA");

jLabel3.setText("NIM");

jLabel4.setText("JURUSAN");

jLabel5.setText("KELAS");

jLabel6.setText("SEMESTER");

jLabel7.setText("KAMPUS");

jScrollPane1.setViewportView(jTextPane1);

jScrollPane2.setViewportView(jTextPane2);

jScrollPane3.setViewportView(jTextPane3);

jScrollPane4.setViewportView(jTextPane4);

jScrollPane5.setViewportView(jTextPane5);

jScrollPane6.setViewportView(jTextPane6);

jButton1.setText("YES");

jButton2.setText("NO");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(26, 26, 26))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane3))))
.addGap(26, 26, 26)))
.addContainerGap(33, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(284, Short.MAX_VALUE)
.addComponent(jButton2)
.addGap(69, 69, 69))
.addGroup(layout.createSequentialGroup()
.addGap(134, 134, 134)
.addComponent(jLabel1)
.addContainerGap(149, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.addComponent(jScrollPane1, 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.TRAILING)
.addComponent(jLabel3)
.addComponent(jScrollPane2, 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.TRAILING)
.addComponent(jLabel4)
.addComponent(jScrollPane3, 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.TRAILING)
.addComponent(jLabel5)
.addComponent(jScrollPane6, 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.TRAILING)
.addComponent(jLabel6)
.addComponent(jScrollPane4, 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.TRAILING)
.addComponent(jLabel7)
.addComponent(jScrollPane5, 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(jButton2)
.addComponent(jButton1))
.addContainerGap())
);

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

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

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JScrollPane jScrollPane5;
private javax.swing.JScrollPane jScrollPane6;
private javax.swing.JTextPane jTextPane1;
private javax.swing.JTextPane jTextPane2;
private javax.swing.JTextPane jTextPane3;
private javax.swing.JTextPane jTextPane4;
private javax.swing.JTextPane jTextPane5;
private javax.swing.JTextPane jTextPane6;
// End of variables declaration

}
Hasil Debugnya
Capture

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s