JComboBox in a public class
<p><code>enter code here</code>Hi I'm new developer in Java I want to put a JComboBox in a public class to get the selected item  but Java don't recognize it for example i want to do this.</p>

<pre><code>myComboBox.getSelectedItem().toString()
</code></pre>

<p>and when i try to put a JTextField it works fine.</p>

<p>Sorry for my expression I'm not speak English very well</p>

<p>Thats my code</p>

<pre><code>package guarderia;

import java.awt.BorderLayout;

public class enfermedades extends JFrame {

    private JPanel contentPane;
    private JTextField txtid;
    private JTextField textField_1;
    private JTextField txtnom;
    private static JTable table;



    //VARIABLES PARA CONEXION A MYSQL
    static Connection con;
    static CallableStatement ps;
    static ResultSet rs;
    static DefaultTableModel tm;
    static Statement sql;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    enfermedades frame = new enfermedades();
                    frame.setVisible(true);
                    llenar();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public enfermedades() {


        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1211, 663);
        contentPane = new JPanel();
        contentPane.setBackground(new Color(255, 255, 255));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBackground(new Color(0, 0, 0));
        panel.setBounds(0, 0, 1624, 53);
        contentPane.add(panel);

        JLabel lblEnfermedades = new JLabel("ENFERMEDADES");
        lblEnfermedades.setForeground(Color.WHITE);
        lblEnfermedades.setFont(new Font("Dialog", Font.BOLD, 19));
        lblEnfermedades.setBounds(12, 12, 231, 41);
        panel.add(lblEnfermedades);

        JPanel panel_1 = new JPanel();
        panel_1.setLayout(null);
        panel_1.setBounds(10, 102, 426, 510);
        contentPane.add(panel_1);

        JLabel lblIdNio = new JLabel("ID Niño");
        lblIdNio.setBounds(12, 30, 70, 15);
        panel_1.add(lblIdNio);

        txtid = new JTextField();
        txtid.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                int c = e.getKeyChar();
                if (c &lt; '0' || c &gt; '9') e.consume();
                    }

            @Override
            public void keyReleased(KeyEvent e) {
                con= (Connection) conexion.conectar();
                try {
                    if (txtid.getText().equals(""))
                    {
                        txtnom.setText("");
                    }
                    ps = (CallableStatement) con.prepareCall("{call select_nin (?) }");
                    ps.setLong(1, Integer.parseInt(txtid.getText()));



                    ResultSet res = ps.executeQuery();


                    if (res.next())
                    {
                    String tipo = res.getString("Nombre");
                    txtnom.setText(tipo);
                    llenar();
                    }
                    else
                    {
                        txtnom.setText(null);

                    }
                } catch (SQLException e1) {
                    JOptionPane.showMessageDialog(null, e1 );
                    }





            }


        });
        txtid.setColumns(10);
        txtid.setBounds(120, 28, 146, 19);
        panel_1.add(txtid);



        JLabel cbi = new JLabel("Enfermedad");
        cbi.setBounds(12, 95, 194, 15);
        panel_1.add(cbi);

        JLabel lblTratamiento = new JLabel("Tratamiento");
        lblTratamiento.setBounds(12, 130, 194, 15);
        panel_1.add(lblTratamiento);

        JButton btnNueva = new JButton("Nueva");
        btnNueva.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {


            }
        });
        btnNueva.setBounds(282, 90, 88, 25);
        panel_1.add(btnNueva);

        JTextPane textPane = new JTextPane();
        textPane.setBounds(12, 157, 402, 118);
        panel_1.add(textPane);

        JLabel lblFechaDeteccion = new JLabel("Fecha Deteccion");
        lblFechaDeteccion.setBounds(12, 299, 194, 15);
        panel_1.add(lblFechaDeteccion);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(135, 297, 95, 19);
        panel_1.add(textField_1);

        JLabel lblNombre = new JLabel("Nombre");
        lblNombre.setBounds(12, 61, 70, 15);
        panel_1.add(lblNombre);

        txtnom = new JTextField();
        txtnom.setEditable(false);
        txtnom.setColumns(10);
        txtnom.setBounds(120, 56, 294, 19);
        panel_1.add(txtnom);

        JLabel lblMedidadRecomendadas = new JLabel("Medidas recomendadas");
        lblMedidadRecomendadas.setBounds(12, 330, 194, 15);
        panel_1.add(lblMedidadRecomendadas);

        JTextPane textPane_1 = new JTextPane();
        textPane_1.setBounds(12, 355, 402, 93);
        panel_1.add(textPane_1);

        JButton btnGuardar = new JButton("Guardar");
        btnGuardar.setBounds(297, 470, 117, 25);
        panel_1.add(btnGuardar);

        JComboBox cbx = new JComboBox();
        cbx.setBounds(120, 90, 146, 24);
        panel_1.add(cbx);

        JPanel panel_2 = new JPanel();
        panel_2.setLayout(null);
        panel_2.setBackground(new Color(220, 20, 60));
        panel_2.setBounds(10, 65, 426, 32);
        contentPane.add(panel_2);

        JLabel lblRegistrarEnfermedad = new JLabel("Registrar enfermedad");
        lblRegistrarEnfermedad.setForeground(Color.WHITE);
        lblRegistrarEnfermedad.setFont(new Font("Dialog", Font.BOLD, 19));
        lblRegistrarEnfermedad.setBounds(12, 5, 401, 23);
        panel_2.add(lblRegistrarEnfermedad);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(448, 102, 751, 510);
        contentPane.add(scrollPane);

        table = new JTable();
        scrollPane.setViewportView(table);

        JPanel panel_3 = new JPanel();
        panel_3.setLayout(null);
        panel_3.setBackground(new Color(128, 0, 128));
        panel_3.setBounds(448, 65, 751, 32);
        contentPane.add(panel_3);

        JLabel lblEnfermedades_1 = new JLabel("Enfermedades");
        lblEnfermedades_1.setForeground(Color.WHITE);
        lblEnfermedades_1.setFont(new Font("Dialog", Font.BOLD, 19));
        lblEnfermedades_1.setBounds(12, 5, 401, 23);
        panel_3.add(lblEnfermedades_1);
    }



    public static void llenar()
    {
        try
        {

            con = (Connection) conexion.conectar();
            String[]titulos={"ID", "ENFERMEDAD", "DESCRIPCION", "RIESGO CONTAGIO", "TIPO"};
            String cmd = "select * from enfermedades";
            tm = new DefaultTableModel(null,titulos);
            sql = (Statement) con.createStatement();
            ResultSet rs = sql.executeQuery(cmd);
            String[]fila = new String[5];

            while(rs.next())
            {
                fila[0]= rs.getString("id");
                fila[1]= rs.getString("enfermedad");
                fila[2]= rs.getString("descripcion");
                fila[3]= rs.getString("riesgo");
                fila[4]= rs.getString("tipo");  

                tm.addRow(fila);

            }
            table.setModel(tm);
            TableColumnModel columnModel = table.getColumnModel();
            columnModel.getColumn(0).setPreferredWidth(3);
            columnModel.getColumn(1).setPreferredWidth(50);
            columnModel.getColumn(2).setPreferredWidth(290);



        }
        catch(Exception d)
        {
            JOptionPane.showMessageDialog(null, d + " No se pudo conectar.");
        }


    }
     public void ns()
     {
         cbx. //this is where not recognize the jcombobox
     }

}
</code></pre>