简体   繁体   中英

Filter JTable using a JcomboBox

Im trying to filter my Jtabke using my Jcomboox but i cant do it and in the : http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting it only says how to filter using search txt field or sort using headers but not how to use a JComboBox change event to filter a JTable is there a simple code to do this??

EDITED GOT iT WORKING

This is my Code:

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

Try this, using a TableRowSorter , fill the table with some numbers and filter it ...

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

Fill the table with some numbers

在此处输入图片说明

filter it

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM