How to perform an action when you stop writing in a JText Field in Java Swing? (something like Java's onChange script)

3

I have a JText Field where you can type the amount of a product and I want to call a function after filling the JText Field to be able to so convert the value entered in currency.

I did not find something similar in Key events and used keyPressed, Keyreleased but both work as an onKeyUp.

Does anyone know how I can do this?

I do not know what code to show so I put the form in which I convert the value in currency:

String numCadena = "";
   if (txtValorCompra.getText() == "" || txtValorCompra.getText() == null){
       numCadena = "0.00";
   }else{
       numCadena = txtValorCompra.getText();
   }
   int numEntero = Integer.parseInt(numCadena);
   double monto = numEntero;
   NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.getDefault());
   txtValorCompra.setText(nf.format(monto));
    
asked by Luis 11.09.2018 в 00:05
source

2 answers

2

A good way or way to perform a action when you stop writing in the JTextField would be to use the Win or lose focus event with the FocusListener interface and the respective addFocusListener () belonging to java.awt.Component which calls that action .

txtValorCompra.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
              //que hacer cuando ganamos el foco
            }

            @Override
            public void focusLost(FocusEvent e) {
                //que hacer cuando se pierda el foco
                String numCadena = "";
                if (txtValorCompra.getText().trim().equals("")) {
                    numCadena = "0.00";
                } else {
                    numCadena = txtValorCompra.getText();
                }
                double numEntero = Double.parseDouble(numCadena);
                area.setText(String.valueOf(numEntero));
            }
        });

I'll give you an example:

public class JTextFieldTest extends JFrame {

    public JTextFieldTest() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        this.setBounds(300, 300, 350, 300);
        JTextArea area = new JTextArea();
        JTextField txtValorCompra = new JTextField(20);

        txtValorCompra.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {

            }

            @Override
            public void focusLost(FocusEvent e) {
                String numCadena = "";
                if (txtValorCompra.getText().trim().equals("")) {
                    numCadena = "0.00";
                } else {
                    numCadena = txtValorCompra.getText();
                }
                double numEntero = Double.parseDouble(numCadena);
                area.setText(String.valueOf(numEntero));
            }
        });

        JPanel panel = new JPanel();
        JLabel label = new JLabel();

        label.setText("Valor: ");
        panel.add(label);
        panel.add(txtValorCompra);
        this.add("North", panel);
        this.add("South", area);
    }

    public static void main(String[] args) {
        JTextFieldTest jt = new JTextFieldTest();
        jt.setDefaultCloseOperation(EXIT_ON_CLOSE);
        jt.setVisible(true);
    }

}
    
answered by 11.09.2018 / 09:03
source
1

Try this:

txtValorCompra.getDocument().addDocumentListener(new DocumentListener() {
   //Lo que desees u ocupes hacer, este es un metodo similar a onchange en JS.
})

Greetings.

    
answered by 11.09.2018 в 00:09