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());
}

}

}


}

No comments: