Save the JTextField values in double type variables

1

I have the following code, and my problem is that I do not know how to do the action listener of my "Accept" button so that I save what is written in the JTextFields in the variables type double

import javax.swing.*;

public class Aprendizaje  extends JFrame {
private JLabel lx1,lx2,ly1;
private JTextField  textx1,textx2,texty1,
                    textx1a,textx2a,texty1a,
                    textx1b,textx2b,texty1b,
                    textx1c,textx2c,texty1c;

public double x1, x2, x1a, x1b, x2a, x2b, x1c, x2c, y, y1, y2, y3,
w1, w2, umbral;

private JButton button1;
public Aprendizaje(){
this.setLayout(null);
this.setSize(350,350);            
this.setTitle("thisrendizaje");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setVisible(true);
this.setLocationRelativeTo(null);
//Instanciando Cuadros de Texto         
lx1 = new JLabel("X1");
this.add(lx1);
lx1.setBounds(25, 15, 25, 20);
textx1 = new JTextField();
this.add(textx1);
textx1.setBounds(25, 35, 35, 30);
lx2 = new JLabel("X2");
this.add(lx2);
lx2.setBounds(125, 15, 25, 20);
textx2 = new JTextField();
this.add(textx2);
textx2.setBounds(125, 35, 35, 30);
ly1 = new JLabel("Y1");
this.add(ly1);
ly1.setBounds(250, 10, 25, 30);
texty1 = new JTextField();
this.add(texty1);
texty1.setBounds(250, 35, 35, 30);
textx1a = new JTextField();
this.add(textx1a);
textx1a.setBounds(25, 90, 35, 30);
textx2a = new JTextField();
this.add(textx2a);
textx2a.setBounds(125, 90, 35, 30);
texty1a = new JTextField();
this.add(texty1a);
texty1a.setBounds(250, 90, 35, 30);
textx1b = new JTextField();
this.add(textx1b);
textx1b.setBounds(25, 145, 35, 30);
textx2b = new JTextField();
this.add(textx2b);
textx2b.setBounds(125, 145, 35, 30);
texty1b = new JTextField();
this.add(texty1b);
texty1b.setBounds(250, 145, 35, 30);
textx1c = new JTextField();
this.add(textx1c);
textx1c.setBounds(25, 195, 35, 30);
textx2c = new JTextField();
this.add(textx2c);
textx2c.setBounds(125, 195, 35, 30);
texty1c = new JTextField();
this.add(texty1c);
texty1c.setBounds(250, 195, 35, 30);
//Fin cuadros de texto
//Botones
button1 = new JButton("Aceptar");
this.add(button1);
button1.setBounds(75, 300, 100, 20);
  }
}

For example, the value of the TextField textx1 would be saved in the double x1, the value of textx2 in the double x2 and so on.

    
asked by Juan Pimentel 11.09.2017 в 01:27
source

2 answers

0

I have a small example of how to use the listener (although in this case I send text strings to a table)

tablaBuscar.addMouseListener(new MouseAdapter(){
        public void mousePressed(MouseEvent me) {
            if (me.getClickCount() == 2){
                 int filaseleccionada = tablaBuscar.getSelectedRow();
                 if (filaseleccionada== -1){
                    JOptionPane.showMessageDialog(null, "No se ha seleccionado ninguna fila");
                }
                else{
                    try{   
                        //llenando los Campos-columnas
                        txtId.setText(tablaBuscar.getValueAt(filaseleccionada, 0).toString());
                        txtDescripcion.setText(tablaBuscar.getValueAt(filaseleccionada, 1).toString());
                        txtcargo.setText(tablaBuscar.getValueAt(filaseleccionada, 2).toString());


                    }
                    catch(Exception ex2){
                        JOptionPane.showMessageDialog(null, "Error al mostrar datos del registro;\n"+ex2);
                    }
                }
            }
        }
});

You can change in the first line of code the tablaBuscar.addMouseListener... for your button: botonAceptar.addMouseListener ... Then you just need to assign to each variable the values of the jTextFields

    
answered by 11.09.2017 в 01:58
0

To convert the value of a JTextField to double use the method parseDouble() of the class Double . I'll tell you what it would be like:

button1 = new JButton("Aceptar");
button1.addActionListener(new ActionListener() { 
    @Override
    public void actionPerformed(ActionEvent e) {
        x1 = Double.parseDouble(textx1.getText());
        x2 = Double.parseDouble(textx2.getText());
        ...
    }
);
this.add(button1);

I wrote it to you with the method to add the listener since I see that you also had some doubt with that (correct me if I'm wrong). The actionPerformed(ActionEvent) method is normally used to control the user's click action. If you use mousePressed as you have already been proposed, you would only be controlling the action of clicking with the mouse so your application would not work correctly on touch screens for example or on any device that does not have a mouse.

P.D .: Remember to add the validations of JTextField needed so that the user can only enter values double . If you do not do this and the user enters, for example, a String or something else, your application will throw an exception and crash.

I hope it works for you:)

    
answered by 14.10.2018 в 13:23