从ComboBox过滤查询填充的JTable

时间:2016-05-11 20:40:50

标签: java eclipse swing combobox jtable

我有一个Jtable填充了我的Sql表中的查询,我有一些" ComboBox"这也是由查询填充,我想要的是能够根据用户ComboBox选择过滤我的Jtable的数据,但我不知道我该怎么做

这是我的Sql.class:

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Sql extends JFrame{
JTable table = new JTable();
DefaultTableModel model = new DefaultTableModel(new Object[][]{},new     String[]{"fecha","clave_pdv","pdv","turno","clave_platillo","platillo","precio","total sin iva"});
private TableRowSorter<DefaultTableModel> sorter;

Connection conn = null;
Connection conn1 = null;
Statement st = null;
Statement st1 = null;
ResultSet rs = null;
ResultSet rs1 = null;
//Create list of values


private final JComboBox comboBox = new JComboBox();
private final JComboBox comboBox_1 = new JComboBox();



public Sql(){

    try{
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        conn =     DriverManager.getConnection("jdbc:sqlserver://ARTURO-LAPTOP;user=sa;password=sacompusis;database=PDV");
        st = conn.createStatement();
        rs= st.executeQuery("SELECT DISTINCT Nombre_Pdv FROM   VENTA_PLATILLOS");

        while(rs.next()){
            comboBox.addItem(rs.getString(1));  
        }

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

    try{
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        conn1 =     DriverManager.getConnection("jdbc:sqlserver://ARTURO-LAPTOP;user=sa;password=sacompusis;database=PDV");
        st1 = conn.createStatement();
        rs1 = st1.executeQuery("SELECT DISTINCT Nombre_Turno FROM   VENTA_PLATILLOS");
        while(rs1.next()){
            comboBox_1.addItem(rs1.getString(1));

        }

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





    getContentPane().setLayout(null);
    table.setModel(model);
    table.setBounds(50, 50, 50, 50);
    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setBounds(139, 88, 535, 227);
    getContentPane().add(scrollPane);
    comboBox.setBounds(404, 11, 130, 31);
    getContentPane().add(comboBox);
    comboBox_1.setBounds(544, 11, 130, 31);

    getContentPane().add(comboBox_1);

    //Populate table
    sqlConection bd = new sqlConection();
    List<Value> values = bd.selectAll();
    for(Value v : values){
        model.addRow(new Object[]{v.fecha,v.clave_pdv,v.pdv,v.turno,v.clave_platillo,v.platillo,v.precio,v.total});
    }
}



public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable(){
        public void run() {
            Sql ms = new Sql();
            ms.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            ms.pack();
            ms.setVisible(true);
        }});
}
}

我的Connection Class sqlConnection.class:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class sqlConection {

public List<Value> selectAll(){
    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;
    //Create list of values
    List<Value> values = new ArrayList<Value>();

    try{
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        conn =     DriverManager.getConnection("jdbc:sqlserver://ARTURO-LAPTOP;user=sa;password=sacompusis;database=PDV");
        st = conn.createStatement();
        rs = st.executeQuery("select Fecha,Clave_PDV,Nombre_Pdv,Nombre_Turno,Platillo,Nombre_Platillo,Precio,Total from VENTA_PLATILLOS");

        while(rs.next()){
            Value v = new Value();
            v.setFecha(rs.getDate("Fecha"));
            v.setclave_pdv(rs.getString("Clave_PDV"));
            v.setpdv(rs.getString("Nombre_Pdv"));
            v.setturno(rs.getString("Nombre_Turno"));
            v.setclave_platillo(rs.getString("Platillo"));
            v.setplatillo(rs.getString("Nombre_Platillo"));
            v.setprecio(rs.getFloat("Precio"));
            v.setTotal(rs.getFloat("Total"));


            values.add(v);
        }
    }
    catch(Exception e){
        e.printStackTrace();
    }
    finally{
        try {
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            st.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    return values;
}
}

我如何制作我的&#34;组合框&#34;根据用户选择??

对表项进行排序

0 个答案:

没有答案