Kita akan memanfaatkan MouseEvent di Java untuk membuat mouseEntered dan mouseExited pada TextField. mouseEntered ketika mouse berada diatas posisi TextField and mouseExited ketika mouse keluar dari area TextField .
Coba kita buat program yang diberi nama "MyMouseEvent1.java". program ini berisi Label Command, Name, Age and Description. Jika mouse berada di atas posisi TextField Name maka label command akan tertulis "Please type your name" dan jika mouse berada diatas posisi TextField 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 :
esnips.com/doc/6f73d79d-9a06-4707-9a8d-d4b42ed52188/MyMouseEvent1
==========================================================
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 java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
public class MyMouseEvent1 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 MyMouseEvent1() {
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.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
lblCommand.setText("Please type your name");
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
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.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
lblCommand.setText("Please type your Age");
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
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;
}
}
==========================================================
No comments:
Post a Comment