Java 对JTable中具有特定值的行进行计数

Java 对JTable中具有特定值的行进行计数,java,jtable,Java,Jtable,因此,我试图找出如何计算表中具有特定值的行数,并将其显示在标签中,例如,我想计算书籍软装订的所有行数 private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { // count total of books that is softbound for(int i =0; i<jTable2.getRowC

因此,我试图找出如何计算表中具有特定值的行数,并将其显示在标签中,例如,我想计算书籍软装订的所有行数

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     // count total of books that is softbound
      for(int i =0; i<jTable2.getRowCount(); i++){
      if(jTable2.getValueAt(i, 6) == "SoftBound")
          {
             //val = Integer.valueOf(jTable2.getValueAt(i, 6).toString());
              int numrow = jTable2.getRowCount();
              jLabel11.setText(Integer.toString(numrow));
          }

      }  
     
 }
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt){
//计算软装订的书籍总数

对于(int i=0;i您正在错误地比较
String
s,请参阅。此外,您当前应用于行计数的逻辑存在错误

当前,您没有缩小仅使用“软绑定”的行数。如果“软绑定”与检索到的值匹配,您仍将显示完整的行数

要修复逻辑,您需要创建某种计数器,按原样循环行,正确比较值(使用equals或equalsIgnoreCase),在每个找到的行上递增计数器,然后在循环所有行后,显示找到的金额

修复所用方法的工作示例

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> buildGui());
}

private static void buildGui() {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    String[] header = { "ISBN", "Title", "Type" };
    Object[][] data = { { 1111, "Title1", "Softbound" }, { 2222, "Title2", "Softbound" }, { 3333, "Title3", "Hardbound" } };

    JTable table = new JTable(data, header);
    JLabel label = new JLabel("Found result:");
    JLabel labelAmount = new JLabel();

    JComboBox<String> comboBox = new JComboBox<>(new String[] { "Hardbound", "Softbound" });

    JScrollPane scrollPane = new JScrollPane(table);
    JButton btn = new JButton("Get amount");
    btn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            int amount = 0;
            for (int i = 0; i < table.getRowCount(); i++) {
                if (table.getValueAt(i, 2).equals(comboBox.getSelectedItem())) {
                    amount++;
                }
            }
            labelAmount.setText("" + amount);
        }
    });

    frame.add(scrollPane, BorderLayout.CENTER);
    panel.add(comboBox);
    panel.add(btn);
    panel.add(label);
    panel.add(labelAmount);
    frame.add(panel, BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}
publicstaticvoidmain(字符串[]args){
调用器(()->buildGui());
}
私有静态void buildGui(){
JFrame=新JFrame();
JPanel面板=新的JPanel();
字符串[]头={“ISBN”,“Title”,“Type”};
对象[][]数据={{1111,“标题1”,“软绑定”},{2222,“标题2”,“软绑定”},{3333,“标题3”,“硬绑定”};
JTable table=新的JTable(数据、标题);
JLabel label=新的JLabel(“找到的结果:”);
JLabel labelAmount=新的JLabel();
JComboBox组合框=新的JComboBox(新字符串[]{“硬绑定”、“软绑定”});
JScrollPane scrollPane=新的JScrollPane(表);
JButton btn=新JButton(“获取金额”);
btn.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
整数金额=0;
对于(int i=0;i
结果

旁注

如果要修改
JTable
中显示的值,特别是通过排序和筛选,可以使用教程中所示的一些有用工具来完成。请仔细查看和