Swing - How to search JTable's Content

Dear Friend

Could any one please help me in Finding out the JTable's Content with a Search key word.

That in the JTable Content is displayed, Suppose i want to search a Name Let's say "Jofin". When i enter jofin The Entire Row should be Selected. and i want to read the Selected Rows Record.

Please give me an ideas or samples to do this

import java.awt.*;

import javax.swing.*;

import javax.swing.table.*;

class Testingextends JFrame

{

String colNames[] ={" string1 ","String2","String2"};

Object[][] data ={{"jothi","vimal","raj"},

{"jony","brito","kumar"},

{"kuru","banner","selvam"}};

DefaultTableModel dtm;

public Testing()

{

setLocation(50,50);

setDefaultCloseOperation(EXIT_ON_CLOSE);

JTable table =new JTable(data,colNames);

JScrollPane sp =new JScrollPane(table);

JLabel lbl =new JLabel("SREACH:");

JTextField txt =new JTextField(20);

JPanel pnl =new JPanel();

pnl.add(lbl);

pnl.add(txt);

getContentPane().add(sp);

getContentPane().add(pnl,BorderLayout.SOUTH);

pack();

}

publicstaticvoid main (String[] args){

new Testing().setVisible(true);}

}

You can run my code and tell me how to solve this

Thank you

jofin

[2833 byte] By [jofin123a] at [2007-11-26 23:13:27]
# 1

Lots of stuff for you to do here. Many of the mechanics behind it are found here:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

First figure out how to search the table. One way to do this is get the table model and then iterate through the rows and columns.

Once you have found a match, you need to figure out how to select the row. We have the column and row index from the search in the table model and by default the entire row is selected when a cell is selected.

Keep in mind columns can be rearranged by default, so if the column selection matters you'll have to convert using convertColumnIndexToModel or convertColumnIndexToView. And if you have any other fancy sorting or filtering you'll have to deal with both of those too.

Now to get the row's data, I would prefer to use an underlying data model, say make your rows Vectors or something like that, but I guess you could just use getValueAt to get the values for ach cell in the row.

pthorsona at 2007-7-10 14:11:37 > top of Java-index,Desktop,Core GUI APIs...