enum attribute combox

0

I have an attribute of type enum {V, M} and I have to assign the value through a combox from a swing application that I am doing from another class of another package, the fact is that I do not know how to invoke the attribute from its class to that of the application within the event method.

Could you give me an example?

    
asked by rodic 09.03.2018 в 21:08
source

2 answers

0

Surely with this example you can guide yourself better. Enum class

public enum DiasSemanas {
 LUNES, MARTES, MIERCOLES, JUEVES, VIERNES, SABADO, DOMINGO;
}

Then a class that implements it

public class Enumeraciones {


public Enumeraciones() {
    super();

}

public void actividadesDiarias(DiasSemanas ds) {
    switch(ds) {
    case LUNES:
        System.out.println("Es el primer dia de la semana");
        break;
    case MARTES:
        System.out.println("Es el segundo dia de la semana");
        break;
    case MIERCOLES:
        System.out.println("Es el tercer dia de la semana");
        break;
    case JUEVES:
        System.out.println("Es el cuarto dia de la semana");
        break;
    case VIERNES:
        System.out.println("Es el quinto dia de la semana");
        break;
    case SABADO:
        System.out.println("Es el sexto dia de la semana");
        break;
    case DOMINGO:
        System.out.println("Es el septimo dia de la semana");
        break;
    default:
        System.out.println("Opcion no valida...!!!");
        break;


    }

}

and finally I execute it from a main class

public class Ejemplos {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Enumeraciones enumeraciones = new Enumeraciones();
    enumeraciones.actividadesDiarias(DiasSemanas.LUNES);
    enumeraciones.actividadesDiarias(DiasSemanas.MIERCOLES);

    String[] lista = { "Lunes", "Martes", "Miercoles", "Jueves", "Viernes","Sabado", "Domingo" };


    JComboBox combo = new JComboBox(lista);
    combo.setSelectedIndex(0); // establecemos el primer valor por defecto
    combo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            System.out.println("Evento");

            if (combo.getSelectedIndex() == 5) {
                enumeraciones.actividadesDiarias(DiasSemanas.SABADO);   
            }



        }
    });


    JFrame ventana = new JFrame();
    ventana.getContentPane().setLayout(new FlowLayout());
    ventana.getContentPane().add(combo);
    ventana.pack();
    ventana.setVisible(true);
    ventana.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

}

Here only what you have left is in this last class to implement the JComboBox

    
answered by 10.03.2018 в 16:26
0

Thank you very much!

I think I have the same, but I still get an error. I detail it a little summarized:

On the one hand I have the class with the enum attribute:

public class Medicos{

private enum Respuesta {S,N};

public void eleccionResp(Respuesta r){
    switch(r) {
        case S:
            r = Respuesta.S;
            break;
        case N:
            r = Respuesta.N;
            break;
    }

}

On the other hand I have the class with the application and a method with the event:

private void jCB1ActionPerformed(java.awt.event.ActionEvent evt) {

  Medicos m5 = new Medicos();
    int i = jCB1.getSelectedIndex();
    if(i == 0){
        m5.eleccionResp(r.S));// Aquí me da error (No puede encontrar la variable r)
    }else{}
 }    

I miss something with the variable that I can not find out ...

    
answered by 10.03.2018 в 20:06