首页 > 解决方案 > 按 jcombobox 搜索 / 按 jcombobox 过滤

问题描述

我想通过 jcombobox 和 jtexfield 做一个过滤器,我做了一个 jtextfield,这是我的 DAO 和我的 VIEW,但我不知道如何通过组合添加搜索,表格中显示了代码和问题,这些问题是与组合中的专业相关并且我要过滤

public List buscarPregunta(String texto)throws Exception{
         ResultSet res;
         List listaPregunta = new ArrayList();
         cn = new Conexion();    
         try {
             Conexion cn = new Conexion();
             String filtro = "" + texto + "_%";
             String sql="SELECT *FROM TablaPregunta where PreguntaNombre like "+'"'+filtro+'"';
             PreparedStatement consulta = cn.getConnection().prepareStatement(sql);
             res = consulta.executeQuery();
             while (res.next()) {                 
             PreguntaModel ps = new PreguntaModel();
             ps.setIdPregunta(res.getInt("PreguntaID"));
             ps.setPregunta(res.getString("PreguntaNombre"));;
             listaPregunta.add(ps);     
             }
         } catch (Exception e) {
             System.out.println(e.getMessage());
         }
         cn.cerrarConexion();
         return listaPregunta;
     }

这是视图

public void buscarPreguntas() {
        LimpiarTablaPreguntas();
        Object[] obj = new Object[2];
        List ls;
        t = 0;
        try {
            ls = dao.buscarPregunta(txtPreguntando.getText());
            for (int i = 0; i < ls.size(); i++) {
                ps = (PreguntaModel) ls.get(i);
                obj[0] = ps.getIdPregunta();
                obj[1] = ps.getPregunta();
                modelo.addRow(obj);
                t++;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        this.TablaPreguntas.setModel(modelo);
    }

这是我要搜索的组合

public void cargarlistaEspecialidad() {
        ResultSet res;
        try {
            Conexion con = new Conexion();
            String sql = "Select * from TablaEspecialidad";
            PreparedStatement consulta = con.getConnection().prepareStatement(sql);
            res = consulta.executeQuery();
            combo.removeAllElements();
            while (res.next()) {
                combo.addElement(res.getString("EspecialidadNombre"));
            }
            jComboBox1.setModel(combo);
            con.cerrarConexion();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

标签: javaswingnetbeansjcombobox

解决方案


推荐阅读