Highlight found data in JTable row

寵の児 提交于 2019-12-13 06:50:32

问题


I want to search data from JTable when data found then I want to highlight table row. This code is work properly search record but I don't know what I do for highlight the row.

  String target = jTextField1.getText();
    for(int row = 0; row < jTable1.getRowCount(); row++)
    for(int col = 0; col < jTable1.getColumnCount(); col++)
        {
    String next = (String)jTable1.getValueAt(row, col);
        if(next.equals(target))
        {
            System.out.println("found");// here what change for highlight row.
        }
        }

回答1:


The answer depends on your idea of "highlighting"

You could use JTable#addRowSelection to highlight a row using the default selection

Or, you could setup your cell renders to apply additional highlighting support via an additional lookup to determine if the cell/row should be highlighted

Or, you could use the inbuilt filtering capabilities of the JTable to filter out unwanted content

See How to use tables for more details

Or, you could use the highlighting support from the SwingLabs, SwingX librRies




回答2:


We can achieve that with a custom JLabel and TableCellRenderer. Following example does the highlighting on the found (filtered) rows in JTable. The rows are filtered via RowFilter: http://www.logicbig.com/tutorials/core-java-tutorial/swing/jtable-row-filter-highlighting/




回答3:


else if(e.getSource()==field){
            int z;
            for(z = 0;z<table.getRowCount();z++){
                if(Integer.parseInt(field.getText()) == Integer.parseInt((String)table.getValueAt(z, 1))){
                    break;
                     }
            }
            table.setRowSelectionInterval(z, z);


        }

i had the same problem and this was how i went about it.



来源:https://stackoverflow.com/questions/24103513/highlight-found-data-in-jtable-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!