Sunday, November 1, 2009

Sympathy About Cancer

JTextField Reading

At the request of my friend Pollitux talk about events that produce the keyboard on a JTextField to process data quickly on this component.
class JTextField javax.swing package is a class that helps us to create text boxes, graphical user interfaces, which can be adding in a form and ask the user for a value we need to processing such as name, age, address, email, etc. Like all elements have a lot of methods and I shall only discuss some methods relevant.
Builders:
JTextField () - Creates an empty text field.
JTextField (int columns) - Creates an empty text field with a specific number of columns.
JTextField (String text) - Create a text field with a message
JTextField (String text, int columns) - Create a text field with a message and a specific number of columns.
Methods:
Void
addActionListener (java.awt.event.ActionListener action)
Adds a listener component action events, there is generally when we enter into the text field.
void addKeyListener (java.awt.event.KeyListener eventoTeclado)
Adds a listener component events from the keyboard, including enter. Responds to events like pressing a key, and release a key when painting the character corresponding to the key in the text field.
void addMouseListener (java.awt.event.MouseListener eventoMouse)
A gregarious an event listener component generated by the mouse.
void setFont (java.awat.Font source) - I change the font for the text field.
Our focus is on the KeyListener interface. This class provides three methods that we implement and are responding to keyboard events mentioned above:
void keyPressed (KeyEvent evt) - This method is invoked when a key is pressed.
void keyReleased (KeyEvent evt) - This method is invoked when a key is released.
void keyTyped (KeyEvent evt) - This method is invoked when the character is printed for the key pressed on a text field.
NOTE: As KeyListener is an interface must implement all methods, however, if we do not need all the adapters to use the listening component. All interfaces of action have an adapter that helps us to implement only the methods we need, if we may use your class KeyListener adapted KeyAdapter call.

Here a small example of using the KeyListener interface:

java.awt.GridLayout import, import
java.awt.event.KeyListener;
import java.awt.event . KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField ;

EventosTeclado public class extends JFrame implements KeyListener {

    public EventosTeclado () {
        super ( "La interface KeyListener" );
        setSize ( 400, 150 );
        setLocationRelativeTo ( null );
        setResizable ( false );
        setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE );
        iniciarGUI ();
    }

    private void iniciarGUI () {
        JPanel pnlCampos = new JPanel ( new GridLayout ( 3, 2, -100, 0 ) );
        pnlCampos. setBounds ( 10, 10, 380, 85 );
        cmpNumero1 = new JTextField ( "0" );
        cmpNumero1. addKeyListener ( this );
        cmpNumero2 = new JTextField ( "0" );
        cmpNumero2. addKeyListener ( this );
        cmpResultado = new JTextField ( "0" );
        pnlCampos. add (new JLabel ( "Number:" ));
pnlCampos. add (cmpNumero1);
pnlCampos. add (new JLabel ( "Number:" ));
pnlCampos. add (cmpNumero2);
pnlCampos. add ( new JLabel ( "Sum:" ));
pnlCampos. add (cmpResultado)
getContentPane (). setLayout (null )
getContentPane (). add (pnlCampos)
}

public void keyPressed (KeyEvent AD) {
Sumar ();
}

public void keyReleased (KeyEvent AD) {
Sumar ();}


public void keyTyped (KeyEvent evt) {
summer ();}



private void summer () {
double amount = 0;
try {
; = double the amount. parseDouble (cmpNumero1. gettext ()) + double. parseDouble (cmpNumero2. gettext ());}

catch (NumberFormatException exc) {
sum = 0;

} finally {
; cmpResultado. setText ( "" + suma );
        }
    }

    public static void main ( String args[] ) {
        new EventosTeclado (). setVisible ( true );
   
}

private JTextField cmpNumero1;
private JTextField cmpNumero2;
private JTextField cmpResultado;
}

0 comments:

Post a Comment