有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing如何在JTable java中搜索元素?

我用java制作了一个JTable。 除了搜索表中的元素,我什么都可以做。 我想创建一个输入对话框,这就是我现在拥有的:

btnSearch = new JButton("Search");
    btnSearch.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e) 
        {    
            String name = JOptionPane.showInputDialog("Wat wil je zoeken?");
        }
    });
    add(btnSearch);

我想用for循环。 但我不知道怎么做。 有人能帮我吗


共 (1) 个答案

  1. # 1 楼答案

    JTable的模型是存放数据的地方,通过搜索,您应该能够检查它是否包含您想要的内容 :

    for(int i = 0; i < table.getRowCount(); i++){//For each row
        for(int j = 0; j < table.getColumnCount(); j++){//For each column in that row
            if(table.getModel().getValueAt(i, j).equals("STRING_TO_SEARCH")){//Search the model
                System.out.println(table.getModel().getValueAt(i, j));//Print if found string
            }
        }//For loop inner
    }//For loop outer
    

    祝你好运