【发布时间】:2016-05-13 00:22:45
【问题描述】:
我正在尝试使用我的 Jcomboox 过滤我的 Jtabke,但我做不到,并且在: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting 它只说如何使用搜索 txt 字段进行过滤或使用标题进行排序,而不是如何使用 JComboBox 更改事件来过滤 JTable 是否有简单的代码来执行此操作??
已编辑 让它正常工作
这是我的代码:
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});
}
}
【问题讨论】:
-
请出示您的代码。
-
Java 中没有“简单代码”这样的东西。到目前为止,您编写了哪些代码?
-
我看不到你在哪里初始化你的分拣机
sorter = new TableRowSorter<DefaultTableModel>(model);你看到我的答案了吗?它对你有什么帮助? -
您过滤的是哪一列(索引)?,该表是基于零索引的。