Tuesday, October 21, 2008

Bermain FocusEvent di Java

Kita akan memanfaatkan FocusEvent di Java untuk membuat focusGained dan FocusLost pada TextField. FocusGained seperti onKeyDown and FocusLost seperti OnExit di Delphi kali y:).

Coba kita buat program yang diberi nama "MyFocusLostFocusGain1.java". program ini berisi Label Command, Name, Age and Description. Jika onKeydown di Name maka label command akan tertulis "Please type your name" dan jika onKeydown di Age maka label command akan tertulis "Please type your Age". Saat mouse keluar dari name jika name diisi data maka data yang ada di name dan age akan ditampilkan description.Saat mouse keluar dari Age jika name diisi data yang ada di name dan age maka data akan ditampilkan description.


Download source :
www.esnips.com/doc/f6d77178-b25f-4a2f-808d-8aaf71b87910/MyFocusLostFocusGain1
=============================================================================
package test1;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Insets;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;


import javax.swing.JFrame;

public class MyFocusLostFocusGain1 extends JFrame{

/**
* @param args
*/
private Label lblCommand = null;
private Label lblName = null;
private TextField txfName = null;
private Label lblAge = null;
private TextField txfAge = null;
private Label lblDescription = null;
private TextArea txaDescription = null;

public MyFocusLostFocusGain1() {
super("My Title");
initialize();
}

public void initialize(){

int y = 0;
GridBagConstraints gbcLblCommand = new GridBagConstraints();
gbcLblCommand.gridx = 1;
gbcLblCommand.gridy = y;
gbcLblCommand.anchor = GridBagConstraints.WEST;
gbcLblCommand.fill = GridBagConstraints.HORIZONTAL;
gbcLblCommand.insets = new Insets(5,10,0,10);
lblCommand = new Label();
lblCommand.setText("");

y++;
GridBagConstraints gbcLblName = new GridBagConstraints();
gbcLblName.gridx = 0;
gbcLblName.gridy = y;
gbcLblName.anchor = GridBagConstraints.WEST;
gbcLblName.fill = GridBagConstraints.HORIZONTAL;
lblName = new Label();
lblName.setText("Name");
GridBagConstraints gbcTxfName = new GridBagConstraints();
gbcTxfName.gridx = 1;
gbcTxfName.gridy = y;
gbcTxfName.anchor = GridBagConstraints.WEST;
gbcTxfName.insets = new Insets(5,10,0,10);

y++;
GridBagConstraints gbcLblAge = new GridBagConstraints();
gbcLblAge.gridx = 0;
gbcLblAge.gridy = y;
gbcLblAge.anchor = GridBagConstraints.WEST;
gbcLblAge.fill = GridBagConstraints.HORIZONTAL;
lblAge = new Label();
lblAge.setText("Age");
GridBagConstraints gbcTxfAge = new GridBagConstraints();
gbcTxfAge.gridx = 1;
gbcTxfAge.gridy = y;
gbcTxfAge.anchor = GridBagConstraints.WEST;
gbcTxfAge.insets = new Insets(5,10,0,10);

y++;
GridBagConstraints gbcLblDescription = new GridBagConstraints();
gbcLblDescription.gridx = 0;
gbcLblDescription.gridy = y;
gbcLblDescription.anchor = GridBagConstraints.WEST;
gbcLblDescription.fill = GridBagConstraints.HORIZONTAL;
lblDescription = new Label();
lblDescription.setText("description");
GridBagConstraints gbcTxaDescription = new GridBagConstraints();
gbcTxaDescription.gridx = 1;
gbcTxaDescription.gridy = y;
gbcTxaDescription.anchor = GridBagConstraints.WEST;
gbcTxaDescription.insets = new Insets(5,10,0,10);

this.setLayout(new GridBagLayout());
this.setSize(new Dimension(695, 498));
this.add(lblCommand, gbcLblCommand);
this.add(lblName, gbcLblName);
this.add(getTxfName(), gbcTxfName);
this.add(lblAge, gbcLblAge);
this.add(getTxtAge(), gbcTxfAge);
this.add(lblDescription, gbcLblDescription);
this.add(getTxaDescription(), gbcTxaDescription);


}

private TextField getTxfName() {
if (txfName == null) {
txfName = new TextField();
txfName.setColumns(30);
txfName.setName("name");
txfName.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent feLost) {
lblCommand.setText("Please type your name");

}

public void focusLost(FocusEvent feGain){
if ((!txfName.getText().equals("")) && (!txfAge.getText().equals("")) ){
txaDescription.setText("Name : "+txfName.getText()+" ; "+"Age : "+txfAge.getText());
}
else if (!txfName.getText().equals("")){
txaDescription.setText("Name : "+txfName.getText());
}
else if (!txfAge.getText().equals("")){
txaDescription.setText("Age : "+txfAge.getText());
}
}
}
);

}
return txfName;
}

private TextField getTxtAge() {
if (txfAge == null) {
txfAge = new TextField();
txfAge.setColumns(10);
txfAge.setName("age");
txfAge.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent feLost) {
lblCommand.setText("Please type your Age");

}

public void focusLost(FocusEvent feGain){
if ((!txfName.getText().equals("")) && (!txfAge.getText().equals("")) ){
txaDescription.setText("Name : "+txfName.getText()+" ; "+"Age : "+txfAge.getText());
}
else if (!txfName.getText().equals("")){
txaDescription.setText("Name : "+txfName.getText());
}
else if (!txfAge.getText().equals("")){
txaDescription.setText("Age : "+txfAge.getText());
}
}
}
);

}
return txfAge;
}

private TextArea getTxaDescription() {
if (txaDescription == null) {
txaDescription = new TextArea();
txaDescription.setName("addRemark");
txaDescription .setRows(4);
txaDescription.setColumns(40);
txaDescription.setEditable(false);
}
return txaDescription;
}



}

=============================================================================

atau

http://www.esnips.com/doc/3e5e58c6-d1c6-44e6-ab97-b7bf09db5955/MyFocusLostFocusGain2
=============================================================================

package test1;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Insets;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;


import javax.swing.JFrame;

public class MyFocusLostFocusGain2 extends JFrame implements FocusListener{

/**
* @param args
*/
private Label lblCommand = null;
private Label lblName = null;
private TextField txfName = null;
private Label lblAge = null;
private TextField txfAge = null;
private Label lblDescription = null;
private TextArea txaDescription = null;

public MyFocusLostFocusGain2() {
super("My Title");
initialize();
}

public void initialize(){

int y = 0;
GridBagConstraints gbcLblCommand = new GridBagConstraints();
gbcLblCommand.gridx = 1;
gbcLblCommand.gridy = y;
gbcLblCommand.anchor = GridBagConstraints.WEST;
gbcLblCommand.fill = GridBagConstraints.HORIZONTAL;
gbcLblCommand.insets = new Insets(5,10,0,10);
lblCommand = new Label();
lblCommand.setText("");

y++;
GridBagConstraints gbcLblName = new GridBagConstraints();
gbcLblName.gridx = 0;
gbcLblName.gridy = y;
gbcLblName.anchor = GridBagConstraints.WEST;
gbcLblName.fill = GridBagConstraints.HORIZONTAL;
lblName = new Label();
lblName.setText("Name");
GridBagConstraints gbcTxfName = new GridBagConstraints();
gbcTxfName.gridx = 1;
gbcTxfName.gridy = y;
gbcTxfName.anchor = GridBagConstraints.WEST;
gbcTxfName.insets = new Insets(5,10,0,10);

y++;
GridBagConstraints gbcLblAge = new GridBagConstraints();
gbcLblAge.gridx = 0;
gbcLblAge.gridy = y;
gbcLblAge.anchor = GridBagConstraints.WEST;
gbcLblAge.fill = GridBagConstraints.HORIZONTAL;
lblAge = new Label();
lblAge.setText("Age");
GridBagConstraints gbcTxfAge = new GridBagConstraints();
gbcTxfAge.gridx = 1;
gbcTxfAge.gridy = y;
gbcTxfAge.anchor = GridBagConstraints.WEST;
gbcTxfAge.insets = new Insets(5,10,0,10);

y++;
GridBagConstraints gbcLblDescription = new GridBagConstraints();
gbcLblDescription.gridx = 0;
gbcLblDescription.gridy = y;
gbcLblDescription.anchor = GridBagConstraints.WEST;
gbcLblDescription.fill = GridBagConstraints.HORIZONTAL;
lblDescription = new Label();
lblDescription.setText("description");
GridBagConstraints gbcTxaDescription = new GridBagConstraints();
gbcTxaDescription.gridx = 1;
gbcTxaDescription.gridy = y;
gbcTxaDescription.anchor = GridBagConstraints.WEST;
gbcTxaDescription.insets = new Insets(5,10,0,10);

this.setLayout(new GridBagLayout());
this.setSize(new Dimension(695, 498));
this.add(lblCommand, gbcLblCommand);
this.add(lblName, gbcLblName);
this.add(getTxfName(), gbcTxfName);
this.add(lblAge, gbcLblAge);
this.add(getTxtAge(), gbcTxfAge);
this.add(lblDescription, gbcLblDescription);
this.add(getTxaDescription(), gbcTxaDescription);


}

private TextField getTxfName() {
if (txfName == null) {
txfName = new TextField();
txfName.setColumns(30);
txfName.setName("name");
txfName.addFocusListener(this);

}
return txfName;
}

private TextField getTxtAge() {
if (txfAge == null) {
txfAge = new TextField();
txfAge.setColumns(10);
txfAge.setName("age");
txfAge.addFocusListener(this);

}
return txfAge;
}

private TextArea getTxaDescription() {
if (txaDescription == null) {
txaDescription = new TextArea();
txaDescription.setName("addRemark");
txaDescription .setRows(4);
txaDescription.setColumns(40);
txaDescription.setEditable(false);
}
return txaDescription;
}


public void focusGained(FocusEvent feLost) {
if (feLost.getSource() == txfName) {
lblCommand.setText("Please type your name");
}
else if (feLost.getSource() == txfAge){
lblCommand.setText("Please type your Age");
}

}

public void focusLost(FocusEvent feGain){

if ((feGain.getSource() == txfName ) ||(feGain.getSource() == txfAge)) {
if ((!txfName.getText().equals("")) && (!txfAge.getText().equals("")) ){
txaDescription.setText("Name : "+txfName.getText()+" ; "+"Age : "+txfAge.getText());
}
else if (!txfName.getText().equals("")){
txaDescription.setText("Name : "+txfName.getText());
}
else if (!txfAge.getText().equals("")){
txaDescription.setText("Age : "+txfAge.getText());
}

}

}


}

Bermain dengan Gridbagconstraint di java

Property yang ada di class Gridbagconstraint antara lain :
- gridx -> digunakan untuk menentukan posisi x dr komponen
- gridy -> digunakan untuk menentukan posisi y dr komponen
- anchor -> digunakan jika komponen lebih kecil dari display area
- insets -> digunakan untuk menentukan external padding dari komponen, minimum space antara komponen dan ujung dari display area
- gridheight -> digunakan untuk menentukan jumlah sel di suatu kolom untuk display area komponen
- gridwidth -> digunakan untuk menentukan jumlah sel di baris untuk display area komponen

class Gridbagconstraint ini kita coba untuk di test di program yang kita beri nama "MyGridBagConstraints1.java" sbb :

Download Source :
esnips.com/doc/d4d4254d-46a8-4e8e-9f50-8ee70c7e8b17/MyGridBagConstraints1
=================================================================================================
package test1;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Insets;


import javax.swing.JFrame;

public class MyGridBagConstraints1 extends JFrame{

/**
* @param args
*/
private Label lblName = null;
private TextField txfName = null;
private Label lblAddress = null;
private TextField txfAddress = null;
private Label lblAge = null;
private TextField txfAge = null;
private Label lblWeight = null;
private TextField txfWeight = null;
private Label lblDescription = null;
private TextArea txaDescription = null;
private Panel pnlWeightAge = null;

public MyGridBagConstraints1() {
super("My Title");
initialize();
}

public void initialize(){

GridBagConstraints gbcLblName = new GridBagConstraints();
gbcLblName.gridx = 0;
gbcLblName.gridy = 1;
gbcLblName.anchor = GridBagConstraints.WEST;
gbcLblName.weighty = 0.1;
lblName = new Label();
lblName.setText("Name");
GridBagConstraints gbcTxfName = new GridBagConstraints();
gbcTxfName.gridx = 1;
gbcTxfName.gridy = 1;
gbcTxfName.anchor = GridBagConstraints.WEST;
gbcTxfName.insets = new Insets(0,10,0,5);
gbcTxfName.weightx = 0.1;

GridBagConstraints gbcLblAddress = new GridBagConstraints();
gbcLblAddress.gridx = 0;
gbcLblAddress.gridy = 2;
gbcLblAddress.anchor = GridBagConstraints.WEST;
gbcLblAddress.weighty = 0.1;
lblAddress = new Label();
lblAddress.setText("Address");
GridBagConstraints gbcTxfAddress = new GridBagConstraints();
gbcTxfAddress.gridx = 1;
gbcTxfAddress.gridy = 2;
gbcTxfAddress.anchor = GridBagConstraints.WEST;
gbcTxfAddress.insets = new Insets(0,10,0,5);
gbcTxfAddress.weightx = 0.1;

GridBagConstraints gbcLblAge = new GridBagConstraints();
gbcLblAge.gridx = 0;
gbcLblAge.gridy = 3;
gbcLblAge.anchor = GridBagConstraints.WEST;
gbcLblAge.weighty = 0.1;
lblAge = new Label();
lblAge.setText("Age");
GridBagConstraints gbcPnlWeightAge = new GridBagConstraints();
gbcPnlWeightAge.gridx = 1;
gbcPnlWeightAge.gridy = 3;
gbcPnlWeightAge.anchor = GridBagConstraints.WEST;
gbcPnlWeightAge.insets = new Insets(0,10,0,5);
gbcPnlWeightAge.weighty = 0.1;

GridBagConstraints gbcLblDescription = new GridBagConstraints();
gbcLblDescription.gridx = 0;
gbcLblDescription.gridy = 4;
gbcLblDescription.anchor = GridBagConstraints.WEST;
gbcLblDescription.weighty = 0.1;
lblDescription = new Label();
lblDescription.setText("description");
GridBagConstraints gbcTxaDescription = new GridBagConstraints();
gbcTxaDescription.gridx = 1;
gbcTxaDescription.gridy = 4;
gbcTxaDescription.anchor = GridBagConstraints.WEST;
gbcTxaDescription.insets = new Insets(0,10,0,5);
gbcTxaDescription.weightx = 0.1;

this.setLayout(new GridBagLayout());
this.setSize(new Dimension(695, 300));
this.add(lblName, gbcLblName);
this.add(getTxfName(), gbcTxfName);
this.add(lblAddress, gbcLblAddress);
this.add(getTxtAddress(), gbcTxfAddress);
this.add(lblAge, gbcLblAge);
this.add(getPnlWeightAge() , gbcPnlWeightAge);
this.add(lblDescription, gbcLblDescription);
this.add(getTxaDescription(), gbcTxaDescription);

}
private Panel getPnlWeightAge() {
if (pnlWeightAge == null) {
lblWeight = new Label();
lblWeight.setText("Weight");
pnlWeightAge = new Panel();
pnlWeightAge.add(getTxtAge(), null);
pnlWeightAge.add(lblWeight, null);
pnlWeightAge.add(getTxtWeight(), null);
}
return pnlWeightAge;
}

private TextField getTxfName() {
if (txfName == null) {
txfName = new TextField();
txfName.setColumns(30);
txfName.setName("name");
}
return txfName;
}

private TextField getTxtAddress() {
if (txfAddress == null) {
txfAddress = new TextField();
txfAddress.setColumns(40);
txfAddress.setName("address");
}
return txfAddress;
}

private TextField getTxtAge() {
if (txfAge == null) {
txfAge = new TextField();
txfAge.setColumns(10);
txfAge.setName("age");
}
return txfAge;
}

private TextField getTxtWeight() {
if (txfWeight == null) {
txfWeight = new TextField();
txfWeight.setColumns(10);
txfWeight .setName("weight");
}
return txfWeight;
}

private TextArea getTxaDescription() {
if (txaDescription == null) {
txaDescription = new TextArea();
txaDescription.setName("addRemark");
txaDescription .setRows(4);
txaDescription.setColumns(40);
}
return txaDescription;
}
}


=================================================================================================

Monday, October 20, 2008

Create dan Compile Program Assembler di Linux

- bikin program Assembler di linux bs gunakan editor "kwrite" or editor gedit misal : file dikasih nama hello.S
- untuk meng-compile program hello.S agar menghasilkan file object "hello.o" di linux dg perintah "as" yang merupakan compiler default pada linux yang menggunakan GNU Assembler compiler. seperti dibawah ini di console :

as -o hello.o hello.S

- untuk menghasilkan executable file nya digunakan perintah "ld".seperti dibawah ini

ld -s -o hello hello.o

- untuk menjalankan program yang dihasilkan tersebut dg perintah dibawah ini di console:

./hello