使用JcomboBox过滤JTable

时间:2016-05-12 14:42:01

标签: java jtable jcombobox

我试图使用我的Jcomboox过滤我的Jtabke,但我不能这样做,并在: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting 它只说如何使用搜索txt字段过滤或使用标题排序,但不知道如何使用JComboBox更改事件来过滤JTable是否有一个简单的代码来执行此操作?

EDITED GOT iT WORKING

这是我的代码:

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"});
public 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();
private JTextField textField;




public Sql(){

    table.setAutoCreateRowSorter(true);
    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);

    textField = new JTextField();
    textField.setBounds(24, 16, 86, 20);
    getContentPane().add(textField);
    textField.setColumns(10);


    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
             RowFilter<DefaultTableModel, Object> rf  = RowFilter.regexFilter(comboBox.getSelectedItem().toString(), 2);
             sorter.setRowFilter(rf);

        }
    });
    comboBox_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
             RowFilter<DefaultTableModel, Object> rf  = RowFilter.regexFilter(comboBox_1.getSelectedItem().toString(), 3);
             sorter.setRowFilter(rf);


        }
    });


    sorter = new TableRowSorter<DefaultTableModel>(model);
    table.setRowSorter(sorter);


    //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});
    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用TableRowSorter,在表格中填入一些数字并对其进行过滤......

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

public class TableFilter extends JFrame {

    private JTable table;
    private DefaultTableModel model;
    private TableRowSorter<DefaultTableModel> sorter;

    public TableFilter() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initComponents();
        pack();
        setVisible(true);

    }

    public static void main(String args[]) {
        new TableFilter();
    }

    private void initComponents() {
        JPanel panel = new JPanel();
        final JComboBox<String> comboBox = new JComboBox<>(new String[]{"","1","2","3"});
        JButton button = new JButton("filter");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                RowFilter<DefaultTableModel, Object> rf  = RowFilter.regexFilter(comboBox.getSelectedItem().toString(), 0);
                sorter.setRowFilter(rf);
            }
        });
        panel.add(comboBox);
        panel.add(button);

        table = new JTable(model = new DefaultTableModel(3,3));
        sorter = new TableRowSorter<DefaultTableModel>(model);
        table.setRowSorter(sorter);

        add(panel,BorderLayout.SOUTH);
        add(new JScrollPane(table));
    }
}

在表格中加上一些数字

enter image description here

过滤

enter image description here