Autor Tema: Java que al elegir Item en JComboBox mostrar palabra en JTextField (Eclipse)  (Leído 3618 veces)

aprendiendo

  • Sin experiencia
  • *
  • APR2.COM
  • Mensajes: 2
    • Ver Perfil
No logro que al elegir un Item dentro de un JComboBox me arroje en este caso una palabra en un JTextField... les dejo el código  que hice aunque en la parte lógica por lo visto esta mal . Gracias y quedo atento a cualquier mensaje de ayuda.

Código: [Seleccionar]
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Productos_ extends JFrame implements ActionListener {

private JLabel jlDatos;
private JLabel jlID;
private JLabel jlNombre;
private JLabel jlCategoria;
private JLabel jlStock;
private JLabel jlPrecio_venta;
private JComboBox jcbID_prod;
private JTextField jtfNombre;
private JTextField jtfcategoria;
private JLabel jlnum_Stock;
private JTextField jtfprecio_venta;
private JButton btninicio;

public Productos_(){


setSize(500,500);
setTitle("Productos");
initcomponents();

}



private void initcomponents() {
getContentPane().setLayout(null);


jlDatos = new JLabel("Datos del Producto ");
getContentPane().add(jlDatos);
jlDatos.setBounds(20, 20, 160, 20);

jlID = new JLabel("ID Producto: ");
getContentPane().add( jlID);
jlID.setBounds(20, 50, 100, 20);

String ID[] = { "111-229A","111-002B","111-992C","111-882D","111-772E","111-662F","111-552G","111-222H","111-442J","111-332K", };
jcbID_prod = new JComboBox();
getContentPane().add(jcbID_prod);
jcbID_prod.setBounds(130, 50, 100, 20);
jcbID_prod.addActionListener(this);


for(int i=0;i<=9; i++){
jcbID_prod.addItem (ID[i]);

}

jlNombre = new JLabel("Nombre");
getContentPane().add(jlNombre);
jlNombre.setBounds(20, 80, 100, 20);

jtfNombre= new JTextField();
getContentPane().add(jtfNombre);
jtfNombre.setBounds(130,80, 100, 20);


jlCategoria = new JLabel("Categoria: ");
getContentPane().add(jlCategoria);
jlCategoria.setBounds(20, 110, 100, 20);

jtfcategoria= new JTextField();
getContentPane().add(jtfcategoria);
jtfcategoria.setBounds(130,110, 100, 20);

jlStock = new JLabel("Stock: ");
getContentPane().add(jlStock);
jlStock.setBounds(20, 140, 100, 20);

jlnum_Stock = new JLabel(" ");
getContentPane().add(jlnum_Stock);
jlnum_Stock.setBounds(130, 140, 100, 20);

jlPrecio_venta = new JLabel("Precio Venta: ");
getContentPane().add(jlPrecio_venta);
jlPrecio_venta.setBounds(20, 170, 100, 20);

jtfprecio_venta= new JTextField();
getContentPane().add(jtfprecio_venta);
jtfprecio_venta.setBounds(130, 170, 100, 20);

btninicio = new JButton("INICIO");
getContentPane().add(btninicio );
btninicio .setBounds(20, 220, 150, 70);
btninicio .addActionListener(this);
}

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

}
public void actioPerformed(ActionEvent e ){

String ID = (jcbID_prod.getSelectedItem().toString());

if (e.getSource()== "111-229A"){
jtfNombre.setText("Manzana");


}
if( e.getSource()== "111-002B"){
jtfNombre.setText("Pera");


}
if( e.getSource()=="111-992C"){
jtfNombre.setText("Naranja");


}
if( e.getSource()== "111-882D"){

jtfNombre.setText("Mandarina");


}
if( e.getSource()== "111-772E"){
jtfNombre.setText("Platano");


}
if( e.getSource()== "111-662F"){
jtfNombre.setText("Sandia");


}
if( e.getSource()== "111-552G"){
jtfNombre.setText("Melon");


}
if( e.getSource()== "111-222H"){
jtfNombre.setText("Granada");


}
if( e.getSource()== "111-442J"){
jtfNombre.setText("Pomelo");


}
if(e.getSource()== "111-332K"){
jtfNombre.setText("Papaya");


}

}
}
« Última modificación: 30 de Noviembre 2016, 20:33 por Alex Rodríguez »

ESOJ

  • Intermedio
  • ***
  • APR2.COM
  • Mensajes: 143
    • Ver Perfil
Re:dudas en java eclipse
« Respuesta #1 en: 27 de Noviembre 2016, 08:32 »
Buenos días.
A ver si te sirve:
Código: [Seleccionar]
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Productos_ extends JFrame implements ActionListener {
    private JLabel jlDatos;
    private JLabel jlID;
    private JLabel jlNombre;
    private JLabel jlCategoria;
    private JLabel jlStock;
    private JLabel jlPrecio_venta;
    private JComboBox jcbID_prod;
    private JTextField jtfNombre;
    private JTextField jtfcategoria;
    private JTextField jlnum_Stock;
    private JTextField jtfprecio_venta;
    private JButton btninicio;
    private String currentPattern;
    public Productos_(){
        setSize(500,500);
        setTitle("Productos");
        initcomponents();
    }

    private void initcomponents() {
        String[] patternExamples = { "","111-229A","111-002B","111-992C","111-882D","111-772E","111-662F","111-552G","111-222H","111-442J","111-332K"};
        currentPattern = patternExamples[0];
        getContentPane().setLayout(null);
        jlDatos = new JLabel("Datos del Producto ");
        getContentPane().add(jlDatos);
        jlDatos.setBounds(20, 20, 160, 20);

        jlID = new JLabel("ID Producto: ");
        getContentPane().add( jlID);
        jlID.setBounds(20, 50, 100, 20);

        jcbID_prod = new JComboBox(patternExamples);
        jcbID_prod.setEditable(false);
        jcbID_prod.addActionListener(this);

        getContentPane().add(jcbID_prod);
        jcbID_prod.setBounds(130, 50, 100, 20);

        jlNombre = new JLabel("Nombre");
        getContentPane().add(jlNombre);
        jlNombre.setBounds(20, 80, 100, 20);

        jtfNombre= new JTextField();
        getContentPane().add(jtfNombre);
        jtfNombre.setBounds(130,80, 100, 20);

        jlCategoria = new JLabel("Categoria: ");
        getContentPane().add(jlCategoria);
        jlCategoria.setBounds(20, 110, 100, 20);

        jtfcategoria= new JTextField();
        getContentPane().add(jtfcategoria);
        jtfcategoria.setBounds(130,110, 100, 20);

        jlStock = new JLabel("Stock: ");
        getContentPane().add(jlStock);
        jlStock.setBounds(20, 140, 100, 20);

        jlnum_Stock = new JTextField();
        getContentPane().add(jlnum_Stock);
        jlnum_Stock.setBounds(130, 140, 100, 20);

        jlPrecio_venta = new JLabel("Precio Venta: ");
        getContentPane().add(jlPrecio_venta);
        jlPrecio_venta.setBounds(20, 170, 100, 20);

        jtfprecio_venta= new JTextField();
        getContentPane().add(jtfprecio_venta);
        jtfprecio_venta.setBounds(130, 170, 100, 20);

        btninicio = new JButton("INICIO");
        getContentPane().add(btninicio );
        btninicio .setBounds(20, 220, 150, 70);
        btninicio .addActionListener(this);
    }

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

    }

    public void actionPerformed(ActionEvent e ){
        if (e.getActionCommand().equals("INICIO")){  new Productos_().setVisible(true);}
        else{         
            String newSelection = (String) jcbID_prod.getSelectedItem();
            if (newSelection.equals("")){
                jtfNombre.setText("");
                jtfcategoria.setText("");
                 jtfprecio_venta.setText("");
                jlnum_Stock.setText("");
            }
            if (newSelection.equals("111-229A")){
                jtfNombre.setText("Manzana");
                jtfcategoria.setText("Extra");
                jtfprecio_venta.setText("2 eu/kg");
                jlnum_Stock.setText("10 kg");
            }
            if( newSelection.equals( "111-002B")){
                jtfNombre.setText("Pera");
                jtfcategoria.setText("Extra");
                jtfprecio_venta.setText("1.5 eu/kg");
                jlnum_Stock.setText("20 kg");
            }
            if( newSelection.equals("111-992C")){
                jtfNombre.setText("Naranja");
                jtfcategoria.setText("Normal");
                jtfprecio_venta.setText("2,25 eu/kg");
                jlnum_Stock.setText("35 kg");
            }
            if( newSelection.equals("111-882D")){
                jtfNombre.setText("Mandarina");
                jtfcategoria.setText("Extra");
                jtfprecio_venta.setText("2 eu/kg");jlnum_Stock.setText("50 kg");
            }
            if( newSelection.equals("111-772E")){
                jtfNombre.setText("Platano");
                jtfcategoria.setText("Normal");
                jtfprecio_venta.setText("2,5 eu/kg");jlnum_Stock.setText("5 kg");
            }
            if( newSelection.equals ("111-662F")){
                jtfNombre.setText("Sandia");
                jtfcategoria.setText("Extra");
                jtfprecio_venta.setText("1 eu/kg");jlnum_Stock.setText("25 kg");
            }
            if( newSelection.equals("111-552G")){
                jtfNombre.setText("Melon");
                jtfcategoria.setText("Normal");
                jtfprecio_venta.setText("1,5 eu/kg");jlnum_Stock.setText("20 kg");
            }
            if( newSelection.equals("111-222H")){
                jtfNombre.setText("Granada");
                jtfcategoria.setText("Extra");
                jtfprecio_venta.setText("3 eu/kg");jlnum_Stock.setText("15 kg");
            }
            if( newSelection.equals ("111-442J")){
                jtfNombre.setText("Pomelo");
                jtfcategoria.setText("Normal");
                jtfprecio_venta.setText("3,5 eu/kg");jlnum_Stock.setText("10 kg");
            }
            if(newSelection.equals("111-332K")){
                jtfNombre.setText("Papaya");
                jtfcategoria.setText("Normal");
                jtfprecio_venta.setText("3 eu/kg");jlnum_Stock.setText("30 kg");
            }

        }
    }
}

Un saludo

 

Sobre la educación, sólo puedo decir que es el tema más importante en el que nosotros, como pueblo, debemos involucrarnos.

Abraham Lincoln (1808-1865) Presidente estadounidense.

aprenderaprogramar.com: Desde 2006 comprometidos con la didáctica y divulgación de la programación

Preguntas y respuestas

¿Cómo establecer o cambiar la imagen asociada (avatar) de usuario?
  1. Inicia sesión con tu nombre de usuario y contraseña.
  2. Pulsa en perfil --> perfil del foro
  3. Elige la imagen personalizada que quieras usar. Puedes escogerla de una galería de imágenes o subirla desde tu ordenador.
  4. En la parte final de la página pulsa el botón "cambiar perfil".