0

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.
        }
        }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3651840
  • 39
  • 2
  • 9
  • Already answered - see http://stackoverflow.com/questions/5520548/is-there-anyway-i-can-highlight-a-row-in-jtable – DavidPostill Jun 08 '14 at 07:45
  • *"i want to search data from jtable.."* See [`JTable`: Sorting and Filtering](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting). – Andrew Thompson Jun 08 '14 at 09:58

3 Answers3

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

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/

enter image description here

Joe
  • 140
  • 1
  • 1
  • 9
0
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.

Fred
  • 51
  • 5