Calling a JComboBox from a JFrame from a class

0

I am working on a desktop application. The functional is realized in a class of a specific package. My code is as follows

public void cargarComboCurso()
    {
        String url = "jdbc:sqlserver://localhost:1433;databaseName=Escuela";
        String user = "user";
        String password = "password";
        String sql = "SELECT nombreCurso FROM Cursos";

        try(Connection cn = DriverManager.getConnection(url, user, password);
                Statement st = cn.createStatement();
                ResultSet rs = st.executeQuery(sql);)
        {
            cboCurso.removeAllItems();

            while (rs.next())
            {
                cboCurso.addItem(rs.getString("nombreCurso"));
            }
        }
        catch(SQLException e)
        {
            System.out.println("ERROR : ");
            e.printStackTrace(System.out);
        }
    }

The problem is that my combo cboCurso is in a JFrame of another package and when I can not access it, the program throws me an error of can not find symbol

    
asked by Lucas. D 06.09.2017 в 01:36
source

1 answer

1

As Davilo said, I must pass as a parameter the Combobox.

public void cargarComboCurso(JComboBox cboCurso)
    {
        String url = "jdbc:sqlserver://localhost:1433;databaseName=Escuela";
        String user = "user";
        String password = "password";
        String sql = "SELECT nombreCurso FROM Cursos";

        try(Connection cn = DriverManager.getConnection(url, user, password);
                Statement st = cn.createStatement();
                ResultSet rs = st.executeQuery(sql);)
        {
            cboCurso.removeAllItems();

            while (rs.next())
            {
                cboCurso.addItem(rs.getString("nombreCurso"));
            }
        }
        catch(SQLException e)
        {
            System.out.println("ERROR : ");
            e.printStackTrace(System.out);
        }
    }
    
answered by 06.09.2017 / 03:35
source