2

Hi am creating an application in which I am using JTable to listing of file or folder names.

My question is: How can I find and Search a Particular file or folder in JTable like in Windows. In windows directory listing when we press any key then we can see that file or folder start with that character is selected and if we again press same key then next file/folder is selected with start with that character.

fredley
  • 32,953
  • 42
  • 145
  • 236
Ronak Jain
  • 2,402
  • 2
  • 24
  • 38
  • 1
    Sound more like the functionality of a `JComboBox` or `JList` or perhaps a `JTree`. On the last note, see also [File Browser GUI](http://codereview.stackexchange.com/q/4446/7784) which uses a `JTree` (OK, and a `JTable`) to present directory trees (and directory listings). – Andrew Thompson Jan 23 '12 at 12:25
  • @Ronak Did you give JIDE a shot? I use that in my project when I need search-box functionality in a component – Unai Vivi Jan 30 '12 at 11:03

5 Answers5

2

If you can (and you're willing to) use third party UI components, the Open Source JIDE Common Layer offers a few nice components: e.g. you might like FolderChooser which has an automatic find-as-you-type functionality.

Here's the link: JIDE Common Layer. If you click the "RUN IT" button you can see a sample via Java Web Start.

I've used many JIDE components (only the open source ones) in my projects and avoided reinventing the wheel many times.

Hope this might help you.

Unai Vivi
  • 3,073
  • 3
  • 30
  • 46
2

If you can use 3th party code, I would suggest to take a look at the SwingX project. Their JXTable, JXTree, JXList and some other classes provide an implementation of the Searchable interface, which makes creating a search widget a breeze.

And if that is even too difficult, they provide out-of-the-box a JXFindPanel which provides a UI to search a Searchable

Robin
  • 36,233
  • 5
  • 47
  • 99
  • did you understood OP's question ???, because all these shots to the dark so nice covering :-) very lagre areas :-) +1 – mKorbel Jan 23 '12 at 22:30
  • @mKorbel I gave it my best shot, and from what I understood the SwingX behavior is what he is looking for. Only you have to type those letter(s) in a search field and not just when the table has the focus – Robin Jan 23 '12 at 22:36
  • thanks for reply. can you explain How to work with Searchable interface for windows like search on keypress. – Ronak Jain Jan 24 '12 at 04:58
1

You want an action happen when a key is typed ? -> add a keyListener to your table

You want to know which row is valid -> query your datamodel connected to your jtable

You want a selected row to change ? -> in the keytyped implementation of your listener change the selection

table.getSelectionModel().setSelectionInterval(1,1);
Peter
  • 5,728
  • 20
  • 23
1

Since I don't know something about how did you implement your code logics, JTable implemented Sorting and Filtering

but you describtions talking about JTreeTable

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

I would consider writing a custom TableCellRenderer, responsible for highlighting any matching letters in the String being rendered. When someone updates the search text field the simplest approach is going to then be to repaint the entire JTable to show the updated "match state" of the table cells.

Adamski
  • 54,009
  • 15
  • 113
  • 152