Foros aprenderaprogramar.com

Aprender a programar => Aprender a programar desde cero => Mensaje iniciado por: cardiobeta en 13 de Marzo 2013, 01:35

Título: Vaciar string de JOptionPane en jLabel con java
Publicado por: cardiobeta en 13 de Marzo 2013, 01:35
Hola nuevamente. Como hago para vaciar el texto introducido en un jlabel. He intentado esto pero no funciona:
String response=JOptionPane.showInputDialog(null, "Ingrese nuevo dx");
if((response!=null) && (response.length() > 0)) {
label22.setText(response);
Gracias de antemano.
Título: Re:Vaciar string de JOptionPane en jLabel
Publicado por: javi in the sky en 13 de Marzo 2013, 23:47
Creo que en String response=JOptionPane.showInputDialog(null, "Ingrese nuevo dx"); no debes poner null, sino el objeto padre.

Fíjate en este ejemplo, creo te sirva para ver como modificar un label con el contenido introducido por el usuario:

Código: [Seleccionar]
//(1) PAQUETE

import java.awt.*;

import javax.swing.*;

//(2) FORMULARIO

public class Programa1 extends JFrame {

    //(3) CONTROLES DEL FORMULARIO       

    JPanel jpanel = (JPanel) this.getContentPane();

    JLabel jlabel = new JLabel();

    JTextField jtextfield = new JTextField();   

    //(4) CONSTRUCTOR DEL FORMULARIO       

    public Programa1() {

        //(5) PROPIEDADES DEL CONTENEDOR

        jpanel.setLayout(null);

        jpanel.setBackground(Color.lightGray);

        //(6) PROPIEDADES DE LOS CONTROLES         

        jlabel.setBounds(new Rectangle(5, 15, 220, 21));

        jlabel.setText("Introduzca su peso en kgs por favor");

        jtextfield.setBounds(new Rectangle(225, 15, 80, 21));

        //(7) ADICION DE LOS CONTROLES AL CONTENEDOR       

        jpanel.add(jlabel, null);

        jpanel.add(jtextfield, null);

        //(8) PROPIEDADES DEL FORMULARIO         

        setSize(300,150);

        setTitle("Form1");

        setVisible(true);

        // Con caja de texto
        String seleccion = JOptionPane.showInputDialog(
                this,
                "Petición de datos",
                JOptionPane.QUESTION_MESSAGE);  // el icono sera un iterrogante

        //System.out.println("El usuario ha escrito "+seleccion);

        if((seleccion!=null) && (seleccion.length() > 0)) {
            jlabel.setText(seleccion);  }     

    }

    //(9) METODOS DEL FORMULARIO     

    public static void main(String arg[]) {

        new Programa1();

    }

}

Título: Re:Vaciar string de JOptionPane en jLabel
Publicado por: cardiobeta en 14 de Marzo 2013, 00:51
Gracias por tu respuesta. Ya resolvi El problema era que no estaba hecha adecuadamente la declracion del jlabel.